Skip to main content

Stdout

Struct Stdout 

Source
pub struct Stdout { /* private fields */ }
Expand description

Writes each span as one line of JSON (NDJSON): level and action first, then every attribute, then the error (error.type, error.message, error.code, error.file, error.line, error.trace with at most max_trace_frames frames, error.trace_truncated). Spans with an error go to standard error, the others to standard output.

Invalid UTF-8 is replaced with U+FFFD and a value that cannot be encoded becomes null, so one bad attribute cannot drop the whole record; when the record still cannot be encoded, a line naming the failure (level, action, error.type, export.error) is written instead.

PHP: Utopia\Span\Exporter\Stdout.

use std::io::Write;
use std::sync::{Arc, Mutex};
use utopia_span::{Exporter, Span, Stdout};

#[derive(Clone, Default)]
struct Buffer(Arc<Mutex<Vec<u8>>>);
impl Write for Buffer {
    fn write(&mut self, b: &[u8]) -> std::io::Result<usize> { self.0.lock().unwrap().write(b) }
    fn flush(&mut self) -> std::io::Result<()> { Ok(()) }
}

let out = Buffer::default();
let exporter = Stdout::new().writers(Box::new(out.clone()), Box::new(std::io::sink()));
let span = Span::new("job");
span.set("span.trace_id", "t").set("span.id", "s").set("span.started_at", 1.5).set("level", "info");
exporter.export(&span);

let line = String::from_utf8(out.0.lock().unwrap().clone()).unwrap();
assert_eq!(line, "{\"level\":\"info\",\"action\":\"job\",\"span.trace_id\":\"t\",\"span.id\":\"s\",\"span.started_at\":1.5}\n");
use Utopia\Span\Exporter\Stdout;
use Utopia\Span\Span;

$span = new Span('job');
$span->set('span.trace_id', 't')->set('span.id', 's')->set('span.started_at', 1.5)->set('level', 'info');
(new Stdout())->export($span);
// {"level":"info","action":"job","span.trace_id":"t","span.id":"s","span.started_at":1.5}

Implementations§

Source§

impl Stdout

Source

pub fn new() -> Self

An exporter of every span, with at most 3 stack frames per error, writing to the process’s standard output and error.

PHP: new Stdout().

Source

pub fn sampler( self, sampler: impl Fn(&Span) -> bool + Send + Sync + 'static, ) -> Self

Exports only the spans sampler accepts.

PHP: the $sampler argument.

Source

pub fn max_trace_frames(self, frames: usize) -> Self

Prints at most frames stack frames of an error.

PHP: the $maxTraceFrames argument.

Source

pub fn writers( self, out: Box<dyn Write + Send>, err: Box<dyn Write + Send>, ) -> Self

Writes spans to out, and spans with an error to err.

PHP: the $stdout and $stderr arguments.

Source

pub fn line(&self, span: &Span) -> Option<String>

The line this exporter writes for span (without its newline), or None when neither it nor the fallback line can be encoded.

Trait Implementations§

Source§

impl Debug for Stdout

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Stdout

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Exporter for Stdout

Source§

fn sample(&self, span: &Span) -> bool

Whether to export span; false drops it. Read more
Source§

fn export(&self, span: &Span)

Exports a finished span. Called for spans Exporter::sample accepted. Read more

Auto Trait Implementations§

§

impl !Freeze for Stdout

§

impl !RefUnwindSafe for Stdout

§

impl !UnwindSafe for Stdout

§

impl Forget for Stdout

§

impl Move for Stdout

§

impl Send for Stdout

§

impl Sync for Stdout

§

impl Unpin for Stdout

§

impl UnsafeUnpin for Stdout

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more