-
Notifications
You must be signed in to change notification settings - Fork 950
opentelemetry-sdk: add support for metric producers #5419
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| `opentelemetry-sdk`: add support for the `MetricProducer` interface |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
|
|
||
| from abc import ABC, abstractmethod | ||
| from collections.abc import Iterable, Mapping | ||
| from logging import getLogger | ||
| from threading import Lock | ||
| from time import time_ns | ||
|
|
||
|
|
@@ -17,7 +18,14 @@ | |
| from opentelemetry.sdk.metrics._internal.metric_reader_storage import ( | ||
| MetricReaderStorage, | ||
| ) | ||
| from opentelemetry.sdk.metrics._internal.point import MetricsData | ||
| from opentelemetry.sdk.metrics._internal.point import ( | ||
| MetricsData, | ||
| ResourceMetrics, | ||
| ScopeMetrics, | ||
| ) | ||
| from opentelemetry.sdk.resources import Resource | ||
|
|
||
| _logger = getLogger(__name__) | ||
|
|
||
|
|
||
| class MeasurementConsumer(ABC): | ||
|
|
@@ -133,8 +141,83 @@ def collect( | |
|
|
||
| result = self._reader_storages[metric_reader].collect() | ||
|
|
||
| if producer_scope_metrics := self._collect_from_producers( | ||
| metric_reader, deadline_ns | ||
| ): | ||
| result = self._merge_producer_metrics( | ||
| result, producer_scope_metrics, self._sdk_config.resource | ||
| ) | ||
|
|
||
| return result | ||
|
|
||
| @staticmethod | ||
| def _collect_from_producers( | ||
|
Contributor
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. I have opted to treat errors (including timeouts) surfaced from metric producers as "soft" errors to prevent potential disruptions to the standard SDK metric collection process. There doesn't seem to be a consensus on how to handle this scenario across SDK implementations (C++ roughly uses the approach here, whereas Java/Go halt the entire collection process). I'm open to suggestions and additional comments here. |
||
| metric_reader: "opentelemetry.sdk.metrics.export.MetricReader", | ||
| deadline_ns: float, | ||
| ) -> list[ScopeMetrics]: | ||
| """Collect ScopeMetrics from the reader's MetricProducers. | ||
|
|
||
| Called while holding ``self._lock`` so ``produce()`` calls are | ||
| serialized. A producer that fails or times out is isolated so | ||
| metrics collected by the SDK are not dropped. | ||
| """ | ||
| producer_scope_metrics: list[ScopeMetrics] = [] | ||
| # pylint: disable-next=protected-access | ||
| for producer in metric_reader._metric_producers: | ||
| remaining_millis = (deadline_ns - time_ns()) / 1e6 | ||
| if remaining_millis <= 0: | ||
| _logger.warning( | ||
| "Timed out collecting from metric producers, " | ||
| "skipping remaining producers." | ||
| ) | ||
| break | ||
|
|
||
| try: | ||
| scopes = list( | ||
| producer.produce(timeout_millis=remaining_millis) | ||
| ) | ||
| # pylint: disable-next=broad-except | ||
| except Exception: | ||
| _logger.exception( | ||
| "Metric producer %s failed to produce metrics, skipping.", | ||
| producer, | ||
| ) | ||
| continue | ||
|
|
||
| producer_scope_metrics.extend(scopes) | ||
|
|
||
| return producer_scope_metrics | ||
|
|
||
| @staticmethod | ||
| def _merge_producer_metrics( | ||
| result: MetricsData | None, | ||
| producer_scope_metrics: list[ScopeMetrics], | ||
| resource: Resource, | ||
| ) -> MetricsData: | ||
| if result is not None and result.resource_metrics: | ||
| sdk_resource_metrics = result.resource_metrics[0] | ||
| merged = ResourceMetrics( | ||
| resource=sdk_resource_metrics.resource, | ||
| scope_metrics=[ | ||
| *sdk_resource_metrics.scope_metrics, | ||
| *producer_scope_metrics, | ||
| ], | ||
| schema_url=sdk_resource_metrics.schema_url, | ||
| ) | ||
| return MetricsData( | ||
| resource_metrics=[merged, *result.resource_metrics[1:]] | ||
| ) | ||
|
|
||
| return MetricsData( | ||
| resource_metrics=[ | ||
| ResourceMetrics( | ||
| resource=resource, | ||
| scope_metrics=producer_scope_metrics, | ||
| schema_url=resource.schema_url, | ||
| ) | ||
| ] | ||
| ) | ||
|
|
||
| def add_metric_reader( | ||
| self, metric_reader: "opentelemetry.sdk.metrics.MetricReader" | ||
| ) -> None: | ||
|
|
||
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.
no
metric_filterparam ? https://opentelemetry.io/docs/specs/otel/metrics/sdk/#produce-batchThere 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.
it also says
If the batch of Metric Points includes resource information, Produce SHOULD require a resource as a parameter. Produce does not have any other required parameters, however, OpenTelemetry SDK authors MAY choose to add required or optional parameters (e.g. timeout).
so should there be a
resourceparam ?Uh oh!
There was an error while loading. Please reload this page.
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.
Going to add this in a follow-up.
There should only be a
Resourceparam if the batch of metric datapoints include resource information. In our case, we expectproduce()to yield a batch ofScopeMetricswhich do not contain resource information, so we don't need to pass this parameter. The creation of the correspondingResourceobject is handled automatically by the SDK in_merge_producer_metrics()which is why it's not needed on this method.