Skip to content

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
XSAM:mainfrom
karlipegomes:fix/duration-exemplar-span-context
Open

karlipegomes wants to merge 1 commit into
XSAM:mainfrom
karlipegomes:fix/duration-exemplar-span-context

Conversation

@karlipegomes

@karlipegomes karlipegomes commented Nov 18, 2026 •

Copy link
Copy Markdown

Fixes #669

Problem

startDurationMetric(ctx) captures the caller's context before createSpan runs, and Record records the duration with it. The SDK's exemplar reservoir takes the span from that context (trace.SpanContextFromContext), so every exemplar of db.client.operation.duration links to the parent span instead of the database client span that measured the call. In Grafana (Prometheus exemplars → Tempo), clicking an exemplar of sql.conn.query opens the parent span.

Change

After the span of the call is created, linkSpan sets 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 a TraceBased exemplar falls back to the parent instead of being dropped, and an AlwaysOn exemplar never points at a span that is not exported. InstrumentAttributesGetter still receives the caller's context, as AttributesGetter does. This is how otelhttp and otelgrpc record their duration metrics (with the context returned by tracer.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, _Conn and _Stmt assert, with an AlwaysOn exemplar filter, that each method's exemplar carries the span ID of that method's span. They fail on main.
  • TestDurationExemplarWithoutMethodSpan, TestDurationExemplarUnsampledMethodSpan and TestInstrumentAttributesGetterReceivesCallerContext guard 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 and go test -race ./... passes.

Note: #662 also touches durationMetric.Record; whichever lands second needs a small rebase.

@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.2%. Comparing base (1c61098) to head (61ec15a).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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
karlipegomes force-pushed the fix/duration-exemplar-span-context branch from 5b2e296 to 61ec15a Compare November 18, 2026 02:44
@karlipegomes

Copy link
Copy Markdown
Author

Update: I squashed the branch into one commit. The earlier lint-fix commit had made InstrumentAttributesGetter receive the span's context, which the issue said would not change. The branch now keeps getters on the caller's context, and doesn't link a DB span that isn't sampled (so a TraceBased exemplar falls back to the parent instead of being dropped). The description is updated, and the tests now include ping, prepare, reset session, rollback, the unsampled case and the getter context.

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.

Exemplars of db.client.operation.duration link to the parent span instead of the DB span

1 participant