Prompt
What should I use for OpenTelemetry traces and logs correlation
Latest observation
Oct 28, 2026
OpenAI APIWeb search: off
For OpenTelemetry trace/log correlation, the key thing to use is the trace context, specifically:
trace_idspan_id- optionally
trace_flags/ sampling info
What to correlate on
In logs
Include these fields in every log record:
trace_idspan_idservice.nameresource attributeslikedeployment.environment,service.version
In traces
Use the active span context and make sure your logger can read it from the current context.
Recommended approach
- Use OpenTelemetry Context propagation
- Ensure the current span is stored in context/thread-local/async context.
- Inject trace context into logs automatically
- Most OTel SDKs/instrumentations can enrich logs with current span context.
- Use a log formatter/processor that outputs trace fields
- Typically JSON logs with
trace_idandspan_id.
- Typically JSON logs with
Standards/semantic conventions
OpenTelemetry’s log data model supports fields like:
trace_idspan_idtrace_flagsseverity_textbodyattributes
Practical recommendation
If you’re asking what to “use” as the correlation key:
- Primary key:
trace_id - Secondary key:
span_id
trace_id links all logs to the whole request/transaction, while span_id tells you which span the log was emitted from.
Example log entry
{
"timestamp": "2026-10-28T12:34:56Z",
"severity": "INFO",
"message": "User created",
"trace_id": "4f3c2a1b8e9d0f...",
"span_id": "a1b2c3d4e5f67890",
"service.name": "user-api"
}
If you want, I can also show:
- how to do this in Python / Java / Go / Node.js
- how to configure it with OTel Collector
- how to correlate logs in Grafana / Datadog / New Relic / ELK