OpenTelemetry Trace Context Propagation Lost Fix
In this tutorial, you'll learn about OpenTelemetry Trace Context Propagation Lost Fix. We cover key concepts, practical examples, and best practices.
Your distributed traces appear as separate fragments — Service A shows a span but Service B shows a different trace ID. The trace context is not propagated across service boundaries because the propagator is not configured or HTTP headers are not forwarded.
The Problem
# Service A — sends request without context
import requests
def handle_request():
# No trace context propagated
requests.get("http://service-b/api")
Service B receives the request and starts a new trace instead of continuing the parent trace. The result is two disconnected trace fragments.
Step-by-Step Fix
1. Configure W3C TraceContext propagator
# Service A — configure propagation
from opentelemetry import propagate
from opentelemetry.propagators.tracecontext import TraceContextTextMapPropagator
propagate.set_global_textmap(TraceContextTextMapPropagator())
2. Inject context into outgoing requests
import requests
from opentelemetry import trace, propagate
from opentelemetry.instrumentation.requests import RequestsInstrumentor
# Auto-instrumentation — handles propagation automatically
RequestsInstrumentor().instrument()
tracer = trace.get_tracer(__name__)
def handle_request():
with tracer.start_as_current_span("service-a-call") as span:
span.set_attribute("request.path", "/api")
response = requests.get("http://service-b/api")
return response
3. Extract context on the receiving service
# Service B — extract context from incoming request
from opentelemetry import trace, propagate
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from flask import Flask, request
app = Flask(__name__)
FlaskInstrumentor().instrument_app(app)
@app.route("/api")
def api_handler():
# Context automatically extracted by FlaskInstrumentor
current_span = trace.get_current_span()
current_span.set_attribute("handler", "api_handler")
return {"status": "ok"}
4. Propagate context manually without auto-instrumentation
# Service A — manual injection
import requests
from opentelemetry import trace, propagate
from opentelemetry.propagators.tracecontext import TraceContextTextMapPropagator
tracer = trace.get_tracer(__name__)
def call_service_b():
with tracer.start_as_current_span("call-service-b") as span:
headers = {}
propagate.inject(headers) # Inject trace context into headers
response = requests.get(
"http://service-b/api",
headers=headers
)
return response
# Service B — manual extraction
from opentelemetry import trace, propagate
from opentelemetry.propagators.tracecontext import TraceContextTextMapPropagator
tracer = trace.get_tracer(__name__)
def handle_request(headers):
# Extract trace context from incoming headers
context = propagate.extract(headers)
token = trace.use_span(context)
with tracer.start_as_current_span("handle-request") as span:
span.set_attribute("http.method", "GET")
# ... handle request
token.close()
5. Verify propagation with environment variables
# Set the propagator via environment variable
export OTEL_PROPAGATORS=tracecontext
export OTEL_TRACES_EXPORTER=otlp
export OTEL_SERVICE_NAME=service-a
# Run your application
python app.py
Expected output in Jaeger/Grafana Tempo:
Trace: 1234abcd5678efgh
├── Service A: /api (2ms)
│ └── Service A: call-service-b (1ms)
└── Service B: /api (0.5ms)
└── Service B: handle-request (0.3ms)
Prevention Tips
- Configure
OTEL_PROPAGATORS=tracecontextas the default propagator - Use auto-instrumentation packages that handle propagation automatically
- Verify propagation with the
otel-demotest services - Check trace IDs across services in your tracing backend
- Use baggage for non-trace context propagation
Common Mistakes with trace context
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
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