Skip to content

fix(stats): allow WithStatsAttributes to override library defaults#78

Merged
costela merged 2 commits into
mainfrom
obitech/override-default-stats
May 21, 2026
Merged

fix(stats): allow WithStatsAttributes to override library defaults#78
costela merged 2 commits into
mainfrom
obitech/override-default-stats

Conversation

@obitech
Copy link
Copy Markdown
Member

@obitech obitech commented May 12, 2026

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

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.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.name and db.client.connection.pool.name into statsOptions.defaultAttributes before applying StatsOptions so user attributes can override defaults.
  • Remove the late append of db.client.connection.pool.name during 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.

Comment thread meter.go Outdated
Co-authored-by: Leo Antunes <leo@costela.net>
@costela costela merged commit 1dd4120 into main May 21, 2026
3 checks passed
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.

3 participants