Correlate .NET logger trace id with active span (th-de3805)#170
Open
brentrager wants to merge 1 commit into
Open
Correlate .NET logger trace id with active span (th-de3805)#170brentrager wants to merge 1 commit into
brentrager wants to merge 1 commit into
Conversation
The .NET logger seeded traceId with a fabricated uuid (== correlationId) at
construction. When a log is emitted inside an active distributed trace, stamp
the real W3C trace/span id from System.Diagnostics.Activity.Current instead, so
CloudWatch JSON logs correlate to the trace. Captured at emit time — the span is
live at the log call, not necessarily when the logger was created. Falls back to
the prior traceId (fabricated uuid, no spanId) when no W3C activity is in scope.
- SmooLogger.Emit: override traceId + add spanId from Activity.Current (W3C only).
- New ContextKey.SpanId ("spanId").
- Depends only on the BCL diagnostics API (Activity) — no @smooai/observability
dependency (would be circular). Logger lines already flow through the ForwardTo
ILogger the obs provider hooks, so an active obs provider turns them into OTLP
log records with matching ids.
Tests prove a log inside an active Activity carries the span's trace/span id, and
that it falls back to the correlation uuid with no activity.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S2bM94GAnVjYSSv1x7HKRB
|
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 .NET logger seeded
traceIdwith a fabricated uuid (equal tocorrelationId) at construction. When a log is emitted inside an active distributed trace, the JSON logs (CloudWatch) did not carry the real trace id, so they couldn't be correlated to the trace.Solution
At emit time, when an active W3C
System.Diagnostics.Activityis in scope, stamp the entry'straceId(and a newspanId) with the span's real W3C ids. Captured at emit time, not construction — the span is live at the log call, not necessarily when the logger was created. Falls back to the prior behavior (fabricated uuidtraceId, nospanId) when no W3C activity is active.SmooLogger.Emit: overridetraceId+ addspanIdfromActivity.Current(W3C format only).ContextKey.SpanId("spanId").Activity) — not on@smooai/observability(that would be circular). Logger lines already flow through theForwardToILoggerthe obs provider hooks, so when the observability OTel logging provider is active a logger line becomes an OTLP log record carrying the same ids.Verification
Log_Within_Active_Activity_Carries_Real_TraceAndSpanIdstarts a realActivitySourcespan, logs, parses the JSON output, and assertstraceId/spanIdequalactivity.TraceId/SpanIdhex.Log_Without_Activity_Falls_Back_To_Correlation_Uuidasserts the fallback (traceId == correlationId, no spanId).dotnet build(net8/9/10, warnings-as-errors),dotnet test(35 passed, 2 new), anddotnet format --verify-no-changesall green in Release.🤖 Generated with Claude Code
https://claude.ai/code/session_01S2bM94GAnVjYSSv1x7HKRB