Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c9e40c3

Browse files
srikanthccvAlex Boten
andauthored
Add temporality conversion for histogram (open-telemetry#2445)
* Add temporality conversion for histogram * Add tests and update histogram point * point->count * Fix lint Co-authored-by: Alex Boten <[email protected]>
1 parent 672c449 commit c9e40c3

File tree

2 files changed

+208
-1
lines changed

2 files changed

+208
-1
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/_metrics/aggregation.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ def collect(self) -> Histogram:
193193
)
194194

195195

196+
# pylint: disable=too-many-return-statements,too-many-branches
196197
def _convert_aggregation_temporality(
197198
previous_point: Optional[_PointVarT],
198199
current_point: _PointVarT,
@@ -267,4 +268,46 @@ def _convert_aggregation_temporality(
267268
is_monotonic=is_monotonic,
268269
)
269270

271+
if current_point_type is Histogram:
272+
if previous_point is None:
273+
return replace(
274+
current_point, aggregation_temporality=aggregation_temporality
275+
)
276+
if previous_point.aggregation_temporality is not (
277+
AggregationTemporality.CUMULATIVE
278+
):
279+
raise Exception(
280+
"previous_point aggregation temporality must be CUMULATIVE"
281+
)
282+
283+
if current_point.aggregation_temporality is aggregation_temporality:
284+
return current_point
285+
286+
if aggregation_temporality is AggregationTemporality.CUMULATIVE:
287+
start_time_unix_nano = previous_point.start_time_unix_nano
288+
sum_ = current_point.sum + previous_point.sum
289+
bucket_counts = [
290+
curr_count + prev_count
291+
for curr_count, prev_count in zip(
292+
current_point.bucket_counts, previous_point.bucket_counts
293+
)
294+
]
295+
else:
296+
start_time_unix_nano = previous_point.time_unix_nano
297+
sum_ = current_point.sum - previous_point.sum
298+
bucket_counts = [
299+
curr_count - prev_count
300+
for curr_count, prev_count in zip(
301+
current_point.bucket_counts, previous_point.bucket_counts
302+
)
303+
]
304+
305+
return Histogram(
306+
start_time_unix_nano=start_time_unix_nano,
307+
time_unix_nano=current_point.time_unix_nano,
308+
bucket_counts=bucket_counts,
309+
explicit_bounds=current_point.explicit_bounds,
310+
sum=sum_,
311+
aggregation_temporality=aggregation_temporality,
312+
)
270313
return None

opentelemetry-sdk/tests/metrics/test_aggregation.py

Lines changed: 165 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
_convert_aggregation_temporality,
2727
)
2828
from opentelemetry.sdk._metrics.measurement import Measurement
29-
from opentelemetry.sdk._metrics.point import Gauge, Sum
29+
from opentelemetry.sdk._metrics.point import Gauge, Histogram, Sum
3030

3131

3232
class TestSynchronousSumAggregation(TestCase):
@@ -576,3 +576,167 @@ def test_current_point_gauge(self):
576576
),
577577
current_point,
578578
)
579+
580+
581+
class TestHistogramConvertAggregationTemporality(TestCase):
582+
def test_previous_point_none(self):
583+
584+
current_point = Histogram(
585+
start_time_unix_nano=0,
586+
time_unix_nano=1,
587+
bucket_counts=[0, 2, 1, 2, 0],
588+
explicit_bounds=[0, 5, 10, 25],
589+
sum=70,
590+
aggregation_temporality=AggregationTemporality.DELTA,
591+
)
592+
593+
self.assertEqual(
594+
_convert_aggregation_temporality(
595+
None, current_point, AggregationTemporality.CUMULATIVE
596+
),
597+
replace(
598+
current_point,
599+
aggregation_temporality=AggregationTemporality.CUMULATIVE,
600+
),
601+
)
602+
603+
def test_previous_point_non_cumulative(self):
604+
605+
with self.assertRaises(Exception):
606+
607+
_convert_aggregation_temporality(
608+
Histogram(
609+
start_time_unix_nano=0,
610+
time_unix_nano=1,
611+
bucket_counts=[0, 2, 1, 2, 0],
612+
explicit_bounds=[0, 5, 10, 25],
613+
sum=70,
614+
aggregation_temporality=AggregationTemporality.DELTA,
615+
),
616+
Histogram(
617+
start_time_unix_nano=1,
618+
time_unix_nano=2,
619+
bucket_counts=[0, 1, 3, 0, 0],
620+
explicit_bounds=[0, 5, 10, 25],
621+
sum=35,
622+
aggregation_temporality=AggregationTemporality.DELTA,
623+
),
624+
AggregationTemporality.DELTA,
625+
),
626+
627+
def test_same_aggregation_temporality_cumulative(self):
628+
current_point = Histogram(
629+
start_time_unix_nano=0,
630+
time_unix_nano=2,
631+
bucket_counts=[0, 3, 4, 2, 0],
632+
explicit_bounds=[0, 5, 10, 25],
633+
sum=105,
634+
aggregation_temporality=AggregationTemporality.CUMULATIVE,
635+
)
636+
self.assertEqual(
637+
_convert_aggregation_temporality(
638+
Histogram(
639+
start_time_unix_nano=0,
640+
time_unix_nano=1,
641+
bucket_counts=[0, 2, 1, 2, 0],
642+
explicit_bounds=[0, 5, 10, 25],
643+
sum=70,
644+
aggregation_temporality=AggregationTemporality.CUMULATIVE,
645+
),
646+
current_point,
647+
AggregationTemporality.CUMULATIVE,
648+
),
649+
current_point,
650+
)
651+
652+
def test_same_aggregation_temporality_delta(self):
653+
current_point = Histogram(
654+
start_time_unix_nano=1,
655+
time_unix_nano=2,
656+
bucket_counts=[0, 1, 3, 0, 0],
657+
explicit_bounds=[0, 5, 10, 25],
658+
sum=35,
659+
aggregation_temporality=AggregationTemporality.DELTA,
660+
)
661+
662+
self.assertEqual(
663+
_convert_aggregation_temporality(
664+
Histogram(
665+
start_time_unix_nano=0,
666+
time_unix_nano=2,
667+
bucket_counts=[0, 3, 4, 2, 0],
668+
explicit_bounds=[0, 5, 10, 25],
669+
sum=105,
670+
aggregation_temporality=AggregationTemporality.CUMULATIVE,
671+
),
672+
current_point,
673+
AggregationTemporality.DELTA,
674+
),
675+
current_point,
676+
)
677+
678+
def test_aggregation_temporality_to_cumulative(self):
679+
current_point = Histogram(
680+
start_time_unix_nano=1,
681+
time_unix_nano=2,
682+
bucket_counts=[0, 1, 3, 0, 0],
683+
explicit_bounds=[0, 5, 10, 25],
684+
sum=35,
685+
aggregation_temporality=AggregationTemporality.DELTA,
686+
)
687+
688+
self.assertEqual(
689+
_convert_aggregation_temporality(
690+
Histogram(
691+
start_time_unix_nano=0,
692+
time_unix_nano=1,
693+
bucket_counts=[0, 2, 1, 2, 0],
694+
explicit_bounds=[0, 5, 10, 25],
695+
sum=70,
696+
aggregation_temporality=AggregationTemporality.CUMULATIVE,
697+
),
698+
current_point,
699+
AggregationTemporality.CUMULATIVE,
700+
),
701+
Histogram(
702+
start_time_unix_nano=0,
703+
time_unix_nano=2,
704+
bucket_counts=[0, 3, 4, 2, 0],
705+
explicit_bounds=[0, 5, 10, 25],
706+
sum=105,
707+
aggregation_temporality=AggregationTemporality.CUMULATIVE,
708+
),
709+
)
710+
711+
def test_aggregation_temporality_to_delta(self):
712+
current_point = Histogram(
713+
start_time_unix_nano=0,
714+
time_unix_nano=2,
715+
bucket_counts=[0, 3, 4, 2, 0],
716+
explicit_bounds=[0, 5, 10, 25],
717+
sum=105,
718+
aggregation_temporality=AggregationTemporality.CUMULATIVE,
719+
)
720+
721+
self.assertEqual(
722+
_convert_aggregation_temporality(
723+
Histogram(
724+
start_time_unix_nano=0,
725+
time_unix_nano=1,
726+
bucket_counts=[0, 2, 1, 2, 0],
727+
explicit_bounds=[0, 5, 10, 25],
728+
sum=70,
729+
aggregation_temporality=AggregationTemporality.CUMULATIVE,
730+
),
731+
current_point,
732+
AggregationTemporality.DELTA,
733+
),
734+
Histogram(
735+
start_time_unix_nano=1,
736+
time_unix_nano=2,
737+
bucket_counts=[0, 1, 3, 0, 0],
738+
explicit_bounds=[0, 5, 10, 25],
739+
sum=35,
740+
aggregation_temporality=AggregationTemporality.DELTA,
741+
),
742+
)

0 commit comments

Comments
 (0)