From 5574f7ea6b63c0fd7f5b32be4b0ff030f29b9efe Mon Sep 17 00:00:00 2001 From: Lance Erickson Date: Tue, 22 Nov 2022 12:54:37 -0600 Subject: [PATCH] Avoid generator in _ViewInstrumentMatch.collect() (#3035) --- .github/workflows/test.yml | 2 +- CHANGELOG.md | 2 ++ CONTRIBUTING.md | 2 +- .../sdk/metrics/_internal/_view_instrument_match.py | 8 +++++--- opentelemetry-sdk/tests/metrics/test_metrics.py | 4 ++-- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a75f3576232..c4529615261 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ env: # Otherwise, set variable to the commit of your branch on # opentelemetry-python-contrib which is compatible with these Core repo # changes. - CONTRIB_REPO_SHA: c6134843900e2eeb1b8b3383a897b38cc0905c38 + CONTRIB_REPO_SHA: main # This is needed because we do not clone the core repo in contrib builds anymore. # When running contrib builds as part of core builds, we use actions/checkout@v2 which # does not set an environment variable (simply just runs tox), which is different when diff --git a/CHANGELOG.md b/CHANGELOG.md index 030a8a37452..624a41a467d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#3026](https://github.com/open-telemetry/opentelemetry-python/pull/3026)) - Add missing entry points for OTLP/HTTP exporter ([#3027](https://github.com/open-telemetry/opentelemetry-python/pull/3027)) +- Fix: Avoid generator in metrics _ViewInstrumentMatch.collect() + ([#3035](https://github.com/open-telemetry/opentelemetry-python/pull/3035) ## Version 1.14.0/0.35b0 (2022-11-04) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7ef351b38cb..0653deb60c4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -25,7 +25,7 @@ Please take a look at this list first, your contributions may belong in one of t # Find the right branch -The default branch for this repo is `main`. Changes that pertain to `metrics` go into the `metrics` branch. Any changes that pertain to components marked as `stable` in the [specifications](https://github.com/open-telemetry/opentelemetry-specification) or anything that is not `metrics` related go into this branch. +The default branch for this repo is `main`. All feature work is accomplished on branches from `main`. ## Find a Buddy and get Started Quickly! diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/_view_instrument_match.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/_view_instrument_match.py index 4cff25ae789..ab4645c82f3 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/_view_instrument_match.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/_view_instrument_match.py @@ -16,7 +16,7 @@ from logging import getLogger from threading import Lock from time import time_ns -from typing import Dict, Iterable +from typing import Dict, List, Sequence from opentelemetry.metrics import Instrument from opentelemetry.sdk.metrics._internal.aggregation import ( @@ -126,12 +126,14 @@ def collect( self, aggregation_temporality: AggregationTemporality, collection_start_nanos: int, - ) -> Iterable[DataPointT]: + ) -> Sequence[DataPointT]: + data_points: List[DataPointT] = [] with self._lock: for aggregation in self._attributes_aggregation.values(): data_point = aggregation.collect( aggregation_temporality, collection_start_nanos ) if data_point is not None: - yield data_point + data_points.append(data_point) + return data_points diff --git a/opentelemetry-sdk/tests/metrics/test_metrics.py b/opentelemetry-sdk/tests/metrics/test_metrics.py index b128456531b..13efbb91c0d 100644 --- a/opentelemetry-sdk/tests/metrics/test_metrics.py +++ b/opentelemetry-sdk/tests/metrics/test_metrics.py @@ -514,11 +514,11 @@ def test_duplicate_instrument_aggregate_data(self): self.assertEqual(metric_0.name, "counter") self.assertEqual(metric_0.unit, "unit") self.assertEqual(metric_0.description, "description") - self.assertEqual(next(metric_0.data.data_points).value, 3) + self.assertEqual(next(iter(metric_0.data.data_points)).value, 3) metric_1 = scope_metrics[1].metrics[0] self.assertEqual(metric_1.name, "counter") self.assertEqual(metric_1.unit, "unit") self.assertEqual(metric_1.description, "description") - self.assertEqual(next(metric_1.data.data_points).value, 7) + self.assertEqual(next(iter(metric_1.data.data_points)).value, 7)