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
impl Stdout
Sourcepub fn new() -> Self
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().
Sourcepub fn sampler(
self,
sampler: impl Fn(&Span) -> bool + Send + Sync + 'static,
) -> Self
pub fn sampler( self, sampler: impl Fn(&Span) -> bool + Send + Sync + 'static, ) -> Self
Exports only the spans sampler accepts.
PHP: the $sampler argument.
Sourcepub fn max_trace_frames(self, frames: usize) -> Self
pub fn max_trace_frames(self, frames: usize) -> Self
Prints at most frames stack frames of an error.
PHP: the $maxTraceFrames argument.