Skip to main content

Span

Struct Span 

Source
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";  // 32

Implementations§

Source§

impl Span

Source

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

Source

pub fn ptr_eq(a: &Span, b: &Span) -> bool

Whether a and b are the same span.

PHP: $a === $b.

Source

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

Source

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

Source

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"; // b7ad6b7169203331
Source

pub fn current() -> Option<Span>

The current span, if one is active.

PHP: Span::current().

Source

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"; // a1
Source

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().

Source

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

Source

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

Source

pub fn error(&self) -> Option<Arc<Failure>>

The recorded error, if any.

PHP: $span->getError().

Source

pub fn action(&self) -> &str

What the span represents (http.request, db.query).

PHP: $span->getAction().

Source

pub fn get(&self, key: &str) -> Value

An attribute’s value; Value::Null when it is not set.

PHP: $span->get($key).

Source

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-01
Source

pub fn attributes(&self) -> Vec<(Box<str>, Value)>

Every attribute, in the order they were first set.

PHP: $span->getAttributes().

Source

pub fn with_attributes<R>(&self, f: impl FnOnce(&[(Box<str>, Value)]) -> R) -> R

Calls f with every attribute, in order, without copying them.

Source

pub fn finish(&self)

Finishes the span: Span::finish_with without a level or error.

PHP: $span->finish().

Source

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)

Trait Implementations§

Source§

impl Clone for Span

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Span

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Span

Source§

fn default() -> Self

A span of the action unknown.

Source§

impl PartialEq for Span

Source§

fn eq(&self, other: &Self) -> bool

Whether both handles are the same span (PHP’s ===).

1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl Forget for Span

§

impl Freeze for Span

§

impl Move for Span

§

impl RefUnwindSafe for Span

§

impl Send for Span

§

impl Sync for Span

§

impl Unpin for Span

§

impl UnsafeUnpin for Span

§

impl UnwindSafe for Span

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more