Understanding Traces
A trace represents a single request or operation flow through your application. It consists of:- Trace ID: Unique identifier for the entire trace
- Root Span: The top-level operation (transaction)
- Child Spans: Nested operations within the trace
Starting Spans
Automatic Span Finishing
The recommended way to create spans - they finish automatically:Manual Span Control
For cases where you need explicit control:Inactive Spans
Create spans that don’t automatically become the active span:Inactive spans don’t become parent spans for automatic instrumentation. Use them for background tasks or parallel operations.
Working with Active Spans
Setting Active Spans
Control which span is active in a given scope:No Parent Span
Start a span tree without a parent:Trace Context
Access trace information from scopes:Continuing Traces
Continue a trace from incoming headers (useful for distributed tracing):Starting New Traces
Explicitly start a new trace, disconnecting from any parent trace:Span Options
Basic Options
Parent Span Control
Explicitly set the parent span:Conditional Spans
Only create spans if a parent exists:Force Transaction
Force a span to be a transaction (root span):Custom Scope
Provide a specific scope for the span:Span Attributes
Add structured data to spans:Semantic Attributes
Use semantic attributes for standard data:Updating Span Names
Change a span’s name dynamically:Span Status
Set the status to indicate success or failure:Span status is automatically set to
error if an exception occurs within the span callback.Span Events
Add timestamped events to spans:Practical Examples
API Request Handler
Background Job Processing
Best Practices
- Use descriptive names: Span names should clearly indicate what operation they represent
- Set appropriate operations: Use standard operation types for consistency
- Add relevant attributes: Include data that helps identify and debug slow operations
- Keep span hierarchies shallow: Deeply nested spans can be hard to analyze
- Use semantic attributes: Follow OpenTelemetry conventions for standard data
- Finish spans promptly: Don’t leave spans open longer than necessary
Next Steps
Spans
Learn more about individual spans
Distributed Tracing
Track requests across services
Performance
Overview of performance monitoring
Profiling
CPU profiling for performance bottlenecks