Cross-service tracing
A request id is powerful inside one service. It gets more powerful when every downstream service carries the same id. If your API calls a vector database, a feature flag service, and an LLM provider, all four logs can be joined on one field.
utils/http.py
python
import httpx
from middleware import request_id_ctx
async def http_get(url: str, **kwargs) -> httpx.Response:
headers = kwargs.pop("headers", {})
rid = request_id_ctx.get()
if rid:
headers.setdefault("X-Request-ID", rid)
async with httpx.AsyncClient() as client:
return await client.get(url, headers=headers, **kwargs)A thin wrapper around httpx that reads the current request id from the ContextVar and sets it as a header on every outbound call. Downstream services that run the same middleware pick it up and log against the same id.
One trace id across services
Gateway, API, and downstream all log the same id.
AI prompt: Ask an AI to turn your JSON logs into one story
Loading practice…
Quiz: Quiz
Loading practice…