Correlate Python logs to active OTel span + bridge to stdlib logging - #169
Merged
Conversation
Replaces the fabricated uuid trace id with the ACTIVE OTel span's real W3C trace_id/span_id when a span is active (get_current_span().get_span_context()), falling back to the prior uuid behavior only when no span is active. Adds a spanId field to the record. Also bridges every emitted line through a dedicated stdlib `logging` logger (smooai_logger) so an installed @smooai/observability LoggingHandler on the root logger turns each line into an OTLP log record — correlated to the active span, which that handler reads. NullHandler keeps it a true no-op when observability isn't installed. The file-rotation logger no longer propagates its pretty ANSI blob to root (would otherwise leak into the OTLP bridge). Depends on opentelemetry-api ONLY — never on @smooai/observability (circular). Verified with an api-only NonRecordingSpan: a log inside the span carries that span's ids; no span → uuid fallback; the bridged line reaches root within the span context. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S2bM94GAnVjYSSv1x7HKRB
|
… wire in camelCase Two review findings, both cheaper now than after five packages publish. 1. DOUBLE EMISSION — a regression for every existing Python consumer. The bridge logger propagates to root, so it only helps when observability's `LoggingHandler` is attached there. When it is NOT, but the app has called `logging.basicConfig()` — Lambda, uvicorn and pytest all do — root has a plain StreamHandler, and every smooai line is printed a SECOND time on top of our own stdout writer. In exchange for nothing. `_otel_consumer_present()` capability-detects a real OTel logging handler before emitting, and honours the family-wide `SMOOAI_OBSERVABILITY_DISABLED` kill switch. This is the same "enabled means a consumer is registered" contract the other languages use, rather than Python's always-on. Proven, not asserted: deleting the gate fails `test_no_otel_consumer_means_no_bridged_record` with "a plain root handler received a bridged record" and fails the kill-switch test. The paired `test_an_otel_consumer_does_receive_the_record` is the negative control — if the gate were simply always-off, it would fail instead. 2. `extra["correlation_id"]` was the only snake_case key on the wire; every other language emits camelCase (`ContextKey` in TS/Go/Rust/.NET). Renamed to `correlationId` before anything queries it, since changing it later is breaking. The PR's two existing bridge tests asserted delivery to ANY root handler — which is precisely the double-emission behaviour — so they now use an OTel-shaped handler and assert the contract that actually matters. 62 tests pass. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The Python logger fabricated a uuid trace id per context, so its logs never correlated to real OTel traces. And its lines never flowed through the stdlib
loggingfacade, so an installed@smooai/observabilityLoggingHandlernever saw them. (Pearl th-de3805; matches ADR-100.)Solution
_build_recordnow replaces the fabricated uuid with the active OTel span's real W3Ctrace_id/span_id(get_current_span().get_span_context()), adding aspanIdfield. Falls back to the prior uuid behavior only when no span is active.smooai_logger) so an observabilityLoggingHandleron root turns it into an OTLP log record — correlated to the active span, which that handler reads. ANullHandlerkeeps it a true no-op when observability isn't installed. The file-rotation logger no longer propagates its pretty ANSI blob to root (would otherwise leak into the bridge).opentelemetry-apionly — never on@smooai/observability(circular).Tests (verified, not just compiled)
python/tests/test_otel_correlation.py, api-onlyNonRecordingSpan:trace_id/span_idspanIdFull Python suite: 58 passed, ruff lint + format clean, basedpyright 0 errors.
🤖 Generated with Claude Code