Inject active span trace_id into log records#65
Conversation
Adds a logging.Filter that pulls the active span's trace_id (from either ddtrace or OTel, whichever provider is active) and stamps it on every LogRecord. Updates the format string to surface it as trace_id=<32-hex>. Defaults to empty when no tracing is active or no span is current, so the format never KeyErrors. Enables Grafana Explore's logs-to-traces derived-field click-through for milter log lines.
Greptile SummaryAdds a
Confidence Score: 5/5Safe to merge — the change is additive and isolated to log formatting; the filter always sets a default so the format string is safe even with no active tracing provider. The logic is straightforward: the filter guards every exit path with a default empty string, the handler-level attachment means all loggers are covered, and Python's module cache keeps the per-record import lookups cheap. Both the ddtrace and OTel branches are handled correctly, including the 0-valued INVALID_TRACE_ID case. No files require special attention. Important Files Changed
Reviews (2): Last reviewed commit: "Drop dead-code None guard on get_current..." | Re-trigger Greptile |
OTel's get_current_span() always returns a Span (a live span or the INVALID_SPAN sentinel), never None. The trace_id == 0 check already handles the no-active-span case — the if-span-else-None guard above it was unreachable and misleading. Per Greptile P2 nit on #65.
Summary
logging.Filterthat pulls the active span'strace_id(from either ddtrace or OTel, whichever provider is active) and stamps it on everyLogRecord.trace_id=<32-hex>.This enables logs-to-traces correlation in Grafana Explore (and any other UI that runs a regex over the line) for milter log lines — previously the click-through never matched.
Test plan
trace_id=<32-hex>inside an active OTel span, clears it outside, no KeyError on weird messages, no KeyError when no provider is active.trace_id=field in stdout.