cardinality_limit_test.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include <gtest/gtest.h>
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #include <algorithm>
  7. #include <chrono>
  8. #include <functional>
  9. #include <map>
  10. #include <memory>
  11. #include <string>
  12. #include <utility>
  13. #include <vector>
  14. #include "common.h"
  15. #include "opentelemetry/common/key_value_iterable_view.h"
  16. #include "opentelemetry/context/context.h"
  17. #include "opentelemetry/nostd/function_ref.h"
  18. #include "opentelemetry/nostd/span.h"
  19. #include "opentelemetry/nostd/variant.h"
  20. #include "opentelemetry/sdk/metrics/aggregation/aggregation.h"
  21. #include "opentelemetry/sdk/metrics/aggregation/sum_aggregation.h"
  22. #include "opentelemetry/sdk/metrics/data/metric_data.h"
  23. #include "opentelemetry/sdk/metrics/data/point_data.h"
  24. #include "opentelemetry/sdk/metrics/instruments.h"
  25. #include "opentelemetry/sdk/metrics/state/attributes_hashmap.h"
  26. #include "opentelemetry/sdk/metrics/state/filtered_ordered_attribute_map.h"
  27. #include "opentelemetry/sdk/metrics/state/metric_collector.h"
  28. #include "opentelemetry/sdk/metrics/state/sync_metric_storage.h"
  29. #include "opentelemetry/sdk/metrics/view/attributes_processor.h"
  30. #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
  31. # include "opentelemetry/sdk/metrics/exemplar/filter_type.h"
  32. #endif
  33. using namespace opentelemetry::sdk::metrics;
  34. using namespace opentelemetry::common;
  35. namespace nostd = opentelemetry::nostd;
  36. TEST(CardinalityLimit, AttributesHashMapBasicTests)
  37. {
  38. AttributesHashMap hash_map(10);
  39. std::function<std::unique_ptr<Aggregation>()> aggregation_callback =
  40. []() -> std::unique_ptr<Aggregation> {
  41. return std::unique_ptr<Aggregation>(new LongSumAggregation(true));
  42. };
  43. // add 10 unique metric points. 9 should be added to hashmap, 10th should be overflow.
  44. int64_t record_value = 100;
  45. for (auto i = 0; i < 10; i++)
  46. {
  47. FilteredOrderedAttributeMap attributes = {{"key", std::to_string(i)}};
  48. static_cast<LongSumAggregation *>(hash_map.GetOrSetDefault(attributes, aggregation_callback))
  49. ->Aggregate(record_value);
  50. }
  51. EXPECT_EQ(hash_map.Size(), 10);
  52. // add 5 unique metric points above limit, they all should get consolidated as single
  53. // overflowmetric point.
  54. for (auto i = 10; i < 15; i++)
  55. {
  56. FilteredOrderedAttributeMap attributes = {{"key", std::to_string(i)}};
  57. static_cast<LongSumAggregation *>(hash_map.GetOrSetDefault(attributes, aggregation_callback))
  58. ->Aggregate(record_value);
  59. }
  60. EXPECT_EQ(hash_map.Size(), 10); // only one more metric point should be added as overflow.
  61. // record 5 more measurements to already existing (and not-overflow) metric points. They
  62. // should get aggregated to these existing metric points.
  63. for (auto i = 0; i < 5; i++)
  64. {
  65. FilteredOrderedAttributeMap attributes = {{"key", std::to_string(i)}};
  66. static_cast<LongSumAggregation *>(hash_map.GetOrSetDefault(attributes, aggregation_callback))
  67. ->Aggregate(record_value);
  68. }
  69. EXPECT_EQ(hash_map.Size(), 10); // no new metric point added
  70. // get the overflow metric point
  71. auto agg1 = hash_map.GetOrSetDefault(kOverflowAttributes, aggregation_callback);
  72. EXPECT_NE(agg1, nullptr);
  73. auto sum_agg1 = static_cast<LongSumAggregation *>(agg1);
  74. EXPECT_EQ(nostd::get<int64_t>(nostd::get<SumPointData>(sum_agg1->ToPoint()).value_),
  75. record_value * 6); // 1 from previous 10, 5 from current 5.
  76. // get remaining metric points
  77. for (auto i = 0; i < 9; i++)
  78. {
  79. FilteredOrderedAttributeMap attributes = {{"key", std::to_string(i)}};
  80. auto agg2 = hash_map.GetOrSetDefault(attributes, aggregation_callback);
  81. EXPECT_NE(agg2, nullptr);
  82. auto sum_agg2 = static_cast<LongSumAggregation *>(agg2);
  83. if (i < 5)
  84. {
  85. EXPECT_EQ(nostd::get<int64_t>(nostd::get<SumPointData>(sum_agg2->ToPoint()).value_),
  86. record_value * 2); // 1 from first recording, 1 from third recording
  87. }
  88. else
  89. {
  90. EXPECT_EQ(nostd::get<int64_t>(nostd::get<SumPointData>(sum_agg2->ToPoint()).value_),
  91. record_value); // 1 from first recording
  92. }
  93. }
  94. }
  95. class WritableMetricStorageCardinalityLimitTestFixture
  96. : public ::testing::TestWithParam<AggregationTemporality>
  97. {};
  98. TEST_P(WritableMetricStorageCardinalityLimitTestFixture, LongCounterSumAggregation)
  99. {
  100. auto sdk_start_ts = std::chrono::system_clock::now();
  101. const size_t attributes_limit = 10;
  102. InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kCounter,
  103. InstrumentValueType::kLong};
  104. std::unique_ptr<DefaultAttributesProcessor> default_attributes_processor{
  105. new DefaultAttributesProcessor{}};
  106. SyncMetricStorage storage(instr_desc, AggregationType::kSum, default_attributes_processor.get(),
  107. #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
  108. ExemplarFilterType::kAlwaysOff,
  109. ExemplarReservoir::GetNoExemplarReservoir(),
  110. #endif
  111. nullptr, attributes_limit);
  112. int64_t record_value = 100;
  113. // add 9 unique metric points, and 6 more above limit.
  114. for (auto i = 0; i < 15; i++)
  115. {
  116. std::map<std::string, std::string> attributes = {{"key", std::to_string(i)}};
  117. storage.RecordLong(record_value,
  118. KeyValueIterableView<std::map<std::string, std::string>>(attributes),
  119. opentelemetry::context::Context{});
  120. }
  121. AggregationTemporality temporality = GetParam();
  122. std::shared_ptr<CollectorHandle> collector(new MockCollectorHandle(temporality));
  123. std::vector<std::shared_ptr<CollectorHandle>> collectors;
  124. collectors.push_back(collector);
  125. //... Some computation here
  126. auto collection_ts = std::chrono::system_clock::now();
  127. size_t count_attributes = 0;
  128. bool overflow_present = false;
  129. storage.Collect(
  130. collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData &metric_data) {
  131. for (const auto &data_attr : metric_data.point_data_attr_)
  132. {
  133. const auto &data = opentelemetry::nostd::get<SumPointData>(data_attr.point_data);
  134. count_attributes++;
  135. if (data_attr.attributes.begin()->first == kAttributesLimitOverflowKey)
  136. {
  137. EXPECT_EQ(nostd::get<int64_t>(data.value_), record_value * 6);
  138. overflow_present = true;
  139. }
  140. }
  141. return true;
  142. });
  143. EXPECT_EQ(count_attributes, attributes_limit);
  144. EXPECT_EQ(overflow_present, true);
  145. }
  146. INSTANTIATE_TEST_SUITE_P(All,
  147. WritableMetricStorageCardinalityLimitTestFixture,
  148. ::testing::Values(AggregationTemporality::kDelta));