perf: LoggerProvider bridge — inject trace_id into stdlib logging #206

Closed
opened 2026-03-24 23:50:06 +00:00 by kert · 1 comment
Owner

Implement src/perf/_logs.py for correlated logs.

Tasks

  • Python logging.Filter that injects trace_id and span_id from current OTel context into log records
  • Log formatter that includes trace context fields (Promtail/Loki can parse these)
  • attach_trace_filter() utility to wire into existing loggers
  • Loki label extraction: Promtail pipeline stage to parse trace_id from log lines
  • Tests: verify trace_id propagation from active span into log output
Implement `src/perf/_logs.py` for correlated logs. ## Tasks - [ ] Python `logging.Filter` that injects `trace_id` and `span_id` from current OTel context into log records - [ ] Log formatter that includes trace context fields (Promtail/Loki can parse these) - [ ] `attach_trace_filter()` utility to wire into existing loggers - [ ] Loki label extraction: Promtail pipeline stage to parse `trace_id` from log lines - [ ] Tests: verify trace_id propagation from active span into log output
kert added this to the P26: Pipeline Telemetry — OTel instrumentation, perf module, Grafana dashboards milestone 2026-03-24 23:50:06 +00:00
Author
Owner

Complete. Created src/perf/_logs.py with:

  • TraceContextFilter — injects trace_id + span_id into stdlib log records
  • TraceContextFormatter — includes trace context in formatted output
  • attach_trace_filter() / detach_trace_filter() — idempotent attach/detach

6 tests in test_logs.py:

  • Zero IDs outside span, real IDs inside span
  • Attach adds filter, detach removes, double-attach idempotent
  • Formatter includes non-zero trace context

Promtail can parse trace_id from log lines for Loki correlation.

✅ 6/6 passed, lint clean.

Complete. Created `src/perf/_logs.py` with: - `TraceContextFilter` — injects trace_id + span_id into stdlib log records - `TraceContextFormatter` — includes trace context in formatted output - `attach_trace_filter()` / `detach_trace_filter()` — idempotent attach/detach 6 tests in `test_logs.py`: - Zero IDs outside span, real IDs inside span - Attach adds filter, detach removes, double-attach idempotent - Formatter includes non-zero trace context Promtail can parse trace_id from log lines for Loki correlation. ✅ 6/6 passed, lint clean.
kert closed this issue 2026-03-25 00:04:24 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: homelab/stack#206