Prompt
Correlate logs with traces
Latest observation
Oct 28, 2026
OpenAI APIWeb search: off
To correlate logs with traces, you usually want to ensure both systems share a common identifier, then use that identifier in your observability backend.
Core idea
Add trace context to each log entry:
trace_idspan_id- optionally
service.name,environment,request_id,user_id
Then you can:
- jump from a log line to the exact distributed trace
- search all logs for a specific trace
- filter traces using error logs
How to do it
1) Propagate trace context
Use W3C Trace Context:
traceparenttracestateif needed
This lets services pass the same trace across HTTP/gRPC/message queues.
2) Inject trace IDs into logs
Configure your logger to automatically include the current span context.
Examples:
- Java: OpenTelemetry + Logback/Log4j MDC
- Python: OpenTelemetry logging instrumentation
- Node.js: OpenTelemetry + pino/winston custom formatter
- Go: add trace/span IDs from
context.Contextto structured logs
3) Use structured logging
Prefer JSON logs so fields are machine-readable:
{
"timestamp": "2026-10-28T12:00:00Z",
"level": "error",
"message": "payment failed",
"trace_id": "4f3c2d...",
"span_id": "a1b2c3...",
"service": "checkout"
}
4) Index logs and traces in the same observability tool
Common platforms:
- Grafana Loki + Tempo
- Elastic Observability
- Datadog
- New Relic
- Splunk
- Honeycomb
Configure links like:
- from a log to the trace using
trace_id - from a trace to related logs using the same
trace_id
Practical example
If a request enters your API gateway:
- A new trace starts
- Each service adds spans
- Every log emitted during that request includes the trace/span IDs
- In your UI, clicking a log entry opens the trace
Best practices
- Log at span boundaries and on errors
- Don’t log entire trace context in plain text if it’s not indexed
- Ensure sampling doesn’t make trace lookup impossible
- Use consistent field names across all services
- Add request IDs too, since not all logs may be trace-aware
If you want, I can show:
- a language-specific example,
- how to do this with OpenTelemetry, or
- how to correlate logs and traces in Grafana/Tempo, Datadog, or Elastic.