How does context propagation work in-process and across processes?
AdvancedConceptTracing & OpenTelemetry
Answer
- In-process: the SDK stores the active span in a context object (thread-local in Java,
contextvarsin Python,context.Contextpassed explicitly in Go, AsyncLocalStorage in Node.js). New spans pick the active span as their parent. - Across processes: a propagator injects the context into a carrier (HTTP headers, gRPC metadata, Kafka record headers) on the client side and extracts it on the server side.
ctx, span := tracer.Start(r.Context(), "charge-card")
defer span.End()
req, _ := http.NewRequestWithContext(ctx, "POST", url, body)
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header))
Typical breaks: thread pools or goroutines that do not carry the context, async callbacks, and message queues where nobody injects headers. Auto-instrumentation handles common frameworks; custom executors need manual wrapping.
More on Tracing & OpenTelemetry
What is distributed tracing, and what are a trace, a span and a span context?BeginnerWhat fields does an OpenTelemetry span carry?BeginnerWhat are span kinds and why do they matter?IntermediateWhat does the W3C `traceparent` header look like?IntermediateWhat is baggage, and how is it different from span attributes?IntermediateWhat is OpenTelemetry and what are its main parts?Beginner