1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
|
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=protected-access
from math import inf
from time import sleep, time_ns
from typing import Union
from unittest import TestCase
from unittest.mock import Mock
from opentelemetry.context import Context
from opentelemetry.sdk.metrics._internal.aggregation import (
_ExplicitBucketHistogramAggregation,
_LastValueAggregation,
_SumAggregation,
)
from opentelemetry.sdk.metrics._internal.exemplar import (
AlignedHistogramBucketExemplarReservoir,
SimpleFixedSizeExemplarReservoir,
)
from opentelemetry.sdk.metrics._internal.instrument import (
_Counter,
_Gauge,
_Histogram,
_ObservableCounter,
_ObservableGauge,
_ObservableUpDownCounter,
_UpDownCounter,
)
from opentelemetry.sdk.metrics._internal.measurement import Measurement
from opentelemetry.sdk.metrics._internal.view import _default_reservoir_factory
from opentelemetry.sdk.metrics.export import (
AggregationTemporality,
NumberDataPoint,
)
from opentelemetry.sdk.metrics.view import (
DefaultAggregation,
ExplicitBucketHistogramAggregation,
LastValueAggregation,
SumAggregation,
)
from opentelemetry.util.types import Attributes
def measurement(
value: Union[int, float], attributes: Attributes = None
) -> Measurement:
return Measurement(
value,
time_ns(),
instrument=Mock(),
context=Context(),
attributes=attributes,
)
class TestSynchronousSumAggregation(TestCase):
def test_aggregate_delta(self):
"""
`SynchronousSumAggregation` aggregates data for sum metric points
"""
synchronous_sum_aggregation = _SumAggregation(
Mock(),
True,
AggregationTemporality.DELTA,
0,
_default_reservoir_factory(_SumAggregation),
)
synchronous_sum_aggregation.aggregate(measurement(1))
synchronous_sum_aggregation.aggregate(measurement(2))
synchronous_sum_aggregation.aggregate(measurement(3))
self.assertEqual(synchronous_sum_aggregation._value, 6)
synchronous_sum_aggregation = _SumAggregation(
Mock(),
True,
AggregationTemporality.DELTA,
0,
_default_reservoir_factory(_SumAggregation),
)
synchronous_sum_aggregation.aggregate(measurement(1))
synchronous_sum_aggregation.aggregate(measurement(-2))
synchronous_sum_aggregation.aggregate(measurement(3))
self.assertEqual(synchronous_sum_aggregation._value, 2)
def test_aggregate_cumulative(self):
"""
`SynchronousSumAggregation` aggregates data for sum metric points
"""
synchronous_sum_aggregation = _SumAggregation(
Mock(),
True,
AggregationTemporality.CUMULATIVE,
0,
_default_reservoir_factory(_SumAggregation),
)
synchronous_sum_aggregation.aggregate(measurement(1))
synchronous_sum_aggregation.aggregate(measurement(2))
synchronous_sum_aggregation.aggregate(measurement(3))
self.assertEqual(synchronous_sum_aggregation._value, 6)
synchronous_sum_aggregation = _SumAggregation(
Mock(),
True,
AggregationTemporality.CUMULATIVE,
0,
_default_reservoir_factory(_SumAggregation),
)
synchronous_sum_aggregation.aggregate(measurement(1))
synchronous_sum_aggregation.aggregate(measurement(-2))
synchronous_sum_aggregation.aggregate(measurement(3))
self.assertEqual(synchronous_sum_aggregation._value, 2)
def test_collect_delta(self):
"""
`SynchronousSumAggregation` collects sum metric points
"""
synchronous_sum_aggregation = _SumAggregation(
Mock(),
True,
AggregationTemporality.DELTA,
0,
_default_reservoir_factory(_SumAggregation),
)
synchronous_sum_aggregation.aggregate(measurement(1))
# 1 is used here directly to simulate the instant the first
# collection process starts.
first_sum = synchronous_sum_aggregation.collect(
AggregationTemporality.CUMULATIVE, 1
)
self.assertEqual(first_sum.value, 1)
synchronous_sum_aggregation.aggregate(measurement(1))
# 2 is used here directly to simulate the instant the first
# collection process starts.
second_sum = synchronous_sum_aggregation.collect(
AggregationTemporality.CUMULATIVE, 2
)
self.assertEqual(second_sum.value, 2)
self.assertEqual(
second_sum.start_time_unix_nano, first_sum.start_time_unix_nano
)
synchronous_sum_aggregation = _SumAggregation(
Mock(),
True,
AggregationTemporality.DELTA,
0,
_default_reservoir_factory(_SumAggregation),
)
synchronous_sum_aggregation.aggregate(measurement(1))
# 1 is used here directly to simulate the instant the first
# collection process starts.
first_sum = synchronous_sum_aggregation.collect(
AggregationTemporality.DELTA, 1
)
self.assertEqual(first_sum.value, 1)
synchronous_sum_aggregation.aggregate(measurement(1))
# 2 is used here directly to simulate the instant the first
# collection process starts.
second_sum = synchronous_sum_aggregation.collect(
AggregationTemporality.DELTA, 2
)
self.assertEqual(second_sum.value, 1)
self.assertGreater(
second_sum.start_time_unix_nano, first_sum.start_time_unix_nano
)
def test_collect_cumulative(self):
"""
`SynchronousSumAggregation` collects number data points
"""
sum_aggregation = _SumAggregation(
Mock(),
True,
AggregationTemporality.CUMULATIVE,
0,
_default_reservoir_factory(_SumAggregation),
)
sum_aggregation.aggregate(measurement(1))
first_sum = sum_aggregation.collect(
AggregationTemporality.CUMULATIVE, 1
)
self.assertEqual(first_sum.value, 1)
# should have been reset after first collect
sum_aggregation.aggregate(measurement(1))
second_sum = sum_aggregation.collect(
AggregationTemporality.CUMULATIVE, 1
)
self.assertEqual(second_sum.value, 1)
self.assertEqual(
second_sum.start_time_unix_nano, first_sum.start_time_unix_nano
)
# if no point seen for a whole interval, should return None
third_sum = sum_aggregation.collect(
AggregationTemporality.CUMULATIVE, 1
)
self.assertIsNone(third_sum)
class TestLastValueAggregation(TestCase):
def test_aggregate(self):
"""
`LastValueAggregation` collects data for gauge metric points with delta
temporality
"""
last_value_aggregation = _LastValueAggregation(
Mock(), _default_reservoir_factory(_LastValueAggregation)
)
last_value_aggregation.aggregate(measurement(1))
self.assertEqual(last_value_aggregation._value, 1)
last_value_aggregation.aggregate(measurement(2))
self.assertEqual(last_value_aggregation._value, 2)
last_value_aggregation.aggregate(measurement(3))
self.assertEqual(last_value_aggregation._value, 3)
def test_collect(self):
"""
`LastValueAggregation` collects number data points
"""
last_value_aggregation = _LastValueAggregation(
Mock(), _default_reservoir_factory(_LastValueAggregation)
)
self.assertIsNone(
last_value_aggregation.collect(
AggregationTemporality.CUMULATIVE, 1
)
)
last_value_aggregation.aggregate(measurement(1))
# 1 is used here directly to simulate the instant the first
# collection process starts.
first_number_data_point = last_value_aggregation.collect(
AggregationTemporality.CUMULATIVE, 1
)
self.assertIsInstance(first_number_data_point, NumberDataPoint)
self.assertEqual(first_number_data_point.value, 1)
self.assertIsNone(first_number_data_point.start_time_unix_nano)
last_value_aggregation.aggregate(measurement(1))
# CI fails the last assertion without this
sleep(0.1)
# 2 is used here directly to simulate the instant the second
# collection process starts.
second_number_data_point = last_value_aggregation.collect(
AggregationTemporality.CUMULATIVE, 2
)
self.assertEqual(second_number_data_point.value, 1)
self.assertIsNone(second_number_data_point.start_time_unix_nano)
self.assertGreater(
second_number_data_point.time_unix_nano,
first_number_data_point.time_unix_nano,
)
# 3 is used here directly to simulate the instant the second
# collection process starts.
third_number_data_point = last_value_aggregation.collect(
AggregationTemporality.CUMULATIVE, 3
)
self.assertIsNone(third_number_data_point)
class TestExplicitBucketHistogramAggregation(TestCase):
def test_aggregate(self):
"""
Test `ExplicitBucketHistogramAggregation with custom boundaries
"""
explicit_bucket_histogram_aggregation = (
_ExplicitBucketHistogramAggregation(
Mock(),
AggregationTemporality.DELTA,
0,
_default_reservoir_factory(
_ExplicitBucketHistogramAggregation
),
boundaries=[0, 2, 4],
)
)
explicit_bucket_histogram_aggregation.aggregate(measurement(-1))
explicit_bucket_histogram_aggregation.aggregate(measurement(0))
explicit_bucket_histogram_aggregation.aggregate(measurement(1))
explicit_bucket_histogram_aggregation.aggregate(measurement(2))
explicit_bucket_histogram_aggregation.aggregate(measurement(3))
explicit_bucket_histogram_aggregation.aggregate(measurement(4))
explicit_bucket_histogram_aggregation.aggregate(measurement(5))
# The first bucket keeps count of values between (-inf, 0] (-1 and 0)
self.assertEqual(explicit_bucket_histogram_aggregation._value[0], 2)
# The second bucket keeps count of values between (0, 2] (1 and 2)
self.assertEqual(explicit_bucket_histogram_aggregation._value[1], 2)
# The third bucket keeps count of values between (2, 4] (3 and 4)
self.assertEqual(explicit_bucket_histogram_aggregation._value[2], 2)
# The fourth bucket keeps count of values between (4, inf) (3 and 4)
self.assertEqual(explicit_bucket_histogram_aggregation._value[3], 1)
histo = explicit_bucket_histogram_aggregation.collect(
AggregationTemporality.CUMULATIVE, 1
)
self.assertEqual(histo.sum, 14)
def test_min_max(self):
"""
`record_min_max` indicates the aggregator to record the minimum and
maximum value in the population
"""
explicit_bucket_histogram_aggregation = (
_ExplicitBucketHistogramAggregation(
Mock(),
AggregationTemporality.CUMULATIVE,
0,
_default_reservoir_factory(
_ExplicitBucketHistogramAggregation
),
)
)
explicit_bucket_histogram_aggregation.aggregate(measurement(-1))
explicit_bucket_histogram_aggregation.aggregate(measurement(2))
explicit_bucket_histogram_aggregation.aggregate(measurement(7))
explicit_bucket_histogram_aggregation.aggregate(measurement(8))
explicit_bucket_histogram_aggregation.aggregate(measurement(9999))
self.assertEqual(explicit_bucket_histogram_aggregation._min, -1)
self.assertEqual(explicit_bucket_histogram_aggregation._max, 9999)
explicit_bucket_histogram_aggregation = (
_ExplicitBucketHistogramAggregation(
Mock(),
AggregationTemporality.CUMULATIVE,
0,
_default_reservoir_factory(
_ExplicitBucketHistogramAggregation
),
record_min_max=False,
)
)
explicit_bucket_histogram_aggregation.aggregate(measurement(-1))
explicit_bucket_histogram_aggregation.aggregate(measurement(2))
explicit_bucket_histogram_aggregation.aggregate(measurement(7))
explicit_bucket_histogram_aggregation.aggregate(measurement(8))
explicit_bucket_histogram_aggregation.aggregate(measurement(9999))
self.assertEqual(explicit_bucket_histogram_aggregation._min, inf)
self.assertEqual(explicit_bucket_histogram_aggregation._max, -inf)
def test_collect(self):
"""
`_ExplicitBucketHistogramAggregation` collects sum metric points
"""
explicit_bucket_histogram_aggregation = (
_ExplicitBucketHistogramAggregation(
Mock(),
AggregationTemporality.DELTA,
0,
_default_reservoir_factory(
_ExplicitBucketHistogramAggregation
),
boundaries=[0, 1, 2],
)
)
explicit_bucket_histogram_aggregation.aggregate(measurement(1))
# 1 is used here directly to simulate the instant the first
# collection process starts.
first_histogram = explicit_bucket_histogram_aggregation.collect(
AggregationTemporality.CUMULATIVE, 1
)
self.assertEqual(first_histogram.bucket_counts, (0, 1, 0, 0))
self.assertEqual(first_histogram.sum, 1)
# CI fails the last assertion without this
sleep(0.1)
explicit_bucket_histogram_aggregation.aggregate(measurement(1))
# 2 is used here directly to simulate the instant the second
# collection process starts.
second_histogram = explicit_bucket_histogram_aggregation.collect(
AggregationTemporality.CUMULATIVE, 2
)
self.assertEqual(second_histogram.bucket_counts, (0, 2, 0, 0))
self.assertEqual(second_histogram.sum, 2)
self.assertGreater(
second_histogram.time_unix_nano, first_histogram.time_unix_nano
)
def test_boundaries(self):
self.assertEqual(
_ExplicitBucketHistogramAggregation(
Mock(),
AggregationTemporality.CUMULATIVE,
0,
_default_reservoir_factory(
_ExplicitBucketHistogramAggregation
),
)._boundaries,
(
0.0,
5.0,
10.0,
25.0,
50.0,
75.0,
100.0,
250.0,
500.0,
750.0,
1000.0,
2500.0,
5000.0,
7500.0,
10000.0,
),
)
class TestAggregationFactory(TestCase):
def test_sum_factory(self):
counter = _Counter("name", Mock(), Mock())
factory = SumAggregation()
aggregation = factory._create_aggregation(
counter, Mock(), _default_reservoir_factory, 0
)
self.assertIsInstance(aggregation, _SumAggregation)
self.assertTrue(aggregation._instrument_is_monotonic)
self.assertEqual(
aggregation._instrument_aggregation_temporality,
AggregationTemporality.DELTA,
)
aggregation2 = factory._create_aggregation(
counter, Mock(), _default_reservoir_factory, 0
)
self.assertNotEqual(aggregation, aggregation2)
counter = _UpDownCounter("name", Mock(), Mock())
factory = SumAggregation()
aggregation = factory._create_aggregation(
counter, Mock(), _default_reservoir_factory, 0
)
self.assertIsInstance(aggregation, _SumAggregation)
self.assertFalse(aggregation._instrument_is_monotonic)
self.assertEqual(
aggregation._instrument_aggregation_temporality,
AggregationTemporality.DELTA,
)
counter = _ObservableCounter("name", Mock(), Mock(), None)
factory = SumAggregation()
aggregation = factory._create_aggregation(
counter, Mock(), _default_reservoir_factory, 0
)
self.assertIsInstance(aggregation, _SumAggregation)
self.assertTrue(aggregation._instrument_is_monotonic)
self.assertEqual(
aggregation._instrument_aggregation_temporality,
AggregationTemporality.CUMULATIVE,
)
def test_explicit_bucket_histogram_factory(self):
histo = _Histogram("name", Mock(), Mock())
factory = ExplicitBucketHistogramAggregation(
boundaries=(
0.0,
5.0,
),
record_min_max=False,
)
aggregation = factory._create_aggregation(
histo, Mock(), _default_reservoir_factory, 0
)
self.assertIsInstance(aggregation, _ExplicitBucketHistogramAggregation)
self.assertFalse(aggregation._record_min_max)
self.assertEqual(aggregation._boundaries, (0.0, 5.0))
aggregation2 = factory._create_aggregation(
histo, Mock(), _default_reservoir_factory, 0
)
self.assertNotEqual(aggregation, aggregation2)
def test_last_value_factory(self):
counter = _Counter("name", Mock(), Mock())
factory = LastValueAggregation()
aggregation = factory._create_aggregation(
counter, Mock(), _default_reservoir_factory, 0
)
self.assertIsInstance(aggregation, _LastValueAggregation)
aggregation2 = factory._create_aggregation(
counter, Mock(), _default_reservoir_factory, 0
)
self.assertNotEqual(aggregation, aggregation2)
class TestDefaultAggregation(TestCase):
@classmethod
def setUpClass(cls):
cls.default_aggregation = DefaultAggregation()
def test_counter(self):
aggregation = self.default_aggregation._create_aggregation(
_Counter("name", Mock(), Mock()),
Mock(),
_default_reservoir_factory,
0,
)
self.assertIsInstance(aggregation, _SumAggregation)
self.assertTrue(aggregation._instrument_is_monotonic)
self.assertEqual(
aggregation._instrument_aggregation_temporality,
AggregationTemporality.DELTA,
)
def test_up_down_counter(self):
aggregation = self.default_aggregation._create_aggregation(
_UpDownCounter("name", Mock(), Mock()),
Mock(),
_default_reservoir_factory,
0,
)
self.assertIsInstance(aggregation, _SumAggregation)
self.assertFalse(aggregation._instrument_is_monotonic)
self.assertEqual(
aggregation._instrument_aggregation_temporality,
AggregationTemporality.DELTA,
)
def test_observable_counter(self):
aggregation = self.default_aggregation._create_aggregation(
_ObservableCounter("name", Mock(), Mock(), callbacks=[Mock()]),
Mock(),
_default_reservoir_factory,
0,
)
self.assertIsInstance(aggregation, _SumAggregation)
self.assertTrue(aggregation._instrument_is_monotonic)
self.assertEqual(
aggregation._instrument_aggregation_temporality,
AggregationTemporality.CUMULATIVE,
)
def test_observable_up_down_counter(self):
aggregation = self.default_aggregation._create_aggregation(
_ObservableUpDownCounter(
"name", Mock(), Mock(), callbacks=[Mock()]
),
Mock(),
_default_reservoir_factory,
0,
)
self.assertIsInstance(aggregation, _SumAggregation)
self.assertFalse(aggregation._instrument_is_monotonic)
self.assertEqual(
aggregation._instrument_aggregation_temporality,
AggregationTemporality.CUMULATIVE,
)
def test_histogram(self):
aggregation = self.default_aggregation._create_aggregation(
_Histogram(
"name",
Mock(),
Mock(),
),
Mock(),
_default_reservoir_factory,
0,
)
self.assertIsInstance(aggregation, _ExplicitBucketHistogramAggregation)
def test_histogram_with_advisory(self):
boundaries = [1.0, 2.0, 3.0]
aggregation = self.default_aggregation._create_aggregation(
_Histogram(
"name",
Mock(),
Mock(),
explicit_bucket_boundaries_advisory=boundaries,
),
Mock(),
_default_reservoir_factory,
0,
)
self.assertIsInstance(aggregation, _ExplicitBucketHistogramAggregation)
self.assertEqual(aggregation._boundaries, tuple(boundaries))
def test_gauge(self):
aggregation = self.default_aggregation._create_aggregation(
_Gauge(
"name",
Mock(),
Mock(),
),
Mock(),
_default_reservoir_factory,
0,
)
self.assertIsInstance(aggregation, _LastValueAggregation)
def test_observable_gauge(self):
aggregation = self.default_aggregation._create_aggregation(
_ObservableGauge(
"name",
Mock(),
Mock(),
callbacks=[Mock()],
),
Mock(),
_default_reservoir_factory,
0,
)
self.assertIsInstance(aggregation, _LastValueAggregation)
class TestExemplarsFromAggregations(TestCase):
def test_collection_simple_fixed_size_reservoir(self):
synchronous_sum_aggregation = _SumAggregation(
Mock(),
True,
AggregationTemporality.DELTA,
0,
lambda: SimpleFixedSizeExemplarReservoir(size=3),
)
synchronous_sum_aggregation.aggregate(measurement(1))
synchronous_sum_aggregation.aggregate(measurement(2))
synchronous_sum_aggregation.aggregate(measurement(3))
self.assertEqual(synchronous_sum_aggregation._value, 6)
datapoint = synchronous_sum_aggregation.collect(
AggregationTemporality.CUMULATIVE, 0
)
# As the reservoir as multiple buckets, it may store up to
# 3 exemplars
self.assertGreater(len(datapoint.exemplars), 0)
self.assertLessEqual(len(datapoint.exemplars), 3)
def test_collection_simple_fixed_size_reservoir_with_default_reservoir(
self,
):
synchronous_sum_aggregation = _SumAggregation(
Mock(),
True,
AggregationTemporality.DELTA,
0,
_default_reservoir_factory(_SumAggregation),
)
synchronous_sum_aggregation.aggregate(measurement(1))
synchronous_sum_aggregation.aggregate(measurement(2))
synchronous_sum_aggregation.aggregate(measurement(3))
self.assertEqual(synchronous_sum_aggregation._value, 6)
datapoint = synchronous_sum_aggregation.collect(
AggregationTemporality.CUMULATIVE, 0
)
self.assertEqual(len(datapoint.exemplars), 1)
def test_collection_aligned_histogram_bucket_reservoir(self):
boundaries = [5.0, 10.0, 20.0]
synchronous_sum_aggregation = _SumAggregation(
Mock(),
True,
AggregationTemporality.DELTA,
0,
lambda: AlignedHistogramBucketExemplarReservoir(boundaries),
)
synchronous_sum_aggregation.aggregate(measurement(2.0))
synchronous_sum_aggregation.aggregate(measurement(4.0))
synchronous_sum_aggregation.aggregate(measurement(6.0))
synchronous_sum_aggregation.aggregate(measurement(15.0))
synchronous_sum_aggregation.aggregate(measurement(25.0))
datapoint = synchronous_sum_aggregation.collect(
AggregationTemporality.CUMULATIVE, 0
)
self.assertEqual(len(datapoint.exemplars), 4)
# Verify that exemplars are associated with the correct boundaries
expected_buckets = [
(
4.0,
boundaries[0],
), # First bucket, should hold the last value <= 5.0
(
6.0,
boundaries[1],
), # Second bucket, should hold the last value <= 10.0
(
15.0,
boundaries[2],
), # Third bucket, should hold the last value <= 20.0
(25.0, None), # Last bucket, should hold the value > 20.0
]
for exemplar, (value, boundary) in zip(
datapoint.exemplars, expected_buckets
):
self.assertEqual(exemplar.value, value)
if boundary is not None:
self.assertLessEqual(exemplar.value, boundary)
else:
self.assertGreater(exemplar.value, boundaries[-1])
|