Skip to main content

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.


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

  1. Add library import:
from opentelemetry import trace
  1. Obtain current span context:
ctx = trace.get_current_span().get_span_context()
  1. Get trace_id and span_id:
trace_id = '{trace:032x}'.format(trace=ctx.trace_id)
span_id = '{span:016x}'.format(span=ctx.span_id)
  1. Inject to log:'logging test trace_id=%s span_id=%s', trace_id, span_id)
Privacy Statement
Terms of Use

Copyright © 2024 by Sumo Logic, Inc.