- OpenTelemetry can automatically attach
trace_idandspan_idto log records, but only when a tracer is configured and a span is active. - With no tracer, logs still include other metadata (file, function, line) but will show zeroed or empty trace/span identifiers.
trace_id and span_id):
trace_id and span_id automatically.
The
tracer.start_as_current_span method can be used either as a context manager or as a decorator. When used as a decorator, the decorated function runs with the new span as the current active span for the duration of the function.trace_id and span_id, enabling automatic correlation between logs and traces.
- Spans printed by the
ConsoleSpanExporterincludetrace_idandspan_id. - Log records printed by the
ConsoleLogExporterinclude the sametrace_idandspan_idwhen generated while the span is active.
trace_id, unique span_id):
- Automatic log-trace correlation makes root-cause analysis much faster: you can jump from a log entry directly to the full trace to see timing, parent/child spans, and contextual attributes.
- This approach requires minimal changes: set up a tracer and ensure operations run inside spans.
Further reading and references
This setup demonstrates how to attach trace and span identifiers to log records automatically by configuring both OpenTelemetry logging and tracing. Once enabled, logs produced while spans are active will include
trace_id and span_id, giving you fast, reliable links from logs to traces for debugging and observability.