fix(stats): allow WithStatsAttributes to override library defaults#78
Merged
Conversation
Library defaults were appended after user attrs in recordStats, so any override for db.client.connection.pool.name supplied via WithStatsAttributes was silently dropped by attribute.NewSet's last-value-wins dedup. Seed both defaults (db.system.name and db.client.connection.pool.name) into statsOptions before options are applied so user attrs land at the end of the slice and win on key collision. Add a regression test using OTel's manual reader that verifies user overrides take effect while non-overridden defaults are preserved.
There was a problem hiding this comment.
Pull request overview
This PR fixes stats metric attribute override behavior by ensuring library-supplied default attributes are applied before user-provided WithStatsAttributes, so user values win on key collisions (matching attribute.NewSet’s last-value-wins semantics). It also adds a regression test to confirm the override behavior while preserving non-overridden defaults.
Changes:
- Seed
db.system.nameanddb.client.connection.pool.nameintostatsOptions.defaultAttributesbefore applyingStatsOptions so user attributes can override defaults. - Remove the late append of
db.client.connection.pool.nameduring callback registration. - Add a regression test using the OTel SDK manual reader to verify override semantics and default preservation.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| meter.go | Moves default attribute construction earlier and ensures default pool-name attribute is included before user attrs so overrides work. |
| meter_test.go | Adds regression test verifying user-provided db.client.connection.pool.name overrides the library default while db.system.name remains the default. |
| go.mod | Adds explicit dependency on go.opentelemetry.io/otel/sdk/metric for the new manual-reader-based test. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
costela
approved these changes
May 21, 2026
Co-authored-by: Leo Antunes <leo@costela.net>
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.
Library defaults were appended after user attrs in recordStats, so any override for db.client.connection.pool.name supplied via WithStatsAttributes was silently dropped by attribute.NewSet's last-value-wins dedup. Seed both defaults (db.system.name and db.client.connection.pool.name) into statsOptions before options are applied so user attrs land at the end of the slice and win on key collision.
Add a regression test using OTel's manual reader that verifies user overrides take effect while non-overridden defaults are preserved.
Ref #71