utopia-span 0.1.0

Span tracing: attributes on the current span of each task, W3C trace context, and exporters to stdout, a terminal and Sentry.
Documentation

Utopia Span

[!IMPORTANT] This repository is a read-only mirror of crates/utopia/span in appwrite/appwrite. Development happens there — please open issues and pull requests against appwrite/appwrite.

Discord

Span tracing: attributes on the current span of each task, W3C trace context, and exporters to stdout, a terminal and Sentry.

This library is maintained by the Appwrite team.

Getting started

[dependencies]
utopia-span = { git = "https://github.com/appwrite/appwrite" }

A span records one unit of work, such as a request, a job or a query: what it was, the attributes that describe it, how long it took and whether it failed. Start a span when the work starts, add attributes from anywhere while it runs, and finish it to send it to your exporters.

Trace a request

Choose where the current span lives once at startup, start a span per request, and add attributes without passing the span around.

use std::sync::Arc;
use utopia_span::{Memory, Span, Value};

Span::set_storage(Some(Arc::new(Memory::new())));

let span = Span::init("http.request", None);
Span::add("http.method", "POST");
Span::add("http.path", "/v1/users");
Span::add("http.response.status_code", 201);

assert_eq!(span.get("http.response.status_code"), Value::Int(201));
span.finish();
assert_eq!(span.get("level"), Value::from("info"));

Keep one span per task

In an async server, each request runs in its own task. The Task storage keeps one current span per task scope, and Auto falls back to the process's span outside one.

use std::sync::Arc;
use utopia_span::{Auto, Span, Task, Value};

Span::set_storage(Some(Arc::new(Auto::new())));

let first = Task::scope(async {
    let span = Span::init("job", None);
    Span::add("job.id", "a");
    span
});
let second = Task::scope(async {
    let span = Span::init("job", None);
    Span::add("job.id", "b");
    span
});
let (first, second) = (first.await, second.await);
assert_eq!(first.get("job.id"), Value::from("a"));
assert_eq!(second.get("job.id"), Value::from("b"));

Continue a trace across services

Pass the current span's traceparent header to the services you call; they start their spans from it, so all spans share one trace id.

use utopia_span::Span;

let incoming = Span::new("gateway");
let header = incoming.traceparent();

let downstream = Span::init("users.get", Some(&header));
assert_eq!(downstream.get("span.trace_id"), incoming.get("span.trace_id"));
assert_eq!(downstream.get("span.parent_id"), incoming.get("span.id"));

Export spans

Exporters receive finished spans. Stdout writes JSON lines for log collectors, Pretty writes readable output for local development, and Sentry reports failed spans. Each takes a sampler to choose which spans it exports.

use std::sync::Arc;
use utopia_span::{Exporter, Failure, Span, Stdout};

let errors_only = Stdout::new().sampler(|span: &Span| span.error().is_some());

let ok = Span::new("job");
let failed = Span::new("job");
failed.set_error(Failure::new("RuntimeException", "timeout"));

assert!(!errors_only.sample(&ok));
assert!(errors_only.sample(&failed));

Span::set_exporters(vec![Arc::new(errors_only)]);

Report errors to Sentry

The Sentry exporter sends spans at warning level or above that carry an error. Attributes become contexts by default; a classifier turns low-cardinality ones into searchable tags.

use utopia_span::exporter::SentryField;
use utopia_span::{Exporter, Failure, Level, Sentry, Span};

let sentry = Sentry::new("https://publickey@o0.ingest.sentry.io/42")?
    .environment("production")
    .classifier(|key| if key.starts_with("tenant.") { SentryField::Tag } else { SentryField::Context });

let span = Span::new("api.request");
span.set("tenant.id", "acme");
span.finish_with(Some(Level::Error), Some(Failure::new("RuntimeException", "database unavailable")));

assert!(sentry.sample(&span));

Contributing

Development happens in appwrite/appwrite, where this crate lives at crates/utopia/span. Run these commands from a checkout of it. The contributing guide covers each step.

Test

cargo test -p utopia-span
cargo clippy -p utopia-span --all-targets -- -D warnings
cargo fmt --all --check

Compare with PHP

Replay the results PHP recorded for every case against Rust, without PHP:

COMPAT_LIB=span cargo test -p compat

Run every case on both runtimes, fuzz both with generated inputs, then run the whole conversion check. These need the dev stack (docker compose up -d):

bin/compat run span
bin/compat fuzz span
bin/compat check span

Benchmark

bin/compat bench span

This times every benchmark on PHP and on a release build of Rust, both in containers on the dev stack. It fails if Rust is slower. Add --write to record the run in tests/compat/span/bench.json. Run it on a quiet machine, one library at a time.

License

BSD-3-Clause. See LICENSE.