Rust

Crate root

Module
What utopia_span defines at its root.

Modules

Contents

use utopia_span::Auto;
Rust
pub struct Auto {
    // private fields
}

The task's span inside a Task::scope, the process's span outside.

PHP: Utopia\Span\Storage\Auto, which uses the coroutine storage inside a Swoole coroutine and the memory storage elsewhere.

Rust
use utopia_span::{Auto, Span, Storage};

let storage = Auto::new();
let span = Span::new("cli");
storage.set(Some(span.clone()));
assert_eq!(storage.get(), Some(span));
PHP
use Utopia\Span\Span;
use Utopia\Span\Storage\Auto;

$storage = new Auto();
$span = new Span('cli');
$storage->set($span);
var_dump($storage->get() === $span); // bool(true)
use utopia_span::Failure;
Rust
pub struct Failure {
    // private fields
}

An error recorded on a span: what exporters report about it.

It carries what exporters print: the error's type (class), message, code, where it was raised, its stack trace and the error that caused it. Failure::new records the caller's location; Failure::from_error converts any std::error::Error with its chain of sources.

PHP: the \Throwable passed to setError() or finish(); class is $e::class, previous is getPrevious().

Rust
use utopia_span::Failure;

let root = Failure::new("LogicException", "root cause");
let error = Failure::new("RuntimeException", "outer").code(42).previous(root);

assert_eq!(error.class(), "RuntimeException");
assert_eq!(error.code_value(), 42);
assert_eq!(error.cause().map(|c| c.message()), Some("root cause"));
PHP
$root = new \LogicException('root cause');
$error = new \RuntimeException('outer', 42, $root);

echo $error::class, "\n";                    // RuntimeException
echo $error->getCode(), "\n";                // 42
echo $error->getPrevious()->getMessage(), "\n"; // root cause
Also implements Display, Error.
use utopia_span::Frame;
Rust
pub struct Frame {
    pub file: Option<String>,
    pub line: Option<u32>,
    pub function: String,
    pub class: Option<String>,
    pub call: Option<String>,
}

One call in a Failure's stack trace, innermost first.

PHP: an entry of $throwable->getTrace().

use utopia_span::Memory;
Rust
pub struct Memory {
    // private fields
}

One current span for the whole process.

PHP: Utopia\Span\Storage\Memory, for FPM and CLI processes that handle one request at a time.

Rust
use utopia_span::{Memory, Span, Storage};

let storage = Memory::new();
assert!(storage.get().is_none());

let span = Span::new("job");
storage.set(Some(span.clone()));
assert_eq!(storage.get(), Some(span));
storage.set(None);
assert!(storage.get().is_none());
PHP
use Utopia\Span\Span;
use Utopia\Span\Storage\Memory;

$storage = new Memory();
var_dump($storage->get()); // NULL

$span = new Span('job');
$storage->set($span);
var_dump($storage->get() === $span); // bool(true)
$storage->set(null);
var_dump($storage->get()); // NULL
use utopia_span::Span;
Rust
pub struct Span(_);

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.

Rust
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));
PHP
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
Also implements Debug, PartialEq, Default.
use utopia_span::Task;
Rust
pub struct Task;

One current span per task.

A task gets its own slot by running inside Task::scope; outside a scope there is no current span and Storage::set does nothing.

PHP: Utopia\Span\Storage\Coroutine, which keeps the span in the Swoole coroutine's context (Coroutine::getContext()); a scope is a coroutine.

Rust
use utopia_span::{Span, Storage, Task};

let storage = Task::new();
storage.set(Some(Span::new("ignored")));
assert!(storage.get().is_none()); // outside a scope

Task::scope(async {
    let storage = Task::new();
    let span = Span::new("request");
    storage.set(Some(span.clone()));
    assert_eq!(storage.get(), Some(span));
})
.await;
PHP
use Utopia\Span\Span;
use Utopia\Span\Storage\Coroutine;

$storage = new Coroutine();
$storage->set(new Span('ignored'));
var_dump($storage->get()); // NULL: outside a coroutine

\Swoole\Coroutine\run(function () {
    $storage = new Coroutine();
    $span = new Span('request');
    $storage->set($span);
    var_dump($storage->get() === $span); // bool(true)
});
use utopia_span::Error;
Rust
pub enum Error {
    InvalidArgument(&'static str),
}

Why an exporter could not be created.

use utopia_span::Level;
Rust
pub enum Level {
    Debug,
    Info,
    Warn,
    Error,
    Fatal,
}

The severity of a span, from least to most severe.

PHP: Utopia\Span\Level. Names follow Grafana Loki's detected_level vocabulary (warn, not warning).

Also implements Display.
use utopia_span::Value;
Rust
pub enum Value {
    Null,
    Bool(bool),
    Int(i64),
    Float(f64),
    Str(Str),
}

The value of a span attribute: a scalar or null.

Strings are bytes, as attribute values copied from requests can be any bytes.

PHP: string|int|float|bool|null.

Rust
use utopia_span::Value;

assert_eq!(Value::from("GET").as_str(), Some("GET"));
assert_eq!(Value::from(200), Value::Int(200));
assert_eq!(Value::from(None::<i64>), Value::Null);
Also implements From<bool>, From<i8>, From<i16>, From<i32>, From<i64>, From<u8>, From<u16>, From<u32>, From<f64>, From<f32>, From<&str>, From<String>, From<&[u8]>, From<Vec<u8>>, From<Str>, From<Option<T>>.
use utopia_span::Storage;
Rust
pub trait Storage: Send + Sync

Keeps the current span of an execution context, so Span::add and Span::current work without passing spans around.

Implement it to keep spans elsewhere (a thread-local, a request extension); register it with Span::set_storage.

PHP: Utopia\Span\Storage\Storage.

Implemented byAutoMemoryTask