Skip to content

Commit

Permalink
Add aggregation temporality conversion algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Jan 15, 2022
1 parent ab285b8 commit 2b4d728
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 17 deletions.
10 changes: 6 additions & 4 deletions opentelemetry-sdk/src/opentelemetry/sdk/_metrics/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -242,12 +242,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
115 changes: 102 additions & 13 deletions opentelemetry-sdk/tests/metrics/test_aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
)

0 comments on commit 2b4d728

Please sign in to comment.