Skip to content

[SDK] Support MeterConfigurator updates and example - #4313

Open
adityabavadekar wants to merge 35 commits into
open-telemetry:mainfrom
adityabavadekar:add_meter_configurator_update
Open

adityabavadekar wants to merge 35 commits into
open-telemetry:mainfrom
adityabavadekar:add_meter_configurator_update

Conversation

@adityabavadekar

@adityabavadekar adityabavadekar commented Jul 27, 2026 •

Copy link
Copy Markdown

Fixes #4256

Adds the ability to update a MeterConfigurator (and the MeterConfig of
existing meters) at runtime. Following the pattern used in #4065 and #4224.

This implements the optional requirement to allow updating the
MeterProvider's MeterConfigurator:
https://opentelemetry.io/docs/specs/otel/metrics/sdk/#meterconfigurator

This function is called when a Meter is first created, and for each
outstanding Meter when a MeterProvider's MeterConfigurator is updated
(if updating is supported).

Changes

  • Adds UpdateMeterConfigurator to MeterProvider to support setting new
    meter configs at runtime. The update holds the same lock as GetMeter, so a
    meter is never returned with a config that is out of date with the provider
    configurator. A null configurator is ignored.
  • Moves the Meter enabled flag into an atomic state shared with its
    instruments, so updates reach existing instruments without blocking
    instrument creation or Collect.
  • Adds MeterContext::SetMeterConfigurator.
  • Adds tests and an example.

Behavior

While a Meter is disabled (also covered in the example README):

  • Existing instruments don't need to be recreated. They start recording again once the Meter is enabled.
  • New measurements are dropped and async callbacks are not called.
  • Data recorded before the Meter was disabled is still exported.

For significant contributions please make sure you have completed the following items:

  • CHANGELOG.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed

Adds MeterProvider::UpdateMeterConfigurator. It sets a new MeterConfig on
all meters that already exist, and new meters get the new configurator too.

The update takes the same lock as GetMeter, so GetMeter can never hand back
a meter with an old config. The meter enabled flag is now an atomic, so the
update does not block instrument creation or Collect.
@adityabavadekar
adityabavadekar requested a review from a team as a code owner July 27, 2026 09:59
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 27, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: adityabavadekar / name: Aditya Bavadekar (0209a3d)

@codecov

codecov Bot commented Jul 27, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.71233% with 34 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.63%. Comparing base (cdd17eb) to head (86b8516).

Files with missing lines Patch % Lines
sdk/src/metrics/sync_instruments.cc 68.00% 32 Missing ⚠️
sdk/src/metrics/meter.cc 90.91% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4313      +/-   ##
==========================================
- Coverage   86.81%   86.63%   -0.18%     
==========================================
  Files         525      526       +1     
  Lines       20736    20809      +73     
==========================================
+ Hits        18000    18025      +25     
- Misses       2736     2784      +48     
Files with missing lines Coverage Δ
...etry/sdk/instrumentationscope/scope_configurator.h 100.00% <ø> (ø)
sdk/include/opentelemetry/sdk/metrics/meter.h 62.50% <100.00%> (+5.36%) ⬆️
...de/opentelemetry/sdk/metrics/meter_enabled_state.h 100.00% <100.00%> (ø)
...clude/opentelemetry/sdk/metrics/sync_instruments.h 100.00% <100.00%> (ø)
sdk/src/metrics/meter_context.cc 94.12% <100.00%> (+0.31%) ⬆️
sdk/src/metrics/meter_context_factory.cc 100.00% <ø> (ø)
sdk/src/metrics/meter_provider.cc 93.69% <100.00%> (+0.83%) ⬆️
sdk/src/metrics/meter_provider_factory.cc 100.00% <ø> (ø)
sdk/src/metrics/meter.cc 84.00% <90.91%> (+1.19%) ⬆️
sdk/src/metrics/sync_instruments.cc 65.46% <68.00%> (-1.98%) ⬇️

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Fixes include lists reported by include-what-you-use. Adds a bazel target for
the example, and tests for the null configurator and invalid context branches.
@adityabavadekar
adityabavadekar force-pushed the add_meter_configurator_update branch from eeaf61d to ab71694 Compare July 28, 2026 11:55

@dbarker dbarker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the feature and tests! Please see some initial comments below.

Comment thread examples/meter_configurator/main.cc Outdated
Comment thread examples/meter_configurator/main.cc Outdated
Comment thread examples/meter_configurator/main.cc Outdated
Comment thread sdk/include/opentelemetry/sdk/metrics/meter.h Outdated
Comment thread sdk/include/opentelemetry/sdk/metrics/meter.h Outdated
Comment thread examples/meter_configurator/main.cc Outdated
Comment thread sdk/test/metrics/meter_provider_sdk_test.cc
Comment thread sdk/src/metrics/meter.cc Outdated
Comment thread examples/meter_configurator/README.md Outdated
Comment thread sdk/src/metrics/meter_provider.cc Outdated
const std::lock_guard<std::mutex> guard(lock_);
context_->SetMeterConfigurator(std::move(meter_configurator));

for (auto &meter : context_->GetMeters())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

for (auto &meter : context_->GetMeters())GetMeters() is explicitly non-thread-safe, so this loop is safe only under a cross-class invariant: every production mutation of meters_ currently reaches AddMeter() or RemoveMeter() through a MeterProvider method that holds lock_. That invariant is not stated here, and it is not structurally enforced because MeterContext exposes both mutators publicly.

T1: provider lock_ -> GetMeters() -> iterate borrowed span
T2: meter_lock_    -> AddMeter()  -> meters_ may reallocate

lock_ does not exclude a direct/future T2 caller. The analogous tracer and logger loops explicitly document their ownership invariant, and their collections are provider-private.

Please either enforce this invariant by using ForEachMeter() or making the mutators provider-only, or at minimum add the equivalent ownership comment above this loop so future callers do not introduce a race.

@adityabavadekar adityabavadekar Sep 11, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Switched to ForEachMeter

- Share the Meter enabled flag with its instruments so they observe config changes
- Drop the creation-time noop branches, so a disabled Meter no longer returns a permanent noop
- Remove kNoopMeter, now unused
- Keep GetMeters() in UpdateMeterConfigurator; ForEachMeter deadlocks against the collect path
- Gate SyncMetricStorage::Bind so a disabled Meter does not consume cardinality
- Skip duplicate-instrument warnings while a Meter is disabled
- Rename Meter::IsMeterEnabled to IsEnabled
- Assert collected sums in tests instead of only scope names
- Add regression tests for revival, disable, the deadlock, and Bind cardinality
- Use cstdint and std types in the example, and cut its export interval to 10s
- Drop the README claim that the export transcript is exact
@dbarker dbarker added the pr:fix-merge-conflicts Please fix merge conflicts for this pr label Aug 25, 2026
@dbarker dbarker removed the pr:fix-merge-conflicts Please fix merge conflicts for this pr label Aug 25, 2026

@dbarker dbarker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the updates! Please see some feedback and requested changes below.

Comment thread sdk/test/metrics/meter_provider_sdk_test.cc Outdated
Comment thread sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h Outdated
Comment thread sdk/src/metrics/meter.cc Outdated
Comment thread sdk/src/metrics/meter.cc Outdated
Comment thread sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h Outdated
Comment thread sdk/src/metrics/sync_instruments.cc
@dbarker

dbarker commented Sep 11, 2026

Copy link
Copy Markdown
Member

Hi @adityabavadekar, Just checking in. Are you still interested in contributing this PR?

dbarker and others added 2 commits September 11, 2026 16:47
Bind returned nullptr for a disabled meter and the caller wrapped it into a
non-null handle with null storage, so the instrument stayed silent even after
re-enabling. Handles now bind lazily on the first record.
BoundCounterObservesMeterEnabledState missed this by giving the storage and the
instrument separate enabled states.

Also:

- Require meter_enabled_state in SyncMetricStorage, dropping the null checks it
  makes unreachable in Bind and Synchronous::IsEnabled.
- Keep the instrument identity warnings in meter.cc independent of IsEnabled.
- Remove UpdateMeterConfiguratorConcurrentWithCollectDoesNotDeadlock, which
  relied on unsupported reentrant GetMeter and caused the tsan lock-order
  inversion.
- Resolve the reported include-what-you-use diagnostics.
@adityabavadekar

Copy link
Copy Markdown
Author

Yes, I am still interested, and I have fixed the issues you mentioned. Please review.
The changes:

  • Reverted the IsEnabled() warning conditions, at 4 sites rather than 2 since
    WarnOnDuplicateInstrument is also identity-based. All 4 match main again.
  • Made meter_enabled_state required, updating 27 test construction sites, and removed 2 null
    checks it makes unreachable.
  • Removed the deadlock test.
  • Fixed a bug introduced earlier: SyncMetricStorage::Bind returning nullptr was wrapped into
    a handle with null storage, so instruments bound while disabled stayed silent after
    re-enabling. It now binds lazily on enable. BoundCounterObservesMeterEnabledState masked
    this by using two separate states.
  • Merged main and fixed the iwyu diagnostics.

- Drop the includes iwyu reports as unused in sync_instruments.cc and
  sync_metric_storage_gauge_test.cc. mutex is kept with an IWYU pragma because
  LazyBoundEntry uses std::mutex and std::lock_guard directly.
- Use cstdint rather than stdint.h in meter_provider_sdk_test.cc.
@dbarker
dbarker self-requested a review September 12, 2026 18:47
Comment thread sdk/src/metrics/sync_instruments.cc Outdated
Comment thread sdk/test/metrics/meter_provider_sdk_test.cc Outdated
Comment thread sdk/test/metrics/meter_provider_sdk_test.cc Outdated
Co-authored-by: Marc Alff <marc.alff@free.fr>
Comment thread sdk/src/metrics/sync_instruments.cc
@marcalff

Copy link
Copy Markdown
Member

Trying to resolve remaining IWYU issues, but doing that with the web UI with comments ... it may take a few attempts.

Comment thread sdk/src/metrics/sync_instruments.cc
Comment thread sdk/test/metrics/bound_sync_instruments_test.cc Outdated
Comment thread sdk/test/metrics/meter_provider_sdk_test.cc
Comment thread sdk/test/metrics/meter_provider_sdk_test.cc
marcalff and others added 3 commits October 11, 2026 17:43
Comment thread sdk/test/metrics/meter_provider_sdk_test.cc Outdated
Comment thread sdk/test/metrics/meter_provider_sdk_test.cc Outdated
Comment thread sdk/test/metrics/meter_provider_sdk_test.cc Outdated
@marcalff

Copy link
Copy Markdown
Member

@adityabavadekar

Thanks for the contribution.

I did small fixes to includes, to make the IWYU CI pass in all 3 flavors, so CI is now clean.

@ThomsonTan

Please look again, I think your comments have been addressed.

@adityabavadekar

adityabavadekar commented Oct 20, 2026 •

Copy link
Copy Markdown
Author

@dbarker I have fix the GCC build issue and all of the tests are passing now. I've also updated the PR description to match latest changes. Please let me know if anything else is needed before this can be merged.
Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr:please-review This PR is ready for review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MeterProvider support for updating the MeterConfigurator

4 participants