Skip to content

Conversation

@JP-MY
Copy link

@JP-MY JP-MY commented Dec 18, 2025

Description

This change adds two public functions to MeterProvider that allow registering and deleting metric readers at run-time.

Fixes #4818

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

  • It has been tested via new unit test added to ensure correct behavior for addition and removal of metric readers at run-time.

Does This PR Require a Contrib Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@JP-MY JP-MY requested a review from a team as a code owner December 18, 2025 14:20
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Dec 18, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: JP-MY / name: JP-MY (023dcf8)

Removed return values
@JP-MY
Copy link
Author

JP-MY commented Dec 18, 2025

One thing to note is that the type of metric readers in sdk config is not very well defined (sequence) and is up to the users to decide; this makes it tricky to add or remove elements without checking for all known types (list, tuple, etc.). As a compromise I used type to automatically convert a tuple back to the type provided by the user and as long as that type supports the + operator (which both lists and tuples do) the logic works but if there is a custom sequence type defined by users which doesn't define __add__ magic method, this code can technically break.

metric_reader._instrument_class_temporality,
metric_reader._instrument_class_aggregation,
)
metric_reader._set_collect_callback(self.collect)

Choose a reason for hiding this comment

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

This _set_collect_callback call should ideally be made in the MeterProvider class.

with self._lock:
if metric_reader in self._reader_storages:
_logger.warning("'%s' already registered!", metric_reader)
self._sdk_config.metric_readers += type(

Choose a reason for hiding this comment

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

This really isn't a good idea, we don't know that the type of self._sdk_config.metric_readers will even have a constructor that accepts an iterable.

Copy link
Author

@JP-MY JP-MY Jan 22, 2026

Choose a reason for hiding this comment

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

Any suggestions for a better alternative? given the Sequence type, this is the best I could come up with. It'd be better if the type is explicitly defined as something like Tuple | List but that would be a breaking API change

Copy link

@herin049 herin049 Jan 22, 2026

Choose a reason for hiding this comment

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

I would recommend we remove the metric readers attribute entirely from that configuration object and let the measurement consumer accept a sequence of metric readers, allowing it to store them in whichever data structure it wants.

Choose a reason for hiding this comment

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

I will defer to other contributors/maintainers on this one.

Comment on lines +587 to +588
with self._lock:
self._measurement_consumer.add_metric_reader(metric_reader)

Choose a reason for hiding this comment

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

We need to make sure the reader is properly added to _all_metric_readers and that the callback is set here instead of in the measurement consumer.

metric_reader: "opentelemetry.sdk.metrics.export.MetricReader",
) -> None:
with self._lock:
self._measurement_consumer.remove_metric_reader(metric_reader)

Choose a reason for hiding this comment

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

Similar to above, we need to make sure this reader is removed from _all_metric_readers. Furthermore, we will want to make sure that the reader is properly shutdown to prevent resource leaks.

_logger.warning("'%s' has not been registered!", metric_reader)
self._reader_storages.pop(metric_reader, None)
metric_reader._set_collect_callback(None)
self._sdk_config.metric_readers = type(

Choose a reason for hiding this comment

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

Same deal here with the typing, we should not be doing this.

)
),
resource=resource,
metric_readers=metric_readers,

Choose a reason for hiding this comment

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

To avoid some of the issues I presented, I would be in favor of removing the metric readers from the SdkConfiguration object entirely and instead make sure we pass metric readers explicitly to downstream components. @xrmx any thoughts here?

"""Registers a new metric reader."""
with self._lock:
if metric_reader in self._reader_storages:
_logger.warning("'%s' already registered!", metric_reader)

Choose a reason for hiding this comment

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

This should not be a warning, this really should through an exception or at the very least return early.

Copy link
Author

Choose a reason for hiding this comment

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

exceptions can be more problematic so I think an early return makes more sense. Will update the PR.

Choose a reason for hiding this comment

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

This is the pattern that is already established in the MeterProvider constructor. I figure that for consistency, the behavior should be the same in both places.

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.

Exposing public APIs to for addition and removal of metric readers at run-time

2 participants