Python TraceId and SpanId injection into logs configuration
To add trace_id
and span_id
to your Python lambda logs, you'll have to get a context from current span and attach it to the log.
info
Make sure that the code from below is active in the .py
file where your application requests are traced with OpenTelemetry python instrumentation. Logs not related to traced requests will not have span_id
or trace_id
filled in.
Python lambda
- Add library import:
from opentelemetry import trace
- Obtain current span context:
ctx = trace.get_current_span().get_span_context()
- Get trace_id and span_id:
trace_id = '{trace:032x}'.format(trace=ctx.trace_id)
span_id = '{span:016x}'.format(span=ctx.span_id) - Inject to log:
log.info('logging test trace_id=%s span_id=%s', trace_id, span_id)