Prompt
I'm building a backend with many services how should I trace requests
Latest observation
Oct 28, 2026
OpenAI APIWeb search: off
For a backend with many services, the best way to trace requests is to use distributed tracing with a single correlation ID propagated across every service hop.
Recommended approach
-
Generate or accept a trace ID at the edge
- When a request enters your system (API gateway, load balancer, public API), create a new
trace_idif one doesn’t already exist. - If the caller sends one, validate and reuse it.
- When a request enters your system (API gateway, load balancer, public API), create a new
-
Propagate trace context through every service
- Pass the trace context in HTTP headers, message metadata, or gRPC metadata.
- Common standard: W3C Trace Context
traceparenttracestate
- If you’re using OpenTelemetry, it handles this well.
-
Create spans for each operation
- Each service should create:
- a server span for handling the incoming request
- client spans for outbound calls
- internal spans for important steps like DB queries, cache calls, queue publish/consume
- Each service should create:
-
Use structured logs with trace IDs
- Include
trace_id,span_id,service_name,request_id, anduser_idwhere appropriate. - This lets you jump from logs to traces and back.
- Include
-
Export traces to a tracing backend
- Popular choices:
- OpenTelemetry Collector
- Jaeger
- Grafana Tempo
- Zipkin
- commercial tools like Datadog, New Relic, Honeycomb
- Popular choices:
-
Sample and control volume
- In high-traffic systems, trace every request only in lower environments.
- In production, use:
- head-based sampling
- tail-based sampling for errors/slow requests
- always keep error traces if possible
Practical standard
If you want a default stack:
- Instrumentation: OpenTelemetry
- Context propagation: W3C
traceparent - Collection: OpenTelemetry Collector
- Backend: Grafana Tempo or Jaeger
- Logs: JSON logs with
trace_id
Good rules
- Never rely only on per-service local request IDs.
- Don’t invent custom trace headers unless you must.
- Keep trace IDs in async messages too, not just HTTP.
- Make trace IDs visible in logs and error responses in non-prod or internal APIs.
Example flow
API Gateway
→ creates trace context
→ calls Auth Service with same trace headers
→ Auth Service calls User Service and DB
→ each hop adds spans under the same trace
→ logs from all services contain the same trace_id
If you want, I can also show:
- a reference architecture
- header examples
- or sample implementation in a specific language/framework.