Rust

stdout

Module

Spans as JSON lines.

Contents

use utopia_span::Stdout;
Rust
pub struct Stdout

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.

A span whose JSON cannot be encoded (a string attribute that is not UTF-8) is dropped.

Rust
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");
Also implements Default, Debug.