Skip to main content
The trace module provides 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.

Core Concepts

OpenTelemetry tracing consists of three main traits:
  • TracerProvider - The entry point of the API that provides access to Tracers
  • Tracer - Responsible for creating Spans
  • Span - Represents a single operation within a trace

Getting Started

Here’s a basic example of instrumenting an application:

Trace Structure

A trace is a tree of spans. Each trace contains:
  • A root span - The top-level span with no parent
  • Zero or more child spans - Spans that have a parent span
Spans can be nested to form a trace tree:

Synchronous vs Asynchronous Code

Synchronous Code

In synchronous code, use in_span for automatic span management:

Asynchronous Code

For async code, use the FutureExt trait to attach context to futures:
Do not use mark_span_as_active directly in async blocks, as the guard will remain active for the entire lifetime of the future, not just when it’s being polled.

Working with the Global TracerProvider

OpenTelemetry provides a global TracerProvider singleton:

Complete Example with OTLP

Here’s a complete example using the OTLP exporter:

Next Steps

Tracers

Learn how to create and configure tracers

Spans

Understand span lifecycle, attributes, and events

Context Propagation

Propagate trace context across service boundaries

Sampling

Control which traces are recorded and exported