Add preview support for bound instruments #4042
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #4042 +/- ##
==========================================
+ Coverage 82.00% 82.05% +0.06%
==========================================
Files 385 385
Lines 16031 16255 +224
==========================================
+ Hits 13144 13336 +192
- Misses 2887 2919 +32
🚀 New features to boost your workflow:
|
1fc14cf to
3690e7e
Compare
| // This macro affects SDK class layout/vtables and MUST match between the SDK | ||
| // library build and consumer translation units. | ||
| #if OPENTELEMETRY_ABI_VERSION_NO >= 2 && defined(ENABLE_METRICS_BOUND_INSTRUMENTS_PREVIEW) | ||
| # define OPENTELEMETRY_HAVE_METRICS_BOUND_INSTRUMENTS_PREVIEW 1 |
There was a problem hiding this comment.
I kept this derived preview macro in version.h so API and SDK headers use one consistent guard for the ABI-v2 + preview-flag combination. This is temporary while bound instruments are experimental; once the API is stabilized, we can remove the preview flag and keep the surface under the normal ABI rules.
|
this is ready for review now. This is preview-only and gated behind ABI v2 plus |
|
I also did a local throwaway stress test on a 64-core test VM to sanity-check the expected performance shape using the stress framework #3241 The result matches the design expectation: bound instruments help most when the workload records repeatedly to stable attribute sets, because the hot path avoids repeated attribute processing and hashmap lookup. The benefit is small when all threads record to the same single series, since the same bound cell becomes the contention point. With many pre-bound series, the bound path scaled much better because contention was spread across many handles. So I would not describe the largest speedup number as a general result, but the trend is encouraging and supports the usefulness of the preview API for high-concurrency, stable-attribute workloads. For example, with 2 attributes per series and 64 threads, the local stress test showed roughly:
|
dbarker
left a comment
There was a problem hiding this comment.
Thanks for the feature. I'm excited to try it. Please see some initial feedback/questions below.
2784346 to
d64810f
Compare
|
@dbarker Thanks for the review. I’ve addressed the comments and pushed the updates. Please let me know if you spot anything else. |
There was a problem hiding this comment.
Pull request overview
This PR introduces an experimental, feature-gated “bound synchronous instruments” preview for metrics ABI v2 in OpenTelemetry C++. It adds API and SDK support for binding a fixed attribute set once and recording repeated measurements via a bound handle, reducing per-call attribute processing overhead.
Changes:
- Adds new API surface for
Counter<T>::Bind(...)/Histogram<T>::Bind(...)and correspondingBoundCounter<T>/BoundHistogram<T>interfaces (plus noop implementations), guarded byOPENTELEMETRY_HAVE_METRICS_BOUND_INSTRUMENTS_PREVIEW. - Implements SDK-side bound storage (
SyncMetricStorage::Bind) with unified cardinality policy and multi-storage fanout (SyncMultiMetricStorage::Bind). - Adds targeted unit tests and benchmarks for bound vs unbound hot-path recording, and wires the preview behind CMake/Bazel feature gates.
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| test_common/cmake/preview-options.cmake | Enables the preview option in test preview configurations when ABI v2 is on. |
| sdk/test/metrics/measurements_benchmark.cc | Adds bound vs unbound fixed-attribute counter benchmarks (feature-gated). |
| sdk/test/metrics/CMakeLists.txt | Adds a preview-gated unit test target for bound sync instruments under ABI v2. |
| sdk/test/metrics/bound_sync_instruments_test.cc | Adds comprehensive unit tests for bound sync storage behavior (merge, overflow, GC, noop, multi-storage fanout, etc.). |
| sdk/src/metrics/sync_instruments.cc | Implements instrument-layer bound handles for counters/histograms and binds them to bound storage handles. |
| sdk/src/metrics/state/sync_metric_storage.cc | Adds bound-entry lifecycle, rotation, and unified cardinality handling integrated with collection. |
| sdk/include/opentelemetry/sdk/metrics/sync_instruments.h | Declares SDK instrument Bind(...) overrides under the preview macro. |
| sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h | Extends sync storage with Bind(...), BoundEntry, and unified cardinality tracking (preview-gated). |
| sdk/include/opentelemetry/sdk/metrics/state/multi_metric_storage.h | Adds bound-handle fanout support for multi-storage sync pipelines (preview-gated). |
| sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h | Introduces BoundSyncWritableMetricStorage and adds optional SyncWritableMetricStorage::Bind(...) (preview-gated). |
| CMakeLists.txt | Adds the CMake feature gate option for the preview and prints its status. |
| api/include/opentelemetry/version.h | Defines OPENTELEMETRY_HAVE_METRICS_BOUND_INSTRUMENTS_PREVIEW when ABI v2 + preview define are enabled. |
| api/include/opentelemetry/metrics/sync_instruments.h | Adds BoundCounter/BoundHistogram and Bind(...) API methods (preview-gated). |
| api/include/opentelemetry/metrics/noop.h | Adds noop bound handle implementations and hooks them into noop instruments. |
| api/CMakeLists.txt | Propagates ENABLE_METRICS_BOUND_INSTRUMENTS_PREVIEW to API consumers when the CMake option is enabled. |
| api/BUILD | Adds Bazel flag/config plumbing to enable the preview define under ABI v2. |
d64810f to
1bdd807
Compare
Summary
Related specs PR - open-telemetry/opentelemetry-specification#5050
Adds experimental bound synchronous metric instruments for metrics ABI v2. Bound instruments let callers bind a fixed attribute set once, then record repeated measurements through a bound handle:
Both bound handles record to the same metric instrument,
http.server.requests, with different pre-bound attribute sets. This avoids repeated per-call attribute processing and hashmap lookup for hot paths that reuse the same attribute set.Scope
This PR intentionally keeps the preview surface narrow:
Not included in this preview:
Those are left as follow-ups while the spec is still being finalized.
Feature Gate
The feature is disabled by default.
CMake:
Bazel:
CMake rejects enabling
WITH_METRICS_BOUND_INSTRUMENTS_PREVIEWwithout ABI v2.Bound code is guarded by:
OPENTELEMETRY_HAVE_METRICS_BOUND_INSTRUMENTS_PREVIEWImplementation Notes
Benchmark
Bound recording is about
18.3xfaster by mean for repeated recordings with the same fixed attribute set.Env: Ubuntu 24.04 · GCC 13.3 · AMD EPYC 9V45 (32C/64T) · 251 GiB RAM.
For significant contributions please make sure you have completed the following items:
CHANGELOG.mdupdated for non-trivial changes