Expand description
Span tracing: record what one unit of work (a request, a job, a query) did as attributes on a span, and export the span when it finishes.
Span::init starts a span and makes it the current span of the running
task, so code anywhere below can add attributes with Span::add without
holding it. Span::finish stamps the duration and level and hands the
span to every registered Exporter: Stdout (JSON lines), Pretty
(colourful terminal output) or Sentry (errors as Sentry issues). Spans
carry W3C trace context, so a trace continues across services
(Span::traceparent, Span::init with a traceparent header).
Where “current” lives is a Storage: Memory (one span per process),
Task (one span per task) or Auto (the task’s span inside a
task scope, the process’s otherwise), chosen once with
Span::set_storage.
PHP: ports utopia-php/span.
| PHP | Rust |
|---|---|
Span (new Span($action)) | Span (Span::new) |
Span::setStorage(), Span::setExporters() | Span::set_storage, Span::set_exporters |
Span::init(), Span::current(), Span::add(), Span::traceparent() | Span::init, Span::current, Span::add, Span::current_traceparent |
set(), get(), getAttributes(), getAction(), getTraceparent() | Span::set, Span::get, Span::attributes, Span::action, Span::traceparent |
setError(), getError(), finish() | Span::set_error, Span::error, Span::finish / Span::finish_with |
string|int|float|bool|null attribute values | Value |
\Throwable | Failure |
Level | Level |
Storage\Storage, Memory, Coroutine, Auto | Storage, Memory, Task, Auto |
Exporter\Exporter, None, Stdout, Pretty | Exporter, exporter::None, Stdout, Pretty |
Exporter\Sentry, SentryField, Sentry\Level | Sentry, exporter::SentryField, exporter::SentryLevel |
PHP: storage and exporters are static properties of Span; here they are
process-wide too. A Span is a shared handle, as a PHP object is: every
clone is the same span (Span::ptr_eq).
use utopia_span::{Memory, Span};
use std::sync::Arc;
Span::set_storage(Some(Arc::new(Memory::new())));
let span = Span::init("http.request", None);
Span::add("http.method", "GET");
Span::add("http.response.status_code", 200);
assert_eq!(span.get("http.method"), "GET".into());
assert_eq!(span.get("http.response.status_code"), 200.into());
span.finish();
assert!(Span::current().is_none());use Utopia\Span\Span;
use Utopia\Span\Storage\Memory;
Span::setStorage(new Memory());
$span = Span::init('http.request');
Span::add('http.method', 'GET');
Span::add('http.response.status_code', 200);
echo $span->get('http.method'), "\n"; // GET
echo $span->get('http.response.status_code'), "\n"; // 200
$span->finish();
var_dump(Span::current()); // NULLRe-exports§
pub use exporter::Exporter;pub use exporter::Pretty;pub use exporter::Sentry;pub use exporter::Stdout;
Modules§
- exporter
- Exporters: where finished spans go.
Structs§
- Auto
- The task’s span inside a
Task::scope, the process’s span outside. - Failure
- An error recorded on a span: what exporters report about it.
- Frame
- One call in a
Failure’s stack trace, innermost first. - Memory
- One current span for the whole process.
- Span
- One unit of work: an action, attributes, and the error it failed with, if any.
- Task
- One current span per task.
Enums§
- Error
- Why an exporter could not be created.
- Level
- The severity of a span, from least to most severe.
- Value
- The value of a span attribute: a scalar or null.
Traits§
- Storage
- Keeps the current span of an execution context, so
Span::addandSpan::currentwork without passing spans around.