exporter
ModuleExporters: where finished spans go.
Span::finish hands the span to every registered
Exporter whose Exporter::sample accepts it. None discards
spans, Stdout writes JSON lines, Pretty writes colourful
multi-line output for a terminal, and Sentry reports errors to Sentry.
Contents
3 submodules, and what the module defines itself.
use utopia_span::exporter::None;pub struct NoneDiscards every span: Exporter::sample is always false.
use utopia_span::exporter::{Exporter, None};
use utopia_span::Span;
let span = Span::new("test");
assert!(!None.sample(&span));
None.export(&span); // does nothinguse utopia_span::Exporter;pub trait Exporter: Send + SyncReceives finished spans.
Implement it to send spans to an observability backend, then register it
with Span::set_exporters.
pub type Sampler = Box<dyn Fn(&Span) -> bool + Send + Sync>Decides whether an exporter exports a span.
exporter::pretty
Own pageSpans as colourful, human-readable text.
use utopia_span::Pretty;pub struct PrettyWrites each span as a coloured block for local development: a header
(action, duration, short trace id), the attributes other than span.*
aligned in a column with level first, the error with at most
max_trace_frames stack frames, and a separator line width wide. Spans
with an error go to standard error, the others to standard output.
use utopia_span::{Pretty, Span};
let span = Span::new("db.query");
span.set("span.duration", 0.0123).set("rows", 3);
let text = String::from_utf8(Pretty::new().width(10).render(&span)).unwrap();
assert!(text.contains("12.3ms"));
assert!(text.contains("rows"));Default, Debug.exporter::sentry
Own pageErrors as Sentry issues.
use utopia_span::exporter::sentry::Request;pub struct RequestAn HTTP request to Sentry's envelope endpoint.
use utopia_span::Sentry;pub struct SentryReports spans at warning level or above that carry an error as Sentry issues.
Each event carries the error and its chain of causes (at most 10, root
cause first), the trace context, http.url, http.method and
http.query as the request, http.response.status_code as the response,
and every other attribute (except span.* and level) as a context, tag
or extra, as the classifier decides. A boolean span.handled attribute
sets whether the error was handled; otherwise fatal spans are unhandled.
use std::sync::{Arc, Mutex};
use utopia_span::exporter::Request;
use utopia_span::{Exporter, Failure, Level, Sentry, Span};
let sent = Arc::new(Mutex::new(Vec::<Request>::new()));
let log = sent.clone();
let sentry = Sentry::new("https://publickey@sentry.example.com:9000/123")?
.transport(move |request: Request| log.lock().unwrap().push(request));
let span = Span::new("test");
span.finish_with(Some(Level::Error), Some(Failure::new("RuntimeException", "Test error")));
assert!(sentry.sample(&span));
sentry.export(&span);
let request = &sent.lock().unwrap()[0];
assert_eq!(request.url, "https://sentry.example.com:9000/api/123/envelope/");
assert!(String::from_utf8_lossy(&request.body).contains(r#""value":"Test error""#));Debug.use utopia_span::exporter::sentry::SentryField;pub enum SentryFieldWhere a span attribute goes in a Sentry event.
use utopia_span::exporter::sentry::SentryLevel;pub enum SentryLevelThe levels of Sentry's event API.
use utopia_span::exporter::sentry::Transport;pub trait Transport: Send + SyncSends envelopes to Sentry.
A transport reports its own failures: tracing never fails the application.
exporter::stdout
Own pageSpans as JSON lines.
use utopia_span::Stdout;pub struct StdoutWrites 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.
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");Default, Debug.