Propagate Trace IDs between services
mainFastAPI instrumentation only covers the ASGI app's request/response cycle. To maintain a single trace across multiple services when making outbound HTTP calls, you must manually inject the current span context into the request headers using opentelemetry.propagate.inject or use the HTTPXClientInstrumentor to automate the process.
# Manual injection using inject()
from opentelemetry.propagate import inject
@app.get("/chain")
async def chain(response: Response):
headers = {}
inject(headers) # inject trace info to header
async with httpx.AsyncClient() as client:
await client.get(f"http://other-service:8000/", headers=headers)
# Automatic injection using HTTPX instrumentation
import httpx
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
HTTPXClientInstrumentor().instrument()
@app.get("/chain")
async def chain(response: Response):
async with httpx.AsyncClient() as client:
await client.get(f"http://other-service:8000/") # Headers injected automatically