|
10 | 10 | #include "opentelemetry/nostd/variant.h"
|
11 | 11 | #include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h"
|
12 | 12 | #include "opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_aggregation.h"
|
| 13 | +#include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" |
13 | 14 | #include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h"
|
14 | 15 | #include "opentelemetry/sdk/metrics/aggregation/lastvalue_aggregation.h"
|
15 | 16 | #include "opentelemetry/sdk/metrics/aggregation/sum_aggregation.h"
|
16 | 17 | #include "opentelemetry/sdk/metrics/data/circular_buffer.h"
|
17 | 18 | #include "opentelemetry/sdk/metrics/data/point_data.h"
|
| 19 | +#include "opentelemetry/sdk/metrics/instruments.h" |
18 | 20 |
|
19 | 21 | using namespace opentelemetry::sdk::metrics;
|
20 | 22 | namespace nostd = opentelemetry::nostd;
|
@@ -357,3 +359,95 @@ TEST(Aggregation, Base2ExponentialHistogramAggregation)
|
357 | 359 | ASSERT_TRUE(diffd_point.negative_buckets_ != nullptr);
|
358 | 360 | EXPECT_EQ(diffd_point.positive_buckets_->Get(2), 1);
|
359 | 361 | }
|
| 362 | + |
| 363 | +TEST(Aggregation, Base2ExponentialHistogramAggregationMerge) |
| 364 | +{ |
| 365 | + Base2ExponentialHistogramAggregationConfig config; |
| 366 | + config.max_scale_ = 10; |
| 367 | + config.max_buckets_ = 100; |
| 368 | + config.record_min_max_ = true; |
| 369 | + |
| 370 | + Base2ExponentialHistogramAggregation aggr(&config); |
| 371 | + |
| 372 | + int expected_count = 0; |
| 373 | + double expected_sum = 0.0; |
| 374 | + |
| 375 | + // Aggregate some small values |
| 376 | + for (int i = 1; i < 10; ++i) |
| 377 | + { |
| 378 | + expected_count++; |
| 379 | + const double value = i * 1e-12; |
| 380 | + expected_sum += value; |
| 381 | + aggr.Aggregate(value); |
| 382 | + } |
| 383 | + |
| 384 | + const auto aggr_point = nostd::get<Base2ExponentialHistogramPointData>(aggr.ToPoint()); |
| 385 | + |
| 386 | + ASSERT_EQ(aggr_point.count_, expected_count); |
| 387 | + ASSERT_DOUBLE_EQ(aggr_point.sum_, expected_sum); |
| 388 | + ASSERT_EQ(aggr_point.zero_count_, 0); |
| 389 | + ASSERT_GT(aggr_point.scale_, -10); |
| 390 | + ASSERT_EQ(aggr_point.max_buckets_, config.max_buckets_); |
| 391 | + |
| 392 | + auto test_merge = [](const std::unique_ptr<Aggregation> &merged_aggr, int expected_count, |
| 393 | + double expected_sum, int expected_zero_count, int expected_scale, |
| 394 | + int expected_max_buckets) { |
| 395 | + auto merged_point = nostd::get<Base2ExponentialHistogramPointData>(merged_aggr->ToPoint()); |
| 396 | + EXPECT_EQ(merged_point.count_, expected_count); |
| 397 | + EXPECT_DOUBLE_EQ(merged_point.sum_, expected_sum); |
| 398 | + EXPECT_EQ(merged_point.zero_count_, expected_zero_count); |
| 399 | + EXPECT_EQ(merged_point.scale_, expected_scale); |
| 400 | + EXPECT_EQ(merged_point.max_buckets_, expected_max_buckets); |
| 401 | + }; |
| 402 | + |
| 403 | + // default aggregation merge |
| 404 | + { |
| 405 | + InstrumentDescriptor descriptor; |
| 406 | + descriptor.type_ = InstrumentType::kHistogram; |
| 407 | + descriptor.unit_ = "unit"; |
| 408 | + descriptor.name_ = "histogram"; |
| 409 | + descriptor.description_ = "a histogram"; |
| 410 | + descriptor.value_type_ = InstrumentValueType::kDouble; |
| 411 | + |
| 412 | + auto default_aggr = DefaultAggregation::CreateAggregation( |
| 413 | + AggregationType::kBase2ExponentialHistogram, descriptor); |
| 414 | + auto default_point = nostd::get<Base2ExponentialHistogramPointData>(default_aggr->ToPoint()); |
| 415 | + |
| 416 | + const int expected_scale = |
| 417 | + aggr_point.scale_ < default_point.scale_ ? aggr_point.scale_ : default_point.scale_; |
| 418 | + const int expected_max_buckets = aggr_point.max_buckets_ < default_point.max_buckets_ |
| 419 | + ? aggr_point.max_buckets_ |
| 420 | + : default_point.max_buckets_; |
| 421 | + const int expected_zero_count = 0; |
| 422 | + |
| 423 | + auto merged_from_default = aggr.Merge(*default_aggr); |
| 424 | + test_merge(merged_from_default, expected_count, expected_sum, expected_zero_count, |
| 425 | + expected_scale, expected_max_buckets); |
| 426 | + |
| 427 | + auto merged_to_default = default_aggr->Merge(aggr); |
| 428 | + test_merge(merged_to_default, expected_count, expected_sum, expected_zero_count, expected_scale, |
| 429 | + expected_max_buckets); |
| 430 | + } |
| 431 | + |
| 432 | + // zero count aggregation merge (Zero is a special case and does not increment the buckets) |
| 433 | + { |
| 434 | + Base2ExponentialHistogramAggregation zero_aggr(&config); |
| 435 | + zero_aggr.Aggregate(0.0); |
| 436 | + |
| 437 | + const auto zero_point = nostd::get<Base2ExponentialHistogramPointData>(zero_aggr.ToPoint()); |
| 438 | + const int expected_scale = |
| 439 | + aggr_point.scale_ < zero_point.scale_ ? aggr_point.scale_ : zero_point.scale_; |
| 440 | + const int expected_max_buckets = aggr_point.max_buckets_ < zero_point.max_buckets_ |
| 441 | + ? aggr_point.max_buckets_ |
| 442 | + : zero_point.max_buckets_; |
| 443 | + const int expected_zero_count = 1; |
| 444 | + |
| 445 | + auto merged_from_zero = aggr.Merge(zero_aggr); |
| 446 | + test_merge(merged_from_zero, expected_count + 1, expected_sum, expected_zero_count, |
| 447 | + expected_scale, expected_max_buckets); |
| 448 | + |
| 449 | + auto merged_to_zero = zero_aggr.Merge(aggr); |
| 450 | + test_merge(merged_to_zero, expected_count + 1, expected_sum, expected_zero_count, |
| 451 | + expected_scale, expected_max_buckets); |
| 452 | + } |
| 453 | +} |
0 commit comments