feat: correlate logs with OTel traces + bridge to api-logs#172
Open
brentrager wants to merge 1 commit into
Open
feat: correlate logs with OTel traces + bridge to api-logs#172brentrager wants to merge 1 commit into
brentrager wants to merge 1 commit into
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: e3d9ff7 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 |
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