Overview
This guide covers real-world integration patterns for distributed tracing, including HTTP context propagation, gRPC instrumentation, and baggage handling.HTTP Context Propagation
Context propagation ensures that trace context is passed between services, maintaining the parent-child relationship across network boundaries.Client-Side: Injecting Context
1
Set Up Propagators
Configure the propagators that will serialize trace context into HTTP headers.
2
Inject Context into Request Headers
Before sending an HTTP request, inject the current trace context into headers.
Server-Side: Extracting Context
1
Extract Context from Incoming Headers
On the server side, extract trace context from incoming request headers.
2
Create Server Span with Parent Context
Use the extracted context to create a server span that continues the trace.
Baggage Propagation
Baggage allows you to propagate key-value pairs across service boundaries, useful for cross-cutting concerns like user IDs or request IDs.Setting Up Composite Propagator
Enriching Spans with Baggage
Create a custom span processor to automatically add baggage attributes to spans:Sending Baggage in HTTP Requests
gRPC Distributed Tracing
Instrument gRPC services with OpenTelemetry for complete distributed tracing.gRPC Server Implementation
1
Define gRPC Service
Create a proto file for your service.
proto/helloworld.proto
2
Create Metadata Extractor
Implement an extractor for gRPC metadata to extract trace context.
3
Implement Service with Tracing
Extract context and create server spans in your gRPC handlers.
gRPC Client Implementation
1
Create Metadata Injector
Implement an injector to add trace context to gRPC metadata.
2
Make Traced gRPC Calls
Create a client span and inject context into the request metadata.
Complete HTTP Propagation Example
Here’s a complete working example of an HTTP server and client with context propagation:Dependencies for Full Examples
Cargo.toml
Always propagate context across async boundaries using
with_context() from the FutureExt trait to maintain proper parent-child relationships in your traces.