Description:
Create a custom logging.Filter that automatically extracts the current OpenTelemetry trace context and injects trace_id and span_id into every log record. This enables correlation between logs and distributed traces.
Detailed Requirements:
- Create
OtelTraceFilter class (extends logging.Filter) that:
- Gets current span from
opentelemetry.trace.get_current_span()
- Extracts
trace_id and span_id from span context
- Adds fields to
LogRecord as attributes
- Handles case when no active span (sets to None or empty string)
- Add filter to FancyLogger's handlers
- Update
JsonFormatter to include trace_id/span_id fields
- Update
CustomFormatter to optionally show trace context
- Ensure zero overhead when OpenTelemetry not installed (graceful degradation)
- Format trace_id/span_id as hex strings (standard OTEL format)
Files to Modify:
mellea/core/utils.py - Add OtelTraceFilter, update formatters
Acceptance Criteria:
Description:
Create a custom
logging.Filterthat automatically extracts the current OpenTelemetry trace context and injectstrace_idandspan_idinto every log record. This enables correlation between logs and distributed traces.Detailed Requirements:
OtelTraceFilterclass (extendslogging.Filter) that:opentelemetry.trace.get_current_span()trace_idandspan_idfrom span contextLogRecordas attributesJsonFormatterto include trace_id/span_id fieldsCustomFormatterto optionally show trace contextFiles to Modify:
mellea/core/utils.py- Add OtelTraceFilter, update formattersAcceptance Criteria: