← Back to Pull requests

sdk/metric: wire experimental WithBinding for Sum instruments #9590

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 24, 2026 15:41 UTC
Author

Part of #8759

Adds experimental sdkmetricx.WithBinding() to go.opentelemetry.io/otel/sdk/metric/x and wires bound-instrument support for Int64Counter, Float64Counter, Int64UpDownCounter, and Float64UpDownCounter using the internal BoundSum aggregation merged in #9278.

Summary

  • Opt-in provider option (sdk/metric/x.WithBinding()): Uses the existing structural experimentalOption seam (BindingEnabled() bool) so regular sdk/metric.NewMeterProvider() instruments do not expose experimental Binder methods and incur zero hot-path overhead.

  • Sum instrument wiring (sdk/metric/experimental_bind.go): Decorates opted-in meters for the four counter and up-down counter types using the shared forEachStream pipeline resolver. AggregationSum streams bind via Builder.BoundSum (resolving filters, cardinality limits, and exemplar reservoirs at Bind() time); other compatible aggregations (such as Histogram views or reader selectors) fall back to recording with the pre-bound attributes.

  • Bound recording semantics:

  • - Attribute-free Add calls (including empty attribute options) invoke pre-resolved BoundMeasure closures with zero map lookups and zero allocations.
    - Record-time attributes merge on top of bound attributes (record-time wins on duplicate keys) and fan out across all resolved streams.
    - Caller slices passed to Bind() are cloned and normalized (attrnorm.SetDedup) so caller storage is never mutated or retained.
    - Bound and unbound calls to the same attribute set share the same BoundSum series and cardinality slot.
  • Examples, tests, and benchmarks: Adds runnable ExampleWithBinding, unit/concurrency tests covering both temporalities, views, composable views, opt-in views, cardinality evaluation at bind time, exemplars, and BenchmarkInt64CounterBind.
  • Alternatives Considered

  • Separate sdk/metric/internal/bind package (mirroring sdk/metric/internal/finish): Unlike finish.Lifecycle, sdk/metric/internal/aggregate does not need to import binding wrappers because aggregate.Binder[N] and aggregate.BoundMeasure[N] already live in aggregate. Keeping the meter and instrument wrappers in sdk/metric/experimental_bind.go (matching experimental_configurator_.go) avoids an extra internal package and redundant factory interface while keeping sdk/metric production code free of metric/x imports.

  • Composing WithBinding() and WithFinish() in this PR: Deferred per the SIG agreement on #8962 until combined lifecycle + bound support is requested. Passing both options logs a warning via global.Warn, with WithBinding() taking precedence on overlapping counter instruments.
  • Benchmarks (benchstat, -count=6 -benchtime=100ms)

    Bound Int64Counter.Add cuts recording latency by ~87% for Cumulative and ~79% for Delta with 0 B/op, 0 allocs/op. Unbound recording on an opted-in provider stays within ~1-3% of stable, and all existing sdk/metric/x benchmarks show 0 allocation changes (sdk/metric/instrument.go, sum.go, and atomic.go are unchanged).

    goos: linux
    goarch: amd64
    pkg: go.opentelemetry.io/otel/sdk/metric/x
    cpu: Intel(R) Xeon(R) CPU @ 2.20GHz
                                                                                  │ /tmp/bench_main.txt │          /tmp/bench_pr.txt           │
                                                                                  │       sec/op        │    sec/op      vs base               │
    Int64CounterRecord/stable-24                                                           216.2n ± 17%    223.7n ± 28%        ~ (p=0.240 n=6)
    Int64CounterRecord/finish-24                                                           263.4n ± 13%    273.3n ± 14%        ~ (p=0.240 n=6)
    Int64CounterFinish/reused-slice-24                                                     507.8n ± 17%    496.2n ± 12%        ~ (p=0.937 n=6)
    Int64CounterFinish/inline-24                                                           683.6n ± 10%    624.8n ± 15%        ~ (p=0.329 n=6)
    Float64CounterRecord/stable-24                                                         226.1n ± 14%    256.8n ±  7%  +13.56% (p=0.041 n=6)
    Float64CounterRecord/finish-24                                                         283.0n ± 20%    288.9n ± 14%        ~ (p=0.937 n=6)
    Float64CounterFinish/reused-slice-24                                                   527.9n ± 16%    494.9n ± 13%        ~ (p=0.310 n=6)
    Float64CounterFinish/inline-24                                                         649.9n ±  6%    633.4n ± 16%        ~ (p=0.937 n=6)
    ConfiguratorSyncMeasure/Int64Counter/Attributes/0/provider=stable-24                   58.28n ±  8%    64.05n ± 11%   +9.88% (p=0.004 n=6)
    ConfiguratorSyncMeasure/Int64Counter/Attributes/0/provider=configurator-24             67.73n ±  6%    58.59n ±  4%  -13.48% (p=0.002 n=6)
    ConfiguratorSyncMeasure/Int64Counter/Attributes/10/provider=stable-24                  66.75n ±  5%    67.45n ± 10%        ~ (p=0.394 n=6)
    ConfiguratorSyncMeasure/Int64Counter/Attributes/10/provider=configurator-24            64.03n ± 20%    67.80n ±  5%        ~ (p=0.132 n=6)
    ConfiguratorSyncMeasure/Int64Histogram/Attributes/0/provider=stable-24                 139.3n ±  7%    143.1n ±  4%        ~ (p=0.394 n=6)
    ConfiguratorSyncMeasure/Int64Histogram/Attributes/0/provider=configurator-24           97.90n ±  4%   123.95n ± 33%  +26.62% (p=0.002 n=6)
    ConfiguratorSyncMeasure/Int64Histogram/Attributes/10/provider=stable-24                121.5n ±  7%    166.5n ± 32%  +36.98% (p=0.041 n=6)
    ConfiguratorSyncMeasure/Int64Histogram/Attributes/10/provider=configurator-24          105.3n ±  3%    107.3n ±  3%        ~ (p=0.087 n=6)
    Int64GaugeRecord/stable-24                                                             200.5n ± 16%    186.0n ± 20%        ~ (p=0.240 n=6)
    Int64GaugeRecord/finish-24                                                             223.9n ± 18%    237.6n ± 13%        ~ (p=0.394 n=6)
    Int64GaugeFinish/reused-slice-24                                                       446.7n ± 14%    492.4n ± 17%        ~ (p=0.180 n=6)
    Int64GaugeFinish/inline-24                                                             660.0n ±  9%    677.9n ±  6%        ~ (p=0.065 n=6)
    Float64GaugeRecord/stable-24                                                           182.9n ± 23%    181.3n ± 12%        ~ (p=0.699 n=6)
    Float64GaugeRecord/finish-24                                                           244.2n ±  8%    229.8n ± 15%        ~ (p=0.485 n=6)
    Float64GaugeFinish/reused-slice-24                                                     496.6n ± 15%    479.8n ±  9%        ~ (p=0.699 n=6)
    Float64GaugeFinish/inline-24                                                           680.2n ± 11%    687.5n ±  6%        ~ (p=0.699 n=6)
    Int64UpDownCounterRecord/stable-24                                                     204.3n ± 11%    184.2n ±  6%   -9.84% (p=0.026 n=6)
    Int64UpDownCounterRecord/finish-24                                                     226.7n ±  8%    237.2n ± 17%        ~ (p=0.589 n=6)
    Int64UpDownCounterFinish/reused-slice-24                                               493.7n ±  8%    507.8n ± 11%        ~ (p=0.699 n=6)
    Int64UpDownCounterFinish/inline-24                                                     688.4n ± 10%    672.1n ±  8%        ~ (p=0.589 n=6)
    Float64UpDownCounterRecord/stable-24                                                   185.0n ± 14%    189.9n ± 15%        ~ (p=1.000 n=6)
    Float64UpDownCounterRecord/finish-24                                                   211.0n ± 12%    229.4n ± 10%        ~ (p=0.132 n=6)
    Float64UpDownCounterFinish/reused-slice-24                                             441.8n ± 19%    535.9n ± 19%  +21.31% (p=0.041 n=6)
    Float64UpDownCounterFinish/inline-24                                                   674.2n ± 12%    651.5n ±  9%        ~ (p=0.132 n=6)
    Int64CounterBind/Cumulative/stable-24                                                                  190.8n ± 20%
    Int64CounterBind/Cumulative/unbound-24                                                                 195.7n ±  8%
    Int64CounterBind/Cumulative/bound-24                                                                   25.43n ±  5%
    Int64CounterBind/Delta/stable-24                                                                       203.4n ± 14%
    Int64CounterBind/Delta/unbound-24                                                                      204.6n ± 17%
    Int64CounterBind/Delta/bound-24                                                                        42.84n ±  6%
    geomean                                                                                248.8n          223.5n         +2.72%
    
                                                                                  │ /tmp/bench_main.txt │         /tmp/bench_pr.txt          │
                                                                                  │        B/op         │    B/op     vs base                │
    Int64CounterRecord/stable-24                                                           16.00 ± 0%     16.00 ± 0%       ~ (p=1.000 n=6) ¹
    Int64CounterRecord/finish-24                                                           16.00 ± 0%     16.00 ± 0%       ~ (p=1.000 n=6) ¹
    Int64CounterFinish/reused-slice-24                                                     0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64CounterFinish/inline-24                                                           128.0 ± 0%     128.0 ± 0%       ~ (p=1.000 n=6) ¹
    Float64CounterRecord/stable-24                                                         16.00 ± 0%     16.00 ± 0%       ~ (p=1.000 n=6) ¹
    Float64CounterRecord/finish-24                                                         16.00 ± 0%     16.00 ± 0%       ~ (p=1.000 n=6) ¹
    Float64CounterFinish/reused-slice-24                                                   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64CounterFinish/inline-24                                                         128.0 ± 0%     128.0 ± 0%       ~ (p=1.000 n=6) ¹
    ConfiguratorSyncMeasure/Int64Counter/Attributes/0/provider=stable-24                   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    ConfiguratorSyncMeasure/Int64Counter/Attributes/0/provider=configurator-24             0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    ConfiguratorSyncMeasure/Int64Counter/Attributes/10/provider=stable-24                  0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    ConfiguratorSyncMeasure/Int64Counter/Attributes/10/provider=configurator-24            0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    ConfiguratorSyncMeasure/Int64Histogram/Attributes/0/provider=stable-24                 0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    ConfiguratorSyncMeasure/Int64Histogram/Attributes/0/provider=configurator-24           0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    ConfiguratorSyncMeasure/Int64Histogram/Attributes/10/provider=stable-24                0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    ConfiguratorSyncMeasure/Int64Histogram/Attributes/10/provider=configurator-24          0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64GaugeRecord/stable-24                                                             0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64GaugeRecord/finish-24                                                             0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64GaugeFinish/reused-slice-24                                                       0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64GaugeFinish/inline-24                                                             128.0 ± 0%     128.0 ± 0%       ~ (p=1.000 n=6) ¹
    Float64GaugeRecord/stable-24                                                           0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64GaugeRecord/finish-24                                                           0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64GaugeFinish/reused-slice-24                                                     0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64GaugeFinish/inline-24                                                           128.0 ± 0%     128.0 ± 0%       ~ (p=1.000 n=6) ¹
    Int64UpDownCounterRecord/stable-24                                                     0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64UpDownCounterRecord/finish-24                                                     0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64UpDownCounterFinish/reused-slice-24                                               0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64UpDownCounterFinish/inline-24                                                     128.0 ± 0%     128.0 ± 0%       ~ (p=1.000 n=6) ¹
    Float64UpDownCounterRecord/stable-24                                                   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64UpDownCounterRecord/finish-24                                                   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64UpDownCounterFinish/reused-slice-24                                             0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64UpDownCounterFinish/inline-24                                                   128.0 ± 0%     128.0 ± 0%       ~ (p=1.000 n=6) ¹
    Int64CounterBind/Cumulative/stable-24                                                                 0.000 ± 0%
    Int64CounterBind/Cumulative/unbound-24                                                                0.000 ± 0%
    Int64CounterBind/Cumulative/bound-24                                                                  0.000 ± 0%
    Int64CounterBind/Delta/stable-24                                                                      0.000 ± 0%
    Int64CounterBind/Delta/unbound-24                                                                     0.000 ± 0%
    Int64CounterBind/Delta/bound-24                                                                       0.000 ± 0%
    geomean                                                                                           ²               +0.00%               ²
    ¹ all samples are equal
    ² summaries must be >0 to compute geomean
    
                                                                                  │ /tmp/bench_main.txt │         /tmp/bench_pr.txt          │
                                                                                  │      allocs/op      │ allocs/op   vs base                │
    Int64CounterRecord/stable-24                                                           1.000 ± 0%     1.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64CounterRecord/finish-24                                                           1.000 ± 0%     1.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64CounterFinish/reused-slice-24                                                     0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64CounterFinish/inline-24                                                           1.000 ± 0%     1.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64CounterRecord/stable-24                                                         1.000 ± 0%     1.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64CounterRecord/finish-24                                                         1.000 ± 0%     1.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64CounterFinish/reused-slice-24                                                   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64CounterFinish/inline-24                                                         1.000 ± 0%     1.000 ± 0%       ~ (p=1.000 n=6) ¹
    ConfiguratorSyncMeasure/Int64Counter/Attributes/0/provider=stable-24                   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    ConfiguratorSyncMeasure/Int64Counter/Attributes/0/provider=configurator-24             0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    ConfiguratorSyncMeasure/Int64Counter/Attributes/10/provider=stable-24                  0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    ConfiguratorSyncMeasure/Int64Counter/Attributes/10/provider=configurator-24            0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    ConfiguratorSyncMeasure/Int64Histogram/Attributes/0/provider=stable-24                 0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    ConfiguratorSyncMeasure/Int64Histogram/Attributes/0/provider=configurator-24           0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    ConfiguratorSyncMeasure/Int64Histogram/Attributes/10/provider=stable-24                0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    ConfiguratorSyncMeasure/Int64Histogram/Attributes/10/provider=configurator-24          0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64GaugeRecord/stable-24                                                             0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64GaugeRecord/finish-24                                                             0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64GaugeFinish/reused-slice-24                                                       0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64GaugeFinish/inline-24                                                             1.000 ± 0%     1.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64GaugeRecord/stable-24                                                           0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64GaugeRecord/finish-24                                                           0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64GaugeFinish/reused-slice-24                                                     0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64GaugeFinish/inline-24                                                           1.000 ± 0%     1.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64UpDownCounterRecord/stable-24                                                     0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64UpDownCounterRecord/finish-24                                                     0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64UpDownCounterFinish/reused-slice-24                                               0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64UpDownCounterFinish/inline-24                                                     1.000 ± 0%     1.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64UpDownCounterRecord/stable-24                                                   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64UpDownCounterRecord/finish-24                                                   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64UpDownCounterFinish/reused-slice-24                                             0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=6) ¹
    Float64UpDownCounterFinish/inline-24                                                   1.000 ± 0%     1.000 ± 0%       ~ (p=1.000 n=6) ¹
    Int64CounterBind/Cumulative/stable-24                                                                 0.000 ± 0%
    Int64CounterBind/Cumulative/unbound-24                                                                0.000 ± 0%
    Int64CounterBind/Cumulative/bound-24                                                                  0.000 ± 0%
    Int64CounterBind/Delta/stable-24                                                                      0.000 ± 0%
    Int64CounterBind/Delta/unbound-24                                                                     0.000 ± 0%
    Int64CounterBind/Delta/bound-24                                                                       0.000 ± 0%
    geomean                                                                                           ²               +0.00%               ²
    ¹ all samples are equal
    ² summaries must be >0 to compute geomean
    

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: dashpole Review duration: 8810m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #9590"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.7% · Safe to merge
    27.7%