diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/aggregation.py b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/aggregation.py index b2a4ef00df8..3aabb02b4b0 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/aggregation.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/aggregation.py @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -from dataclasses import replace from abc import ABC, abstractmethod from bisect import bisect_left +from dataclasses import replace from logging import getLogger from math import inf from threading import Lock @@ -209,7 +209,6 @@ def _convert_aggregation_temporality( aggregation_temporality: int, ) -> _PointVarT: - previous_point_type = type(previous_point) current_point_type = type(current_point) if previous_point is not None and type(previous_point) is not type( @@ -218,7 +217,7 @@ def _convert_aggregation_temporality( _logger.warning( "convert_aggregation_temporality called with mismatched " "point types: %s and %s", - previous_point_type, + type(previous_point), current_point_type, ) @@ -245,12 +244,14 @@ def _convert_aggregation_temporality( ) return Sum( - aggregation_temporality=aggregation_temporality, - is_monotonic=is_monotonic, start_time_unix_nano=previous_point.start_time_unix_nano, time_unix_nano=current_point.time_unix_nano, value=value, + aggregation_temporality=aggregation_temporality, + is_monotonic=is_monotonic, ) - elif current_point_type is Gauge: + if current_point_type is Gauge: return current_point + + return None diff --git a/opentelemetry-sdk/tests/metrics/test_aggregation.py b/opentelemetry-sdk/tests/metrics/test_aggregation.py index eb970630f9d..c9d6a8ac224 100644 --- a/opentelemetry-sdk/tests/metrics/test_aggregation.py +++ b/opentelemetry-sdk/tests/metrics/test_aggregation.py @@ -18,19 +18,16 @@ from unittest import TestCase from opentelemetry.sdk._metrics.aggregation import ( + AggregationTemporality, AsynchronousSumAggregation, ExplicitBucketHistogramAggregation, LastValueAggregation, SynchronousSumAggregation, + _convert_aggregation_temporality, _InstrumentMonotonicityAwareAggregation, - _convert_aggregation_temporality ) -from opentelemetry.sdk._metrics.aggregation import AggregationTemporality from opentelemetry.sdk._metrics.measurement import Measurement -from opentelemetry.sdk._metrics.point import ( - Gauge, - Sum, -) +from opentelemetry.sdk._metrics.point import Gauge, Sum class TestSynchronousSumAggregation(TestCase): @@ -326,7 +323,13 @@ class TestConvertAggregationTemporality(TestCase): def test_mismatched_point_types(self): - current_point = Sum(0, 0, 0, AggregationTemporality.DELTA, False) + current_point = Sum( + start_time_unix_nano=0, + time_unix_nano=0, + value=0, + aggregation_temporality=AggregationTemporality.DELTA, + is_monotonic=False, + ) self.assertIs( _convert_aggregation_temporality( @@ -337,18 +340,254 @@ def test_mismatched_point_types(self): def test_current_point_sum_previous_point_none(self): - point_arguments = (0, 0, 0, AggregationTemporality.DELTA, False) + current_point = Sum( + start_time_unix_nano=0, + time_unix_nano=0, + value=0, + aggregation_temporality=AggregationTemporality.DELTA, + is_monotonic=False, + ) + + self.assertEqual( + _convert_aggregation_temporality( + None, current_point, AggregationTemporality.CUMULATIVE + ), + replace( + current_point, + aggregation_temporality=AggregationTemporality.CUMULATIVE, + ), + ) + + def test_current_point_sum_current_point_same_aggregation_temporality( + self, + ): - current_point = Sum(*point_arguments) + current_point = Sum( + start_time_unix_nano=0, + time_unix_nano=0, + value=0, + aggregation_temporality=AggregationTemporality.DELTA, + is_monotonic=False, + ) self.assertEqual( _convert_aggregation_temporality( - None, + Sum( + start_time_unix_nano=0, + time_unix_nano=0, + value=0, + aggregation_temporality=AggregationTemporality.DELTA, + is_monotonic=False, + ), current_point, - AggregationTemporality.CUMULATIVE + AggregationTemporality.DELTA, ), - replace( + current_point, + ) + + current_point = Sum( + start_time_unix_nano=0, + time_unix_nano=0, + value=0, + aggregation_temporality=AggregationTemporality.CUMULATIVE, + is_monotonic=False, + ) + + self.assertEqual( + _convert_aggregation_temporality( + Sum( + start_time_unix_nano=0, + time_unix_nano=0, + value=0, + aggregation_temporality=AggregationTemporality.CUMULATIVE, + is_monotonic=False, + ), current_point, - aggregation_temporality=AggregationTemporality.CUMULATIVE - ) + AggregationTemporality.CUMULATIVE, + ), + current_point, + ) + + def test_current_point_sum_aggregation_temporality_delta(self): + + self.assertEqual( + _convert_aggregation_temporality( + Sum( + start_time_unix_nano=1, + time_unix_nano=2, + value=3, + aggregation_temporality=AggregationTemporality.CUMULATIVE, + is_monotonic=False, + ), + Sum( + start_time_unix_nano=4, + time_unix_nano=5, + value=6, + aggregation_temporality=AggregationTemporality.CUMULATIVE, + is_monotonic=False, + ), + AggregationTemporality.DELTA, + ), + Sum( + start_time_unix_nano=1, + time_unix_nano=5, + value=3, + aggregation_temporality=AggregationTemporality.DELTA, + is_monotonic=False, + ), + ) + + self.assertEqual( + _convert_aggregation_temporality( + Sum( + start_time_unix_nano=1, + time_unix_nano=2, + value=3, + aggregation_temporality=AggregationTemporality.CUMULATIVE, + is_monotonic=True, + ), + Sum( + start_time_unix_nano=4, + time_unix_nano=5, + value=6, + aggregation_temporality=AggregationTemporality.CUMULATIVE, + is_monotonic=False, + ), + AggregationTemporality.DELTA, + ), + Sum( + start_time_unix_nano=1, + time_unix_nano=5, + value=3, + aggregation_temporality=AggregationTemporality.DELTA, + is_monotonic=False, + ), + ) + + self.assertEqual( + _convert_aggregation_temporality( + Sum( + start_time_unix_nano=1, + time_unix_nano=2, + value=3, + aggregation_temporality=AggregationTemporality.CUMULATIVE, + is_monotonic=True, + ), + Sum( + start_time_unix_nano=4, + time_unix_nano=5, + value=6, + aggregation_temporality=AggregationTemporality.CUMULATIVE, + is_monotonic=True, + ), + AggregationTemporality.DELTA, + ), + Sum( + start_time_unix_nano=1, + time_unix_nano=5, + value=3, + aggregation_temporality=AggregationTemporality.DELTA, + is_monotonic=True, + ), + ) + + def test_current_point_sum_aggregation_temporality_cumulative(self): + + self.assertEqual( + _convert_aggregation_temporality( + Sum( + start_time_unix_nano=1, + time_unix_nano=2, + value=3, + aggregation_temporality=AggregationTemporality.DELTA, + is_monotonic=False, + ), + Sum( + start_time_unix_nano=4, + time_unix_nano=5, + value=6, + aggregation_temporality=AggregationTemporality.DELTA, + is_monotonic=False, + ), + AggregationTemporality.CUMULATIVE, + ), + Sum( + start_time_unix_nano=1, + time_unix_nano=5, + value=9, + aggregation_temporality=AggregationTemporality.CUMULATIVE, + is_monotonic=False, + ), + ) + + self.assertEqual( + _convert_aggregation_temporality( + Sum( + start_time_unix_nano=1, + time_unix_nano=2, + value=3, + aggregation_temporality=AggregationTemporality.DELTA, + is_monotonic=True, + ), + Sum( + start_time_unix_nano=4, + time_unix_nano=5, + value=6, + aggregation_temporality=AggregationTemporality.DELTA, + is_monotonic=False, + ), + AggregationTemporality.CUMULATIVE, + ), + Sum( + start_time_unix_nano=1, + time_unix_nano=5, + value=9, + aggregation_temporality=AggregationTemporality.CUMULATIVE, + is_monotonic=False, + ), + ) + + self.assertEqual( + _convert_aggregation_temporality( + Sum( + start_time_unix_nano=1, + time_unix_nano=2, + value=3, + aggregation_temporality=AggregationTemporality.DELTA, + is_monotonic=True, + ), + Sum( + start_time_unix_nano=4, + time_unix_nano=5, + value=6, + aggregation_temporality=AggregationTemporality.DELTA, + is_monotonic=True, + ), + AggregationTemporality.CUMULATIVE, + ), + Sum( + start_time_unix_nano=1, + time_unix_nano=5, + value=9, + aggregation_temporality=AggregationTemporality.CUMULATIVE, + is_monotonic=True, + ), + ) + + def test_current_point_gauge(self): + + current_point = (Gauge(0, 0),) + self.assertEqual( + _convert_aggregation_temporality( + Sum( + start_time_unix_nano=1, + time_unix_nano=2, + value=3, + aggregation_temporality=AggregationTemporality.DELTA, + is_monotonic=True, + ), + current_point, + AggregationTemporality.CUMULATIVE, + ), + current_point, )