feat(otel): add W3C TraceContext propagator for distributed trace correlation

Set up CompositePropagator with TraceContextTextMapPropagator and
W3CBaggagePropagator so that trace context (traceparent/tracestate
headers) is propagated to downstream services via instrumented
HTTP clients (requests, httpx, aiohttp).

Without this, each service starts its own trace root and there is
no way to correlate Open WebUI spans with backend LLM service spans
(e.g. LiteLLM, vLLM) in a trace viewer.

All required classes are already in opentelemetry-api — no new
dependencies needed.
This commit is contained in:
Constantine 2026-04-08 12:43:49 +03:00
parent 20544d412e
commit 60a85ee140
No known key found for this signature in database

View file

@ -1,5 +1,9 @@
from fastapi import FastAPI
from opentelemetry import trace
from opentelemetry.propagate import set_global_textmap
from opentelemetry.propagators.composite import CompositePropagator
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
from opentelemetry.baggage.propagation import W3CBaggagePropagator
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
@ -30,6 +34,11 @@ def setup(app: FastAPI, db_engine: Engine):
resource = Resource.create(attributes={SERVICE_NAME: OTEL_SERVICE_NAME})
if ENABLE_OTEL_TRACES:
trace.set_tracer_provider(TracerProvider(resource=resource))
set_global_textmap(
CompositePropagator(
[TraceContextTextMapPropagator(), W3CBaggagePropagator()]
)
)
# Add basic auth header only if both username and password are not empty
headers = []