The `opentelemetry-propagator-b3` library provides B3 context propagation for OpenTelemetry Python, enabling distributed tracing across systems that use the B3 header format (e.g., Zipkin). It is part of the broader OpenTelemetry Python project, which follows a rapid release cycle, with new versions typically released monthly.
pip install opentelemetry-propagator-b3 opentelemetry-sdkVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set the B3 propagator globally and use it to inject trace context into a carrier (like HTTP headers) and then extract it. This is crucial for distributed tracing where context needs to be passed between services. The `opentelemetry-sdk` is used here for a runnable example, but in a real application, you would configure exporters and resource detectors appropriately.
For multi-header injection, you might need to use `B3MultiFormat` directly or configure `B3Propagator` if a future version exposes an `injectEncoding` option (check latest documentation). Alternatively, use environment variables `OTEL_PROPAGATORS="b3multi"`.
Be aware of this difference when integrating with existing Zipkin systems that might rely on `X-B3-ParentSpanId`. The OpenTelemetry span model creates distinct client and server spans.
Call `set_global_textmap(B3Propagator())` during your application's startup, or set the environment variable `OTEL_PROPAGATORS="b3"` (for single header injection) or `OTEL_PROPAGATORS="b3multi"` (if using a mechanism that allows it, usually implies `CompositePropagator` in code with `B3MultiFormat`).