Rust

exporter

Module

Exporters: 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;
Rust
pub struct None

Discards every span: Exporter::sample is always false.

Rust
use utopia_span::exporter::{Exporter, None};
use utopia_span::Span;

let span = Span::new("test");
assert!(!None.sample(&span));
None.export(&span); // does nothing
use utopia_span::Exporter;
Rust
pub trait Exporter: Send + Sync

Receives finished spans.

Implement it to send spans to an observability backend, then register it with Span::set_exporters.

Rust
pub type Sampler = Box<dyn Fn(&Span) -> bool + Send + Sync>

Decides whether an exporter exports a span.

exporter::pretty

Own page

Spans as colourful, human-readable text.

use utopia_span::Pretty;
Rust
pub struct Pretty

Writes 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.

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

exporter::sentry

Own page

Errors as Sentry issues.

use utopia_span::exporter::sentry::Request;
Rust
pub struct Request

An HTTP request to Sentry's envelope endpoint.

use utopia_span::Sentry;
Rust
pub struct Sentry

Reports 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.

Rust
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""#));
Also implements Debug.
use utopia_span::exporter::sentry::SentryField;
Rust
pub enum SentryField

Where a span attribute goes in a Sentry event.

use utopia_span::exporter::sentry::SentryLevel;
Rust
pub enum SentryLevel

The levels of Sentry's event API.

use utopia_span::exporter::sentry::Transport;
Rust
pub trait Transport: Send + Sync

Sends envelopes to Sentry.

A transport reports its own failures: tracing never fails the application.

Implemented byF

exporter::stdout

Own page

Spans as JSON lines.

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.