Skip to main content
A Span represents a single operation within a trace. Spans can be nested to form a trace tree, where each trace contains a root span and zero or more child spans representing sub-operations.

Span Interface

The Span trait is defined in opentelemetry/src/trace/span.rs:50:

Span Lifecycle

Creating Spans

Spans are created through a Tracer:

Automatic Span Management

The in_span method handles the lifecycle automatically:

Recording State

Check if a span is recording before adding expensive data:
From opentelemetry/src/trace/span.rs:98-108:
This flag may be true despite the entire trace being sampled out. This allows recording and processing of information about individual spans without sending it to the backend.

Span Attributes

Attributes are key-value pairs that provide additional context about the operation.

Setting Attributes

Attributes at Span Creation

Set attributes when creating the span:

Attribute Limits

Spans have configurable limits on the number of attributes. From opentelemetry-sdk/src/trace/span.rs:139-147:
Attributes beyond the limit are dropped, and the drop count is tracked. Configure limits via SpanLimits.

Span Events

Events are timestamped annotations that record something happening during the span’s lifetime.

Adding Events

Events with Custom Timestamps

Recording Errors

The record_error method is a convenience for recording exceptions:
From opentelemetry/src/trace/span.rs:72-77:
record_error only adds an event. To mark the span as failed, you must also call set_status.

Span Status

The status indicates whether the operation succeeded or failed.

Status Values

From opentelemetry/src/trace/span.rs:281-296:

Setting Status

Status Precedence

Statuses form a total order: Ok > Error > Unset
Instrumentation libraries should generally not set Status::Ok. Only application code should mark operations as explicitly successful.

Span Context

The SpanContext contains immutable information about a span that can be propagated.

SpanContext Structure

From opentelemetry/src/trace/span_context.rs:294-300:

Accessing SpanContext

Links connect spans across different traces or within the same trace in a non-parent-child relationship.
Links are typically added when creating the span:
Links added via add_link after span creation are not accessible to samplers. Add links at creation time when possible.

Span Kind

SpanKind describes the relationship between spans in a trace. From opentelemetry/src/trace/span.rs:226-255:

Setting Span Kind

Updating Span Name

Span names can be updated after creation:
Sampling decisions based on the name depend on implementation and may not be re-evaluated after name changes.

Complete Example

Here’s a realistic example combining multiple span features:

Best Practices

Choose span names that represent a general operation, not specific instances. Use get_user instead of get_user_12345. Put the specific ID in an attribute.
Before computing expensive attribute values, check if the span is recording to avoid unnecessary work.
Always set span status to error when an operation fails. Use record_error for the exception details and set_status to mark the span as failed.
Record events for important moments in the span’s lifecycle, like cache hits/misses, retries, or state changes.

Next Steps

Context

Learn about context propagation

Span Processors

Configure how spans are processed and exported