-
Notifications
You must be signed in to change notification settings - Fork 783
New APIs to add/remove metric readers at run-time #4863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -580,3 +580,16 @@ def get_meter( | |
| self._measurement_consumer, | ||
| ) | ||
| return self._meters[info] | ||
|
|
||
| def add_metric_reader( | ||
| self, metric_reader: "opentelemetry.sdk.metrics.export.MetricReader" | ||
| ) -> None: | ||
| with self._lock: | ||
| self._measurement_consumer.add_metric_reader(metric_reader) | ||
|
|
||
| def remove_metric_reader( | ||
| self, | ||
| metric_reader: "opentelemetry.sdk.metrics.export.MetricReader", | ||
| ) -> None: | ||
| with self._lock: | ||
| self._measurement_consumer.remove_metric_reader(metric_reader) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| # pylint: disable=unused-import | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from logging import getLogger | ||
| from threading import Lock | ||
| from time import time_ns | ||
| from typing import Iterable, List, Mapping, Optional | ||
|
|
@@ -31,6 +32,8 @@ | |
| ) | ||
| from opentelemetry.sdk.metrics._internal.point import Metric | ||
|
|
||
| _logger = getLogger(__name__) | ||
|
|
||
|
|
||
| class MeasurementConsumer(ABC): | ||
| @abstractmethod | ||
|
|
@@ -143,3 +146,39 @@ def collect( | |
| result = self._reader_storages[metric_reader].collect() | ||
|
|
||
| return result | ||
|
|
||
| def add_metric_reader( | ||
| self, metric_reader: "opentelemetry.sdk.metrics.MetricReader" | ||
| ) -> None: | ||
| """Registers a new metric reader.""" | ||
| with self._lock: | ||
| if metric_reader in self._reader_storages: | ||
| _logger.warning("'%s' already registered!", metric_reader) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the pattern that is already established in the |
||
| self._sdk_config.metric_readers += type( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any suggestions for a better alternative? given the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will defer to other contributors/maintainers on this one. |
||
| self._sdk_config.metric_readers | ||
| )((metric_reader,)) | ||
| self._reader_storages[metric_reader] = MetricReaderStorage( | ||
| self._sdk_config, | ||
| metric_reader._instrument_class_temporality, | ||
| metric_reader._instrument_class_aggregation, | ||
| ) | ||
| metric_reader._set_collect_callback(self.collect) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
|
|
||
| def remove_metric_reader( | ||
| self, metric_reader: "opentelemetry.sdk.metrics.MetricReader" | ||
| ) -> None: | ||
| """Unregisters the given metric reader.""" | ||
| with self._lock: | ||
| if metric_reader not in self._reader_storages: | ||
| _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( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same deal here with the typing, we should not be doing this. |
||
| self._sdk_config.metric_readers | ||
| )( | ||
| ( | ||
| reader | ||
| for reader in self._sdk_config.metric_readers | ||
| if reader is not metric_reader | ||
| ) | ||
| ) | ||
There was a problem hiding this comment.
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_readersand that the callback is set here instead of in the measurement consumer.