pub struct Span(/* private fields */);Expand description
One unit of work: an action, attributes, and the error it failed with, if any.
A new span has a random 128-bit trace id (span.trace_id), a random
64-bit span id (span.id), both in lowercase hex, and its start time in
seconds since the epoch (span.started_at). A Span is a handle: clones
share one span.
PHP: Utopia\Span\Span.
use utopia_span::{Span, Value};
let span = Span::new("db.query");
span.set("db.table", "users").set("db.rows", 3);
assert_eq!(span.action(), "db.query");
assert_eq!(span.get("db.rows"), Value::Int(3));
assert_eq!(span.get("missing"), Value::Null);
assert_eq!(span.get("span.trace_id").as_str().map(str::len), Some(32));use Utopia\Span\Span;
$span = new Span('db.query');
$span->set('db.table', 'users')->set('db.rows', 3);
echo $span->getAction(), "\n"; // db.query
echo $span->get('db.rows'), "\n"; // 3
var_dump($span->get('missing')); // NULL
echo strlen($span->get('span.trace_id')), "\n"; // 32Implementations§
Source§impl Span
impl Span
Sourcepub fn new(action: impl Into<Box<str>>) -> Span
pub fn new(action: impl Into<Box<str>>) -> Span
A new span of action, with new trace and span ids, started now. It
is not the current span: Span::init makes one current.
PHP: new Span($action).
Sourcepub fn set_storage(storage: Option<Arc<dyn Storage>>)
pub fn set_storage(storage: Option<Arc<dyn Storage>>)
Sets (or clears, with None) where the current span is kept. Call it
once at startup, before creating spans.
PHP: Span::setStorage($storage).
Sourcepub fn set_exporters(exporters: Vec<Arc<dyn Exporter>>)
pub fn set_exporters(exporters: Vec<Arc<dyn Exporter>>)
Replaces every exporter. Finished spans go to each exporter whose
Exporter::sample accepts them.
PHP: Span::setExporters(...$exporters).
Sourcepub fn init(action: impl Into<Box<str>>, traceparent: Option<&str>) -> Span
pub fn init(action: impl Into<Box<str>>, traceparent: Option<&str>) -> Span
Starts a span of action and makes it the current span.
A valid W3C traceparent header (00-<32 hex>-<16 hex>-<2 hex>)
continues its trace: the span takes its trace id, and its parent id
becomes span.parent_id. Anything else is ignored and the span starts
a new trace.
PHP: Span::init($action, $traceparent).
use utopia_span::{Span, Value};
let span = Span::init("http.request", Some("00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01"));
assert_eq!(span.get("span.trace_id"), Value::from("0af7651916cd43dd8448eb211c80319c"));
assert_eq!(span.get("span.parent_id"), Value::from("b7ad6b7169203331"));use Utopia\Span\Span;
$span = Span::init('http.request', '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01');
echo $span->get('span.trace_id'), "\n"; // 0af7651916cd43dd8448eb211c80319c
echo $span->get('span.parent_id'), "\n"; // b7ad6b7169203331Sourcepub fn add(key: &str, value: impl Into<Value>)
pub fn add(key: &str, value: impl Into<Value>)
Sets an attribute on the current span; does nothing when no span is active.
PHP: Span::add($key, $value).
use std::sync::Arc;
use utopia_span::{Memory, Span, Value};
Span::set_storage(Some(Arc::new(Memory::new())));
Span::add("ignored", 1); // no current span yet
let span = Span::init("job", None);
Span::add("job.id", "a1");
assert_eq!(span.get("job.id"), Value::from("a1"));use Utopia\Span\Span;
use Utopia\Span\Storage\Memory;
Span::setStorage(new Memory());
Span::add('ignored', 1); // no current span yet
$span = Span::init('job');
Span::add('job.id', 'a1');
echo $span->get('job.id'), "\n"; // a1Sourcepub fn current_traceparent() -> Option<String>
pub fn current_traceparent() -> Option<String>
The traceparent header of the current span, to propagate the trace
to the services this one calls.
PHP: Span::traceparent().
Sourcepub fn set(&self, key: &str, value: impl Into<Value>) -> &Self
pub fn set(&self, key: &str, value: impl Into<Value>) -> &Self
Sets an attribute. Setting a key again replaces its value in place;
the built-in span.* attributes can be replaced too.
PHP: $span->set($key, $value).
Sourcepub fn set_error(&self, error: impl Into<Arc<Failure>>) -> &Self
pub fn set_error(&self, error: impl Into<Arc<Failure>>) -> &Self
Records the error this span failed with. Exporters read it with
Span::error.
PHP: $span->setError($throwable).
Sourcepub fn error(&self) -> Option<Arc<Failure>>
pub fn error(&self) -> Option<Arc<Failure>>
The recorded error, if any.
PHP: $span->getError().
Sourcepub fn action(&self) -> &str
pub fn action(&self) -> &str
What the span represents (http.request, db.query).
PHP: $span->getAction().
Sourcepub fn get(&self, key: &str) -> Value
pub fn get(&self, key: &str) -> Value
An attribute’s value; Value::Null when it is not set.
PHP: $span->get($key).
Sourcepub fn traceparent(&self) -> String
pub fn traceparent(&self) -> String
The span’s W3C traceparent header: 00-{trace id}-{span id}-01.
PHP: $span->getTraceparent().
use utopia_span::Span;
let span = Span::new("call");
span.set("span.trace_id", "0af7651916cd43dd8448eb211c80319c").set("span.id", "b7ad6b7169203331");
assert_eq!(span.traceparent(), "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01");use Utopia\Span\Span;
$span = new Span('call');
$span->set('span.trace_id', '0af7651916cd43dd8448eb211c80319c')->set('span.id', 'b7ad6b7169203331');
echo $span->getTraceparent(), "\n"; // 00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01Sourcepub fn attributes(&self) -> Vec<(Box<str>, Value)>
pub fn attributes(&self) -> Vec<(Box<str>, Value)>
Every attribute, in the order they were first set.
PHP: $span->getAttributes().
Sourcepub fn with_attributes<R>(&self, f: impl FnOnce(&[(Box<str>, Value)]) -> R) -> R
pub fn with_attributes<R>(&self, f: impl FnOnce(&[(Box<str>, Value)]) -> R) -> R
Calls f with every attribute, in order, without copying them.
Sourcepub fn finish(&self)
pub fn finish(&self)
Finishes the span: Span::finish_with without a level or error.
PHP: $span->finish().
Sourcepub fn finish_with(&self, level: Option<Level>, error: Option<Failure>)
pub fn finish_with(&self, level: Option<Level>, error: Option<Failure>)
Finishes the span and exports it.
Records error (if given), sets span.finished_at and
span.duration (seconds), and sets level to level, or else to
Level::Error when the span has an error and Level::Info
otherwise, replacing any level set before. Then every exporter whose
sampler accepts the span exports it, and the current span is cleared.
PHP: $span->finish($level, $error).
use utopia_span::{Failure, Level, Span, Value};
let span = Span::new("job");
span.finish_with(Some(Level::Warn), Some(Failure::new("RuntimeException", "slow")));
assert_eq!(span.get("level"), Value::from("warn"));
assert!(span.get("span.duration").as_float().is_some());use Utopia\Span\Level;
use Utopia\Span\Span;
$span = new Span('job');
$span->finish(level: Level::Warn, error: new \RuntimeException('slow'));
echo $span->get('level'), "\n"; // warn
var_dump(is_float($span->get('span.duration'))); // bool(true)