Logs
Correlation IDs and Request Tracing in Logs
How attaching a trace or request ID to every log line turns a pile of isolated events into a navigable path through a distributed request.
Last updated
A correlation ID is a unique identifier attached to every log line produced while handling a single request, so that logs scattered across many services and processes can be grouped back into the story of that one request. Without it, a request that touches five microservices produces five disconnected sets of log lines with no way to tell they belong together short of matching timestamps by hand.
In practice, correlation IDs are rarely a bespoke concept anymore — they piggyback on the same trace context used for distributed tracing. A trace ID generated at the entry point of a request is propagated to every downstream service call and stamped onto every log line, every span, and often every metric exemplar produced while handling that request.
Why it matters
- It’s the bridge between traces and logs. A trace tells you which service was slow or failed; the logs from that exact span explain why. Without a shared ID connecting them, an engineer has to guess which log lines, out of millions, belong to the failing request — with it, it’s a direct lookup.
- It makes distributed debugging tractable. A single user action can fan out across dozens of services, queues, and async jobs. A correlation ID is what lets you reconstruct that entire path after the fact, rather than reasoning about each service’s logs in isolation.
- It turns “logs from this service” into “logs from this request.” Most incidents are request-scoped, not service-scoped — a specific user, a specific transaction, a specific edge case. Correlation IDs let queries target exactly that scope instead of a noisy service-wide time window.
- It works even when logs and traces live in different backends. Because the ID is just a field on both the log line and the span, a log stored in Loki or Elasticsearch and a trace stored in Tempo or Jaeger can still be joined at query time — the systems don’t need to share storage, only the identifier.
How it works
- Trace context propagation. The W3C Trace Context standard defines a
traceparentheader (carrying a trace ID, the current span ID, and trace flags) that’s passed along on every outbound call — HTTP header, gRPC metadata, or message queue attribute — so every service in the path can extract it and continue the same trace. OpenTelemetry SDKs implement this propagation automatically for supported frameworks and libraries. - Stamping the ID onto logs. Once a service has the active trace context, its logging library enriches every log line emitted during that request with
trace_idandspan_idfields — usually via middleware or a context-aware logger, so individual log statements don’t need to pass the ID explicitly. - Generating an ID when no trace exists yet. At a system’s entry point (a public API gateway, a load balancer), there may be no incoming trace context to continue. The edge service generates a new trace ID there and propagates it forward, so everything downstream inherits it.
- Jumping from a trace to its logs. Because the span and the log lines share a
trace_id(and often the more specificspan_id), tools that support trace-to-logs correlation can take a slow or failing span in a trace view and query the log backend directly for every log line stamped with that same ID — collapsing what used to be a manual, cross-tool search into a single click. - Extending the pattern to async and batch work. Message queues and background jobs can carry the same trace ID as a message attribute so that work picked up minutes later by an unrelated worker still logs with the originating request’s correlation ID, keeping the chain intact even across asynchronous boundaries.
Getting this right depends on structured logging being in place first — trace_id needs to be a real, consistently named field, not buried in a free-text message — and it’s most valuable when paired with traces and a backend that supports jumping between the two directly.
Related tools
A log aggregation system from Grafana Labs that indexes only labels, not full log content, to keep storage and query costs low.
The full-text search and analytics engine at the core of the ELK stack, widely used for log storage and search.
Language-specific instrumentation libraries (Java, Go, Python, JS/TS, .NET, and more) for emitting traces, metrics, and logs.
A managed observability platform built around high-cardinality, wide structured events, marketed as the model for what its founders call 'Observability 2.0'.