Skip to content

feat: correlate logs with OTel traces + bridge to api-logs - #172

Merged
brentrager merged 2 commits into
mainfrom
feat/otel-trace-correlation
Aug 11, 2026
Merged

feat: correlate logs with OTel traces + bridge to api-logs#172
brentrager merged 2 commits into
mainfrom
feat/otel-trace-correlation

Conversation

@brentrager

Copy link
Copy Markdown
Contributor

Problem

Logger fabricated a random uuid traceId for 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)

  • Real correlationbuildLogObject now stamps the active OTel span's real W3C traceId + spanId (via @opentelemetry/api trace.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 a spanId context key.
  • Bridge to the logs facade — each record is emitted through the standard @opentelemetry/api-logs global logger, so a log line becomes an OTLP log record (correlated to the active span, which the logs SDK reads from context) whenever an observability LoggerProvider is registered — e.g. @smooai/observability's new logs signal. With no provider registered logs.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-logs facades — no SDK dependency and no circular dep on @smooai/observability.

Verification

  • tsc --noEmit clean (full multi-lang typecheck also green), tsdown build clean, oxlint clean, 57/57 vitest tests pass.
  • New Logger.otel.spec.ts is a real-SDK round-trip: logs inside a real active span (real tracer + async-hooks context manager + real LoggerProvider/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 + no spanId + an uncorrelated record.

Branched off main (repo tip was on chore/readme-glowup).

🤖 Generated with Claude Code

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-bot

changeset-bot Bot commented Jul 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1d8486c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@smooai/logger Minor

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>
@brentrager
brentrager merged commit 9c4fc8e into main Aug 11, 2026
1 check passed
@brentrager
brentrager deleted the feat/otel-trace-correlation branch August 11, 2026 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant