sdk/metric: add metric.WithAllowKeyDuplication #8709
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 84.1%. Comparing base (
ec606c3) to head (10c5eda).:warning: Report is 15 commits behind head on main.
<details><summary>Additional details and impacted files</summary>

@@ Coverage Diff @@
## main #8709 +/- ##
=====================================
Coverage 84.0% 84.1%
=====================================
Files 329 329
Lines 26068 26110 +42
=====================================
+ Hits 21918 21961 +43
+ Misses 3769 3768 -1
Partials 381 381
| Files with missing lines | Coverage Δ | |
|---|---|---|
| sdk/metric/config.go | 95.5% <100.0%> (+0.2%) | :arrow_up: |
| sdk/metric/instrument.go | 98.4% <100.0%> (+0.2%) | :arrow_up: |
| sdk/metric/meter.go | 92.7% <100.0%> (+0.2%) | :arrow_up: |
| sdk/metric/provider.go | 100.0% <100.0%> (ø) | |
... and 3 files with indirect coverage changes
</details>
<details><summary> :rocket: New features to boost your workflow: </summary>
</details>
Please fix lint and coverage, then align naming and semantics with #8488.
metric.Observer is captured from the callback and used after Collect returns. observer.ObserveInt64 assumes pipeline.produce still holds the pipeline guard, so this measures an unsupported lifetime rather than the asynchronous API path.
Could we keep the observation inside the callback and benchmark collection, or make this an explicit internal resolver microbenchmark?
allowDupKeys now adds dispatch to every default-path measurement, but the posted results compare two configurations in the same head build. They cannot show whether the unchanged default regresses relative to main.
Could we add main-versus-head benchstat for the default configuration before settling on this dispatch shape? This is the comparison required for a performance-critical hot-path change.
The behavior is consistently wired across the metric paths, but the performance evidence still needs to represent supported async use and establish that the default hot path has not regressed.
yup!! I can see the problem here. I shoudl keep the observation inside a WithInt64Callback and the benchmark measures reader.Collect. Thanks!
yupp! updated the PR with fresh main vs head benchstat
just a gentle ping- @MrAlias, can I get some reviews here whenever you get a chance!!
<!-- pull-request-dashboard-status -->
<!-- pull-request-dashboard-status-revision:21 -->
Pull request dashboard status
Closed · refreshed 2026-10-19 21:49 UTC
<details>
<summary>Status above doesn't look right?</summary>
<br>
</details>
Hey @Mujib-Ahasan I believe you should first resolve the conflicts in the PR, then ask for review, that will be helpful for maintainers while reviewing.
This PR have not seen much review interest over the past three months and Im finding it difficult to stay motivated to continue working on it without any signals from the maintainers! Happy to revisit it if there is renewed interest. For now I am closing this PR.
MrAlias
Mujib-Ahasan
Adds
WithAllowKeyDuplication()to sdk/metric as a MeterProvider to opt out of the attribute map key deduplication( introduced: https://github.com/open-telemetry/opentelemetry-go/pull/8471)fixes: #8479
Benchmarks
goos: darwin
goarch: arm64
pkg: go.opentelemetry.io/otel/sdk/metric
cpu: Apple M4
| Operation | Deduplication | Keys | ns/op | B/op | allocs/op |
|-----------|---------------|----------------------|------:|-----:|----------:|
| Add | Enabled | With Duplicates | 533.9 | 1000 | 8 |
| Add | Disabled | With Duplicates | 234.2 | 424 | 4 |
| Add | Enabled | Without Duplicates | 353.1 | 424 | 4 |
| Add | Disabled | Without Duplicates | 233.3 | 424 | 4 |
| Record | Enabled | With Duplicates | 543.0 | 1000 | 8 |
| Record | Disabled | With Duplicates | 245.4 | 424 | 4 |
| Record | Enabled | Without Duplicates | 365.0 | 424 | 4 |
| Record | Disabled | Without Duplicates | 244.5 | 424 | 4 |
| Observe | Enabled | With Duplicates | 619.1 | 1000 | 8 |
| Observe | Disabled | With Duplicates | 308.1 | 424 | 4 |
| Observe | Enabled | Without Duplicates | 440.2 | 424 | 4 |
| Observe | Disabled | Without Duplicates | 307.2 | 424 | 4 |
With duplicate map keys (opt-in benefit):
Without duplicate map keys:
main vs head
benchstat