OpenTelemetry Propagator Not Injecting Context Fix
In this tutorial, you'll learn about OpenTelemetry Propagator Not Injecting Context Fix. We cover key concepts, practical examples, and best practices.
Your OpenTelemetry trace context is not injected into outgoing HTTP requests — downstream services receive no traceparent header, breaking distributed tracing. The propagator is not set globally, or the injection point is not reached during request processing.
The Problem
import requests
from opentelemetry import trace
# WRONG: propagator never configured
tracer = trace.get_tracer(__name__)
def call_downstream():
with tracer.start_as_current_span("downstream-call"):
resp = requests.get("http://service-b/api")
No traceparent or tracestate headers appear in the outgoing HTTP request. The downstream service starts a new trace.
Step-by-Step Fix
1. Set the global propagator
from opentelemetry import propagate
from opentelemetry.propagators.tracecontext import TraceContextTextMapPropagator
propagate.set_global_textmap(TraceContextTextMapPropagator())
2. Use composite propagators
from opentelemetry import propagate
from opentelemetry.propagators.composite import CompositePropagator
from opentelemetry.propagators.tracecontext import TraceContextTextMapPropagator
from opentelemetry.propagators.baggage import BaggagePropagator
propagate.set_global_textmap(CompositePropagator([
TraceContextTextMapPropagator(),
BaggagePropagator(),
]))
3. Inject context into custom carriers
from opentelemetry import trace, propagate
from opentelemetry.propagators.tracecontext import TraceContextTextMapPropagator
propagate.set_global_textmap(TraceContextTextMapPropagator())
tracer = trace.get_tracer(__name__)
def call_downstream():
with tracer.start_as_current_span("downstream-call") as span:
# Create a carrier (dict of headers)
headers = {}
# Inject current context into the carrier
propagate.inject(headers)
# Use the headers in your HTTP request
resp = requests.get(
"http://service-b/api",
headers=headers,
)
4. Verify injected headers
from opentelemetry import trace, propagate
from opentelemetry.propagators.tracecontext import TraceContextTextMapPropagator
propagate.set_global_textmap(TraceContextTextMapPropagator())
tracer = trace.get_tracer(__name__)
def debug_propagation():
with tracer.start_as_current_span("debug-span") as span:
headers = {}
propagate.inject(headers)
for key, value in headers.items():
print(f"{key}: {value}")
# Expected output:
# traceparent: 00-1234abcd5678efgh1234abcd5678efgh-5678efgh1234abcd-01
5. Configure via environment variables
# Set propagators via environment variable
export OTEL_PROPAGATORS=tracecontext,baggage
# Run your application
opentelemetry-instrument python app.py
6. Extract context on the receiving side
# Service B — extract from incoming request
from flask import Flask, request
from opentelemetry import propagate
from opentelemetry.propagators.tracecontext import TraceContextTextMapPropagator
propagate.set_global_textmap(TraceContextTextMapPropagator())
app = Flask(__name__)
@app.route("/api")
def api_handler():
# Extract context from incoming headers
context = propagate.extract(request.headers)
token = trace.use_span(context)
with trace.get_tracer(__name__).start_as_current_span("handler") as span:
# This span is now a child of the incoming trace
return {"status": "ok"}
token.close()
Expected output (debug):
traceparent: 00-0a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d-1a2b3c4d5e6f7a8b-01
tracestate: vendor1=value1,vendor2=value2
Prevention Tips
- Set the propagator early (at application startup) before any tracing code runs
- Use
CompositePropagatorto support multiple propagation formats - Verify propagation headers with debug logging
- Configure propagators via
OTEL_PROPAGATORSenvironment variable - Test propagation end-to-end with two services
Common Mistakes with propagator
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations
These mistakes appear frequently in real-world OPENTELEMETRY code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro