Go logger: correlate lines with the active OTel span (th-de3805)#171
Open
brentrager wants to merge 1 commit into
Open
Go logger: correlate lines with the active OTel span (th-de3805)#171brentrager wants to merge 1 commit into
brentrager wants to merge 1 commit into
Conversation
Every log line's traceId was a fabricated uuid (== correlationId), so a line could never be joined to the trace that produced it. New *Context log methods (InfoContext/ErrorContext/…) read the active span from the passed context and stamp its real W3C trace_id + span_id onto the line; with no active span the prior uuid behavior is untouched. Also adds SetSlogHandler: each line is mirrored to an installed slog.Handler carrying the call's context, so the @smooai/observability otelslog bridge turns a logger line into an OTLP log record correlated to the active span (→ /v1/logs). The application wires the handler in; logger depends only on go.opentelemetry.io/otel/trace (API), never on @smooai/observability, so there is no dependency cycle. Deps: go.opentelemetry.io/otel/trace v1.44.0 (API only). otel v1.44.0 requires go 1.25.0, matching the sibling observability Go module. 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
Every Go log line's
traceIdwas a fabricated uuid (equal tocorrelationId), set inresetGlobalContext/setCorrelationID. A line could never be joined to the trace that actually produced it.Solution
*Contextlog methods —TraceContext/DebugContext/InfoContext/WarnContext/ErrorContext/FatalContext. Each reads the active span from the passedcontext.Context(trace.SpanContextFromContext) and stamps its real W3CtraceId+spanIdonto the line. With no active span, the prior uuid behavior is untouched (correlationIdstays the fabricated uuid; nospanId).SetSlogHandler(h)— every line is mirrored to an installedslog.Handlercarrying the call'sctx, so the@smooai/observabilityotelslog bridge turns a logger line into an OTLP log record correlated to the active span (→/v1/logs). The application wires the handler in; the logger depends only ongo.opentelemetry.io/otel/trace(API), never on@smooai/observability— no dependency cycle.All log methods now route through one
logWithcore; the caller-frame skip was bumped by one to keepcaller.*pointing at the same site as before.Deps
go.opentelemetry.io/otel/tracev1.44.0 (API only;otelv1.44.0 indirect). otel v1.44.0 requires go 1.25.0, matching the sibling observability Go module.Tests
go test ./...→ 62 passed (4 new). Correlation verified by round-trip, not just compiled:TestInfoContextUsesActiveSpanIDs— active span in ctx → emitted JSONtraceId/spanIdequal the span's ids,correlationIdstays the uuid.TestInfoWithoutSpanFallsBack— no span →traceId== fabricated uuid, nospanId.TestSlogForwardCarriesSpanContext— reads the span back out of the forwarded ctx, proving the bridge would correlate.TestSlogForwardNoHandlerIsNoOp.go vetclean,gofmtclean.🤖 Generated with Claude Code