From bf0aae6f44ed4687bb742f5c7b072115013088b2 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Fri, 14 Jan 2022 21:32:41 -0600 Subject: [PATCH] Add aggregation temporality conversion algorithm Fixes #2329 --- .../opentelemetry/sdk/_metrics/aggregation.py | 13 +- .../tests/metrics/test_aggregation.py | 115 ++++++++++++++++-- 2 files changed, 109 insertions(+), 19 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/aggregation.py b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/aggregation.py index da37ed4e456..9f6e5086a6c 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 @@ -206,7 +206,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( @@ -215,7 +214,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, ) @@ -242,12 +241,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 5896fa73cef..cc71b9c6702 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,18 +323,110 @@ 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(0, 0, 0, AggregationTemporality.DELTA, 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(0, 0, 0, AggregationTemporality.DELTA, False) self.assertEqual( _convert_aggregation_temporality( - None, + Sum(0, 0, 0, AggregationTemporality.DELTA, False), current_point, - AggregationTemporality.CUMULATIVE + AggregationTemporality.DELTA, ), - replace( + current_point, + ) + + current_point = Sum(0, 0, 0, AggregationTemporality.CUMULATIVE, False) + + self.assertEqual( + _convert_aggregation_temporality( + Sum(0, 0, 0, AggregationTemporality.CUMULATIVE, 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(1, 2, 3, AggregationTemporality.CUMULATIVE, False), + Sum(4, 5, 6, AggregationTemporality.CUMULATIVE, False), + AggregationTemporality.DELTA, + ), + Sum(1, 5, 3, AggregationTemporality.DELTA, False), + ) + + self.assertEqual( + _convert_aggregation_temporality( + Sum(1, 2, 3, AggregationTemporality.CUMULATIVE, True), + Sum(4, 5, 6, AggregationTemporality.CUMULATIVE, False), + AggregationTemporality.DELTA, + ), + Sum(1, 5, 3, AggregationTemporality.DELTA, False), + ) + + self.assertEqual( + _convert_aggregation_temporality( + Sum(1, 2, 3, AggregationTemporality.CUMULATIVE, True), + Sum(4, 5, 6, AggregationTemporality.CUMULATIVE, True), + AggregationTemporality.DELTA, + ), + Sum(1, 5, 3, AggregationTemporality.DELTA, True), + ) + + def test_current_point_sum_aggregation_temporality_cumulative(self): + + self.assertEqual( + _convert_aggregation_temporality( + Sum(1, 2, 3, AggregationTemporality.DELTA, False), + Sum(4, 5, 6, AggregationTemporality.DELTA, False), + AggregationTemporality.CUMULATIVE, + ), + Sum(1, 5, 9, AggregationTemporality.CUMULATIVE, False), + ) + + self.assertEqual( + _convert_aggregation_temporality( + Sum(1, 2, 3, AggregationTemporality.DELTA, True), + Sum(4, 5, 6, AggregationTemporality.DELTA, False), + AggregationTemporality.CUMULATIVE, + ), + Sum(1, 5, 9, AggregationTemporality.CUMULATIVE, False), + ) + + self.assertEqual( + _convert_aggregation_temporality( + Sum(1, 2, 3, AggregationTemporality.DELTA, True), + Sum(4, 5, 6, AggregationTemporality.DELTA, True), + AggregationTemporality.CUMULATIVE, + ), + Sum(1, 5, 9, AggregationTemporality.CUMULATIVE, True), + ) + + def test_current_point_gauge(self): + + current_point = (Gauge(0, 0),) + self.assertEqual( + _convert_aggregation_temporality( + Sum(1, 2, 3, AggregationTemporality.DELTA, True), + current_point, + AggregationTemporality.CUMULATIVE, + ), + current_point, )