Repository navigation
Record db.client.operation.duration in the span's context so exemplars link to the DB span - #670
Open
karlipegomes wants to merge 1 commit into
Open
karlipegomes wants to merge 1 commit into
karlipegomes wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #670 +/- ##
=======================================
+ Coverage 89.9% 90.2% +0.3%
=======================================
Files 16 16
Lines 608 627 +19
=======================================
+ Hits 547 566 +19
Misses 61 61 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The duration was recorded with the context captured before the span of the call was created, so its exemplars linked to the parent span. Record it in the context of the span created for the call; when no span is created or the span is not sampled, keep the caller's context. Getters still receive the caller's context. Fixes XSAM#669
karlipegomes
force-pushed
the
fix/duration-exemplar-span-context
branch
from
November 18, 2026 02:44
5b2e296 to
61ec15a
Compare
Author
|
Update: I squashed the branch into one commit. The earlier lint-fix commit had made |
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.
Fixes #669
Problem
startDurationMetric(ctx)captures the caller's context beforecreateSpanruns, andRecordrecords the duration with it. The SDK's exemplar reservoir takes the span from that context (trace.SpanContextFromContext), so every exemplar ofdb.client.operation.durationlinks to the parent span instead of the database client span that measured the call. In Grafana (Prometheus exemplars → Tempo), clicking an exemplar ofsql.conn.queryopens the parent span.Change
After the span of the call is created,
linkSpansets its context as the context the duration is recorded with. The caller's context is kept when no span is created (span filters,Omit*options) and when the span is not sampled, so aTraceBasedexemplar falls back to the parent instead of being dropped, and anAlwaysOnexemplar never points at a span that is not exported.InstrumentAttributesGetterstill receives the caller's context, asAttributesGetterdoes. This is howotelhttpandotelgrpcrecord their duration metrics (with the context returned bytracer.Start).Covered paths: connector connect; conn ping, exec, query, prepare, begin, reset session; stmt exec and query; rows; tx commit and rollback.
Edge cases: a call with no parent span now gets exemplars under
TraceBased(its DB span is a sampled root); with a sampler that is not parent-based, the exemplar follows the DB span's sampling decision.Tests
exemplar_test.go:TestDurationExemplarLinksToMethodSpan,_Connand_Stmtassert, with anAlwaysOnexemplar filter, that each method's exemplar carries the span ID of that method's span. They fail onmain.TestDurationExemplarWithoutMethodSpan,TestDurationExemplarUnsampledMethodSpanandTestInstrumentAttributesGetterReceivesCallerContextguard the unchanged behavior: the caller's span when no span is created or the span is not sampled, and the caller's context for getters.golangci-lint run ./...reports 0 issues andgo test -race ./...passes.Note: #662 also touches
durationMetric.Record; whichever lands second needs a small rebase.