Skip to content

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

Open
brentrager wants to merge 1 commit into
mainfrom
feat/otel-trace-correlation
Open

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

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

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