feat: correlate logs with OTel traces + bridge to api-logs - #172
Merged
Conversation
buildLogObject now stamps the active OTel span's real W3C trace_id/span_id onto every record (via @opentelemetry/api trace.getActiveSpan), replacing the fabricated uuid traceId; falls back to the prior uuid correlation id when no span is active. Each record is also emitted through the standard @opentelemetry/api-logs facade so it becomes an OTLP log record (correlated to the active span) when an observability LoggerProvider is registered — a cheap no-op otherwise, stdout output unchanged. Depends only on the @opentelemetry/api + api-logs facades (no SDK, no circular dep on @smooai/observability). Adds a real-SDK round-trip spec proving a log inside a span carries that span's ids in both the stdout JSON and the bridged OTLP record. th-de3805. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S2bM94GAnVjYSSv1x7HKRB
🦋 Changeset detectedLatest commit: 1d8486c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
…r deps Two review findings on this PR, both cheaper to fix now than after publish. 1. MISSING VALIDITY GUARD — a regression, not a nicety. `applyOtelCorrelation` stamped whatever `getActiveSpan()?.spanContext()` returned. An app that imports the OTel API but registers no TracerProvider gets a NonRecordingSpan carrying INVALID_SPAN_CONTEXT, and so does a context propagated from a sampled-out parent. Both stamp all-zero ids, which does not merely fail to correlate — it OVERWRITES the correlation uuid that `setCorrelationId` maintains, so the existing correlationId join breaks silently with no error. Go (`sc.IsValid()`), Rust (`span_context.is_valid()`) and Python (`if not ctx.is_valid`) all guard here. TypeScript was the only one that did not, in the highest-traffic runtime we have. Proven rather than asserted: reverting the one-line guard fails the new test with `expected '00000000000000000000000000000000' to be '11111111-…'`. The companion test stamps a VALID context, so the pair cannot pass vacuously if correlation were removed entirely. 2. `@opentelemetry/api` / `@opentelemetry/api-logs` moved to peerDependencies (kept in devDependencies so tests still install them). Both are global-singleton facades: two copies in a consumer's tree means two registries, `getActiveSpan()` returns undefined forever, and correlation quietly reverts to the uuid with no error. Every instrumentation library declares these peer. Changing it after publish is a breaking install-graph change for every consumer. 59 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
Loggerfabricated a random uuidtraceIdfor every record (CONTEXT/setCorrelationId). Log lines could never be joined to the OTel spans they occurred inside — the id was made up, not the real trace context.Solution (th-de3805)
buildLogObjectnow stamps the active OTel span's real W3CtraceId+spanId(via@opentelemetry/apitrace.getActiveSpan()?.spanContext()) onto every record. Falls back to the prior uuid correlation id only when no span is active. Stamped after the config/redact transforms so it always survives. Added aspanIdcontext key.@opentelemetry/api-logsglobal logger, so a log line becomes an OTLP log record (correlated to the active span, which the logs SDK reads from context) whenever an observabilityLoggerProvideris registered — e.g.@smooai/observability's new logs signal. With no provider registeredlogs.getLogger()is the api-logs no-op, so the bridge is cheap and stdout output is unchanged. Wrapped so telemetry can never break app logging.Depends only on the
@opentelemetry/api+@opentelemetry/api-logsfacades — no SDK dependency and no circular dep on@smooai/observability.Verification
tsc --noEmitclean (full multi-langtypecheckalso green),tsdownbuild clean, oxlint clean, 57/57 vitest tests pass.Logger.otel.spec.tsis a real-SDK round-trip: logs inside a real active span (real tracer + async-hooks context manager + realLoggerProvider/InMemoryLogRecordExporter) and asserts the span's real 32/16-hex ids appear in both the stdout JSON object and the bridged OTLP record; the no-span case asserts the uuid fallback + nospanId+ an uncorrelated record.🤖 Generated with Claude Code