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
TheSpan trait is defined in opentelemetry/src/trace/span.rs:50:
Span Lifecycle
Creating Spans
Spans are created through aTracer:
Automatic Span Management
Thein_span method handles the lifecycle automatically:
Recording State
Check if a span is recording before adding expensive data: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. Fromopentelemetry-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
Therecord_error method is a convenience for recording exceptions:
opentelemetry/src/trace/span.rs:72-77:
Span Status
The status indicates whether the operation succeeded or failed.Status Values
Fromopentelemetry/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
TheSpanContext contains immutable information about a span that can be propagated.
SpanContext Structure
Fromopentelemetry/src/trace/span_context.rs:294-300:
Accessing SpanContext
Span Links
Links connect spans across different traces or within the same trace in a non-parent-child relationship.Adding Links
Links at Span Creation
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. Fromopentelemetry/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
Use descriptive span names
Use descriptive span names
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.Check is_recording for expensive operations
Check is_recording for expensive operations
Before computing expensive attribute values, check if the span is recording to avoid unnecessary work.
Set status on errors
Set status on errors
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.Use events for significant moments
Use events for significant moments
Record events for important moments in the span’s lifecycle, like cache hits/misses, retries, or state changes.
Add links at span creation
Add links at span creation
Links added after span creation are not available to samplers. Include links in the SpanBuilder when possible.
Next Steps
Context
Learn about context propagation
Span Processors
Configure how spans are processed and exported