trace only.Expand description
API for tracing applications and libraries.
The trace module includes types for tracking the progression of a single
request while it is handled by services that make up an application. A trace
is a tree of Spans which are objects that represent the work being done
by individual services or components involved in a request as it flows
through a system. This module implements the OpenTelemetry trace
specification.
§Getting Started
In application code:
use opentelemetry::{global, sdk::export::trace::stdout, trace::Tracer};
fn main() {
// Create a new trace pipeline that prints to stdout
let tracer = stdout::new_pipeline().install_simple();
tracer.in_span("doing_work", |cx| {
// Traced app logic here...
});
// Shutdown trace pipeline
global::shutdown_tracer_provider();
}In library code:
use opentelemetry::{global, trace::{Span, Tracer, TracerProvider}};
fn my_library_function() {
// Use the global tracer provider to get access to the user-specified
// tracer configuration
let tracer_provider = global::tracer_provider();
// Get a tracer for this library
let tracer = tracer_provider.versioned_tracer(
"my_name",
Some(env!("CARGO_PKG_VERSION")),
None
);
// Create spans
let mut span = tracer.start("doing_work");
// Do work...
// End the span
span.end();
}§Overview
The tracing API consists of a three main traits:
TracerProviders are the entry point of the API. They provide access toTracers.Tracers are types responsible for creatingSpans.Spans provide the API to trace an operation.
§Working with Async Runtimes
Exporting spans often involves sending data over a network or performing
other I/O tasks. OpenTelemetry allows you to schedule these tasks using
whichever runtime you area already using such as Tokio or async-std.
When using an async runtime it’s best to use the BatchSpanProcessor
where the spans will be sent in batches as opposed to being sent once ended,
which often ends up being more efficient.
§Managing Active Spans
Spans can be marked as “active” for a given Context, and all newly
created spans will automatically be children of the currently active span.
The active span for a given thread can be managed via get_active_span
and mark_span_as_active.
use opentelemetry::{global, trace::{self, Span, StatusCode, Tracer, TracerProvider}};
fn may_error(rand: f32) {
if rand < 0.5 {
// Get the currently active span to record additional attributes,
// status, etc.
trace::get_active_span(|span| {
span.set_status(StatusCode::Error, "value too small".into());
});
}
}
// Get a tracer
let tracer = global::tracer("my_tracer");
// Create a span
let span = tracer.start("parent_span");
// Mark the span as active
let active = trace::mark_span_as_active(span);
// Any span created here will be a child of `parent_span`...
// Drop the guard and the span will no longer be active
drop(active)Additionally Tracer::with_span and Tracer::in_span can be used as shorthand to
simplify managing the parent context.
use opentelemetry::{global, trace::Tracer};
// Get a tracer
let tracer = global::tracer("my_tracer");
// Use `in_span` to create a new span and mark it as the parent, dropping it
// at the end of the block.
tracer.in_span("parent_span", |cx| {
// spans created here will be children of `parent_span`
});
// Use `with_span` to mark a span as active for a given period.
let span = tracer.start("parent_span");
tracer.with_span(span, |cx| {
// spans created here will be children of `parent_span`
});§Async active spans
Async spans can be propagated with TraceContextExt and FutureExt.
use opentelemetry::{Context, global, trace::{FutureExt, TraceContextExt, Tracer}};
async fn some_work() { }
// Get a tracer
let tracer = global::tracer("my_tracer");
// Start a span
let span = tracer.start("my_span");
// Perform some async work with this span as the currently active parent.
some_work().with_context(Context::current_with_span(span));Modules§
- noop
- No-op trace impls
Structs§
- Event
- A
Spanhas the ability to add events. Events have a time associated with the moment when they are added to theSpan. - Link
- During the
Spancreation user MUST have the ability to record links to otherSpans. LinkedSpans can be from the same or a different trace. - Span
Builder SpanBuilderallows span attributes to be configured before the span has started.- Span
Context - Immutable portion of a
Spanwhich can be serialized and propagated. - SpanId
- An 8-byte value which identifies a given span.
- SpanRef
- A reference to the currently active span in this context.
- Trace
Flags - Flags that can be set on a
SpanContext. - TraceId
- A 16-byte value which identifies a given trace.
- Trace
State - TraceState carries system-specific configuration data, represented as a list of key-value pairs. TraceState allows multiple tracing systems to participate in the same trace.
Enums§
- Span
Kind SpanKinddescribes the relationship between the Span, its parents, and its children in aTrace.SpanKinddescribes two independent properties that benefit tracing systems during analysis.- Status
Code - The
StatusCodeinterface represents the status of a finishedSpan. It’s composed of a canonical code in conjunction with an optional descriptive message. - Trace
Error - Errors returned by the trace API.
- Trace
State Error - Error returned by
TraceStateoperations.
Traits§
- Future
Ext - Extension trait allowing futures, streams, and sinks to be traced with a span.
- IdGenerator
- Interface for generating IDs
- Span
- Interface for a single operation within a trace.
- Trace
Context Ext - Methods for storing and retrieving trace data in a context.
- Tracer
- Interface for constructing
Spans. - Tracer
Provider - Types that can create instances of
Tracer.
Functions§
- get_
active_ span - Executes a closure with a reference to this thread’s current span.
- mark_
span_ as_ active - Mark a given
Spanas active.
Type Aliases§
- Trace
Result - Describe the result of operations in tracing API.