Skip to content

Add WithSpanErrorAttributesGetter to set error attributes on spans - #651

Merged
XSAM merged 4 commits into
mainfrom
branch/otelsql-issue-377-a3be31
Sep 5, 2026
Merged

XSAM merged 4 commits into
mainfrom
branch/otelsql-issue-377-a3be31

Conversation

@XSAM

@XSAM XSAM commented Sep 5, 2026

Copy link
Copy Markdown
Owner

Closes #377

What changed

  • New SpanErrorAttributesGetter type and WithSpanErrorAttributesGetter option. The getter receives the error returned by the driver and its attributes are set on the span.
  • recordSpanError now takes the full config instead of SpanOptions, calls the getter for every non-nil error on a recording span, and no longer invokes SpanOptions.RecordError when the error is nil.
  • Tests for the option wiring, attribute setting, interaction with RecordError / DisableErrSkip, nil span, and non-recording span.
  • Changelog entry under Unreleased.

Why

Semantic conventions define db.response.status_code as a span attribute, but the only place a driver status code is visible in database/sql is the returned error. The existing AttributesGetter runs at span creation with no error, and SpanOptions.RecordError sees the error but has no access to the span. Metrics already have InstrumentErrorAttributesGetter for exactly this purpose; spans had no equivalent hook.

Reviewer notes

  • The getter shares the func(error) []attribute.KeyValue shape with InstrumentErrorAttributesGetter, so one function can be passed to both options.
  • The getter runs regardless of RecordError and DisableErrSkip, matching how the metrics getter ignores DisableSkipErrMeasurement. A response status code is still useful on a span even when the error is deliberately not recorded as a span error. This is documented on the option.
  • The getter is skipped when the span is not recording, so sampled-out spans pay only a nil check.
  • Passing config by value to recordSpanError copies a larger struct than SpanOptions did. Happy to switch to a pointer if preferred.

🤖 Generated with Claude Code

XSAM and others added 2 commits September 4, 2026 17:23
Semantic conventions define db.response.status_code as a span attribute,
but the only place the driver error is visible is after the operation
completes, and the existing AttributesGetter runs at span creation with
no error. Metrics already have InstrumentErrorAttributesGetter for this
purpose; spans had no equivalent hook.

Add SpanErrorAttributesGetter, invoked from recordSpanError for every
non-nil error on a recording span, with the returned attributes set on
the span. It shares the func(error) []attribute.KeyValue shape with the
metrics getter so one function can serve both signals. The getter runs
regardless of SpanOptions.RecordError and DisableErrSkip so response
details can be attached even when the error is not recorded as a span
error.

recordSpanError now takes the full config instead of SpanOptions, and
no longer calls RecordError when the error is nil.

Closes #377

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@XSAM XSAM added this to the v0.44.0 milestone Sep 5, 2026
@XSAM
XSAM marked this pull request as ready for review September 5, 2026 00:33
@codecov

codecov Bot commented Sep 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.4%. Comparing base (59ba93b) to head (c93ea25).

Additional details and impacted files
@@          Coverage Diff          @@
##            main    #651   +/-   ##
=====================================
  Coverage   89.4%   89.4%           
=====================================
  Files         15      15           
  Lines        576     579    +3     
=====================================
+ Hits         515     518    +3     
  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 .tools cache was keyed only on the tools module hash, so bumping the
CI Go version reused a golangci-lint binary built by the previous Go.
Its embedded type checker then failed on the newer standard library
("method must have no type parameters" in math/rand/v2), breaking the
lint job on main and every PR. The compatibility matrix also shared one
cache across three Go versions. Keying the cache on the installed Go
version forces a rebuild whenever the toolchain changes.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@XSAM
XSAM merged commit 97ff1d0 into main Sep 5, 2026
24 checks passed
@XSAM
XSAM deleted the branch/otelsql-issue-377-a3be31 branch September 5, 2026 05:34
@XSAM XSAM mentioned this pull request Sep 6, 2026
XSAM added a commit that referenced this pull request Sep 6, 2026
This release is the last to support [Go 1.25].
The next release will require at least [Go 1.26].

### Added

- Support testing of [Go 1.27]. (#650)
- Add `WithSpanErrorAttributesGetter` option to set additional
attributes (e.g., `db.response.status_code`) on spans when an operation
returns an error. (#651)
- Add `SpanOptions.RowsChildOfQuery` to create `sql.rows` spans as
children of the `sql.conn.query` or `sql.stmt.query` span that produced
them, so concurrent queries can be correlated with their result
iteration. (#652)
- Support `driver.RowsColumnScanner` on [Go 1.27]. (#649)

### Changed

- Replace `ErrorTypeAttributes` with `ErrorTypeAttribute` to avoid
allocating an additional slice. (#643)
- Upgrade OTel to `v1.46.0`. (#644)

### Fixed

- `db.client.operation.duration` histogram now uses the explicit bucket
boundaries recommended by the OTel Semantic Conventions (`0.001` to `10`
seconds) instead of the SDK defaults, which placed virtually all
database operations in the first bucket. (#632)

[Go 1.26]: https://go.dev/doc/go1.26
[Go 1.25]: https://go.dev/doc/go1.25
[Go 1.27]: https://go.dev/doc/go1.27
XSAM added a commit to boekkooi-impossiblecloud/XSAM-otelsql that referenced this pull request Nov 14, 2026
Resolve conflicts with XSAM#643, XSAM#649, XSAM#651 and XSAM#652:
- recordSpanError takes *config (needed for SpanErrorAttributesGetter)
- otRows keeps pointer receivers and durationMetric, wrapped by
  wrapRowsColumnScanner
- durationMetric uses ErrorTypeAttribute instead of ErrorTypeAttributes

Co-Authored-By: Claude Fable 5.2 <[email protected]>
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.

add a mechanism to record db.response.status_code

1 participant