Overview
Context propagation is fundamental to distributed tracing. It allows OpenTelemetry to:- Link related operations - Connect spans within a single process
- Trace across services - Maintain trace continuity as requests flow between services
- Share metadata - Propagate baggage and other contextual information
- In-process propagation - Using the
ContextAPI - Cross-service propagation - Using propagators to inject/extract context from message headers
In-Process Context Propagation
TheContext type is an immutable, execution-scoped collection of values that flows through your application.
Creating and Using Context
Contexts are immutable. Operations like
with_value() return a new context containing both the original and new values.Attaching Context to the Current Thread
Useattach() to make a context active for the current thread:
Nested Contexts
Contexts can be nested, creating a stack of active contexts:Context with Spans
Spans are automatically stored in the context:Async Context Propagation
For async code, useFutureExt to propagate context across .await points:
Cross-Service Propagation
When a request crosses service boundaries (e.g., HTTP, gRPC), context must be serialized into message headers and deserialized on the receiving side.Propagators
Propagators handle serialization and deserialization of context. OpenTelemetry provides several standard propagators:- TraceContext - W3C Trace Context standard (recommended)
- Baggage - W3C Baggage standard
- Jaeger - Jaeger-specific format
- B3 - Zipkin B3 format
Setting Up Propagation
Injecting Context (Client Side)
When making an outbound request, inject the current context into headers:Extracting Context (Server Side)
When receiving a request, extract the context from headers:Complete HTTP Example
Here’s a complete example of context propagation between HTTP client and server: Client:Baggage
Baggage allows you to propagate arbitrary key-value pairs across service boundaries. Unlike span attributes, baggage is propagated to all downstream services.Using Baggage
Propagating Baggage Across Services
To propagate baggage, use a composite propagator:Baggage in Spans and Logs
Baggage is not automatically added to spans or logs. You need custom processors:Custom Propagators
You can implement custom propagators for proprietary formats:Telemetry Suppression
OpenTelemetry components can suppress telemetry generation to prevent infinite loops:Best Practices
Next Steps
Signals
Learn about traces, metrics, and logs
Exporters
Configure where to send your telemetry data