Skip to content

Commit 0eb5099

Browse files
committed
Test min, max and sum
1 parent 947d559 commit 0eb5099

File tree

3 files changed

+35
-80
lines changed

3 files changed

+35
-80
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,9 @@ def __init__(
414414

415415
self._previous_collection_start_nano = self._start_time_unix_nano
416416
self._previous_cumulative_value = self._get_empty_bucket_counts()
417+
self._previous_min = inf
418+
self._previous_max = -inf
419+
self._previous_sum = 0
417420

418421
def _get_empty_bucket_counts(self) -> List[int]:
419422
return [0] * (len(self._boundaries) + 1)
@@ -496,71 +499,22 @@ def collect(
496499
previous_cumulative_value_element,
497500
) in zip(current_value, self._previous_cumulative_value)
498501
]
502+
self._previous_min = min(min_, self._previous_min)
503+
self._previous_max = max(max_, self._previous_max)
504+
self._previous_sum = sum_ + self._previous_sum
499505

500506
return HistogramDataPoint(
501507
attributes=self._attributes,
502508
start_time_unix_nano=self._start_time_unix_nano,
503509
time_unix_nano=collection_start_nano,
504510
count=sum(self._previous_cumulative_value),
505-
sum=sum_,
511+
sum=self._previous_sum,
506512
bucket_counts=tuple(self._previous_cumulative_value),
507513
explicit_bounds=self._boundaries,
508-
min=min_,
509-
max=max_,
514+
min=self._previous_min,
515+
max=self._previous_max,
510516
)
511517

512-
# This happens when the corresponding instrument for this
513-
# aggregation is asynchronous.
514-
515-
if current_value is None:
516-
# This happens when the corresponding instrument callback
517-
# does not produce measurements.
518-
return None
519-
520-
if (
521-
collection_aggregation_temporality
522-
is AggregationTemporality.DELTA
523-
):
524-
525-
result_value = [
526-
current_value_element - previous_cumulative_value_element
527-
for (
528-
current_value_element,
529-
previous_cumulative_value_element,
530-
) in zip(current_value, self._previous_cumulative_value)
531-
]
532-
533-
self._previous_cumulative_value = current_value
534-
535-
previous_collection_start_nano = (
536-
self._previous_collection_start_nano
537-
)
538-
self._previous_collection_start_nano = collection_start_nano
539-
540-
return HistogramDataPoint(
541-
attributes=self._attributes,
542-
start_time_unix_nano=previous_collection_start_nano,
543-
time_unix_nano=collection_start_nano,
544-
count=sum(result_value),
545-
sum=sum_,
546-
bucket_counts=tuple(result_value),
547-
explicit_bounds=self._boundaries,
548-
min=min_,
549-
max=max_,
550-
)
551-
552-
return HistogramDataPoint(
553-
attributes=self._attributes,
554-
start_time_unix_nano=self._start_time_unix_nano,
555-
time_unix_nano=collection_start_nano,
556-
count=sum(current_value),
557-
sum=sum_,
558-
bucket_counts=tuple(current_value),
559-
explicit_bounds=self._boundaries,
560-
min=min_,
561-
max=max_,
562-
)
563-
564518

565519
# pylint: disable=protected-access
566520
class _ExponentialBucketHistogramAggregation(_Aggregation[HistogramPoint]):

opentelemetry-sdk/tests/metrics/integration_test/test_explicit_bucket_histogram_aggregation.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727

2828
class TestExplicitBucketHistogramAggregation(TestCase):
2929

30-
def setUp(self):
31-
self.test_values = iter([1, 6, 11, 26, 51, 76, 101, 251, 501, 751])
30+
test_values = [1, 6, 11, 26, 51, 76, 101, 251, 501, 751]
3231

3332
@mark.skipif(
3433
system() != "Linux",
@@ -67,38 +66,29 @@ def test_synchronous_delta_temporality(self):
6766
histogram.record(test_value)
6867
results.append(reader.get_metrics_data())
6968

70-
previous_time_unix_nano = (
69+
metric_data = (
7170
results[0]
7271
.resource_metrics[0]
7372
.scope_metrics[0]
7473
.metrics[0]
75-
.data.data_points[0]
76-
.time_unix_nano
74+
.data.
75+
data_points[0]
7776
)
7877

78+
previous_time_unix_nano = metric_data.time_unix_nano
79+
7980
self.assertEqual(
80-
(
81-
results[0]
82-
.resource_metrics[0]
83-
.scope_metrics[0]
84-
.metrics[0]
85-
.data.data_points[0]
86-
.bucket_counts
87-
),
81+
metric_data.bucket_counts,
8882
(0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
8983
)
9084

9185
self.assertLess(
92-
(
93-
results[0]
94-
.resource_metrics[0]
95-
.scope_metrics[0]
96-
.metrics[0]
97-
.data.data_points[0]
98-
.start_time_unix_nano
99-
),
86+
metric_data.start_time_unix_nano,
10087
previous_time_unix_nano,
10188
)
89+
self.assertEqual(metric_data.min, self.test_values[0])
90+
self.assertEqual(metric_data.max, self.test_values[0])
91+
self.assertEqual(metric_data.sum, self.test_values[0])
10292

10393
for index, metrics_data in enumerate(results[1:]):
10494
metric_data = (
@@ -125,6 +115,9 @@ def test_synchronous_delta_temporality(self):
125115
self.assertLess(
126116
metric_data.start_time_unix_nano, metric_data.time_unix_nano
127117
)
118+
self.assertEqual(metric_data.min, self.test_values[index + 1])
119+
self.assertEqual(metric_data.max, self.test_values[index + 1])
120+
self.assertEqual(metric_data.sum, self.test_values[index + 1])
128121

129122
results = []
130123

@@ -205,6 +198,11 @@ def test_synchronous_cumulative_temporality(self):
205198
]
206199
)
207200
)
201+
self.assertEqual(metric_data.min, self.test_values[0])
202+
self.assertEqual(metric_data.max, self.test_values[index])
203+
self.assertEqual(
204+
metric_data.sum, sum(self.test_values[:index + 1])
205+
)
208206

209207
results = []
210208

@@ -239,3 +237,8 @@ def test_synchronous_cumulative_temporality(self):
239237
metric_data.bucket_counts,
240238
(0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0)
241239
)
240+
self.assertEqual(metric_data.min, self.test_values[0])
241+
self.assertEqual(metric_data.max, self.test_values[-1])
242+
self.assertEqual(
243+
metric_data.sum, sum(self.test_values)
244+
)

opentelemetry-sdk/tests/metrics/test_aggregation.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def test_aggregate(self):
270270
explicit_bucket_histogram_aggregation = (
271271
_ExplicitBucketHistogramAggregation(
272272
Mock(),
273-
AggregationTemporality.CUMULATIVE,
273+
AggregationTemporality.DELTA,
274274
0,
275275
boundaries=[0, 2, 4],
276276
)
@@ -356,7 +356,7 @@ def test_collect(self):
356356
explicit_bucket_histogram_aggregation = (
357357
_ExplicitBucketHistogramAggregation(
358358
Mock(),
359-
AggregationTemporality.CUMULATIVE,
359+
AggregationTemporality.DELTA,
360360
0,
361361
boundaries=[0, 1, 2],
362362
)
@@ -375,8 +375,6 @@ def test_collect(self):
375375
# CI fails the last assertion without this
376376
sleep(0.1)
377377

378-
from ipdb import set_trace
379-
set_trace()
380378
explicit_bucket_histogram_aggregation.aggregate(measurement(1))
381379
# 2 is used here directly to simulate the instant the second
382380
# collection process starts.

0 commit comments

Comments
 (0)