|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#include <memory> |
| 5 | +#ifndef ENABLE_METRICS_PREVIEW |
| 6 | +# include "opentelemetry/common/key_value_iterable_view.h" |
| 7 | +# include "opentelemetry/nostd/shared_ptr.h" |
| 8 | +# include "opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h" |
| 9 | +# include "opentelemetry/sdk/metrics/instruments.h" |
| 10 | +# include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" |
| 11 | +# include "opentelemetry/sdk/metrics/view/attributes_processor.h" |
| 12 | + |
| 13 | +# include <gtest/gtest.h> |
| 14 | +# include <map> |
| 15 | + |
| 16 | +using namespace opentelemetry::sdk::metrics; |
| 17 | +using namespace opentelemetry::common; |
| 18 | +using M = std::map<std::string, std::string>; |
| 19 | +namespace nostd = opentelemetry::nostd; |
| 20 | + |
| 21 | +class MockCollectorHandle : public CollectorHandle |
| 22 | +{ |
| 23 | +public: |
| 24 | + MockCollectorHandle(AggregationTemporality temp) : temporality(temp) {} |
| 25 | + |
| 26 | + AggregationTemporality GetAggregationTemporality(InstrumentType instrument_type) noexcept override |
| 27 | + { |
| 28 | + return temporality; |
| 29 | + } |
| 30 | + |
| 31 | +private: |
| 32 | + AggregationTemporality temporality; |
| 33 | +}; |
| 34 | + |
| 35 | +class WritableMetricStorageHistogramTestFixture |
| 36 | + : public ::testing::TestWithParam<AggregationTemporality> |
| 37 | +{}; |
| 38 | + |
| 39 | +TEST_P(WritableMetricStorageHistogramTestFixture, LongHistogram) |
| 40 | +{ |
| 41 | + AggregationTemporality temporality = GetParam(); |
| 42 | + auto sdk_start_ts = std::chrono::system_clock::now(); |
| 43 | + long expected_total_get_requests = 0; |
| 44 | + long expected_total_put_requests = 0; |
| 45 | + InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kHistogram, |
| 46 | + InstrumentValueType::kLong}; |
| 47 | + std::map<std::string, std::string> attributes_get = {{"RequestType", "GET"}}; |
| 48 | + std::map<std::string, std::string> attributes_put = {{"RequestType", "PUT"}}; |
| 49 | + |
| 50 | + std::unique_ptr<DefaultAttributesProcessor> default_attributes_processor{ |
| 51 | + new DefaultAttributesProcessor{}}; |
| 52 | + opentelemetry::sdk::metrics::SyncMetricStorage storage( |
| 53 | + instr_desc, AggregationType::kHistogram, default_attributes_processor.get(), |
| 54 | + NoExemplarReservoir::GetNoExemplarReservoir(), |
| 55 | + std::shared_ptr<opentelemetry::sdk::metrics::AggregationConfig>{}); |
| 56 | + |
| 57 | + storage.RecordLong(10l, KeyValueIterableView<std::map<std::string, std::string>>(attributes_get), |
| 58 | + opentelemetry::context::Context{}); |
| 59 | + expected_total_get_requests += 10; |
| 60 | + |
| 61 | + storage.RecordLong(30l, KeyValueIterableView<std::map<std::string, std::string>>(attributes_put), |
| 62 | + opentelemetry::context::Context{}); |
| 63 | + expected_total_put_requests += 30; |
| 64 | + |
| 65 | + storage.RecordLong(20l, KeyValueIterableView<std::map<std::string, std::string>>(attributes_get), |
| 66 | + opentelemetry::context::Context{}); |
| 67 | + expected_total_get_requests += 20; |
| 68 | + |
| 69 | + storage.RecordLong(40l, KeyValueIterableView<std::map<std::string, std::string>>(attributes_put), |
| 70 | + opentelemetry::context::Context{}); |
| 71 | + expected_total_put_requests += 40; |
| 72 | + |
| 73 | + std::shared_ptr<CollectorHandle> collector(new MockCollectorHandle(temporality)); |
| 74 | + std::vector<std::shared_ptr<CollectorHandle>> collectors; |
| 75 | + collectors.push_back(collector); |
| 76 | + |
| 77 | + // Some computation here |
| 78 | + auto collection_ts = std::chrono::system_clock::now(); |
| 79 | + size_t count_attributes = 0; |
| 80 | + storage.Collect( |
| 81 | + collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData data) { |
| 82 | + for (auto data_attr : data.point_data_attr_) |
| 83 | + { |
| 84 | + auto data = opentelemetry::nostd::get<HistogramPointData>(data_attr.point_data); |
| 85 | + if (opentelemetry::nostd::get<std::string>( |
| 86 | + data_attr.attributes.find("RequestType")->second) == "GET") |
| 87 | + { |
| 88 | + EXPECT_EQ(opentelemetry::nostd::get<long>(data.sum_), expected_total_get_requests); |
| 89 | + count_attributes++; |
| 90 | + } |
| 91 | + else if (opentelemetry::nostd::get<std::string>( |
| 92 | + data_attr.attributes.find("RequestType")->second) == "PUT") |
| 93 | + { |
| 94 | + EXPECT_EQ(opentelemetry::nostd::get<long>(data.sum_), expected_total_put_requests); |
| 95 | + count_attributes++; |
| 96 | + } |
| 97 | + } |
| 98 | + return true; |
| 99 | + }); |
| 100 | + EXPECT_EQ(count_attributes, 2); // GET and PUT |
| 101 | + // In case of delta temporarily, subsequent collection would contain new data points, so resetting |
| 102 | + // the counts |
| 103 | + if (temporality == AggregationTemporality::kDelta) |
| 104 | + { |
| 105 | + expected_total_get_requests = 0; |
| 106 | + expected_total_put_requests = 0; |
| 107 | + } |
| 108 | + |
| 109 | + // collect one more time. |
| 110 | + collection_ts = std::chrono::system_clock::now(); |
| 111 | + count_attributes = 0; |
| 112 | + storage.Collect( |
| 113 | + collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData data) { |
| 114 | + for (auto data_attr : data.point_data_attr_) |
| 115 | + { |
| 116 | + auto data = opentelemetry::nostd::get<HistogramPointData>(data_attr.point_data); |
| 117 | + if (opentelemetry::nostd::get<std::string>( |
| 118 | + data_attr.attributes.find("RequestType")->second) == "GET") |
| 119 | + { |
| 120 | + count_attributes++; |
| 121 | + EXPECT_EQ(opentelemetry::nostd::get<long>(data.sum_), expected_total_get_requests); |
| 122 | + } |
| 123 | + else if (opentelemetry::nostd::get<std::string>( |
| 124 | + data_attr.attributes.find("RequestType")->second) == "PUT") |
| 125 | + { |
| 126 | + count_attributes++; |
| 127 | + EXPECT_EQ(opentelemetry::nostd::get<long>(data.sum_), expected_total_put_requests); |
| 128 | + } |
| 129 | + } |
| 130 | + return true; |
| 131 | + }); |
| 132 | + if (temporality == AggregationTemporality::kCumulative) |
| 133 | + { |
| 134 | + EXPECT_EQ(count_attributes, 2); // GET AND PUT |
| 135 | + } |
| 136 | + |
| 137 | + storage.RecordLong(50l, KeyValueIterableView<std::map<std::string, std::string>>(attributes_get), |
| 138 | + opentelemetry::context::Context{}); |
| 139 | + expected_total_get_requests += 50; |
| 140 | + storage.RecordLong(40l, KeyValueIterableView<std::map<std::string, std::string>>(attributes_put), |
| 141 | + opentelemetry::context::Context{}); |
| 142 | + expected_total_put_requests += 40; |
| 143 | + |
| 144 | + collection_ts = std::chrono::system_clock::now(); |
| 145 | + count_attributes = 0; |
| 146 | + storage.Collect( |
| 147 | + collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData data) { |
| 148 | + for (auto data_attr : data.point_data_attr_) |
| 149 | + { |
| 150 | + auto data = opentelemetry::nostd::get<HistogramPointData>(data_attr.point_data); |
| 151 | + if (opentelemetry::nostd::get<std::string>( |
| 152 | + data_attr.attributes.find("RequestType")->second) == "GET") |
| 153 | + { |
| 154 | + EXPECT_EQ(opentelemetry::nostd::get<long>(data.sum_), expected_total_get_requests); |
| 155 | + count_attributes++; |
| 156 | + } |
| 157 | + else if (opentelemetry::nostd::get<std::string>( |
| 158 | + data_attr.attributes.find("RequestType")->second) == "PUT") |
| 159 | + { |
| 160 | + EXPECT_EQ(opentelemetry::nostd::get<long>(data.sum_), expected_total_put_requests); |
| 161 | + count_attributes++; |
| 162 | + } |
| 163 | + } |
| 164 | + return true; |
| 165 | + }); |
| 166 | + EXPECT_EQ(count_attributes, 2); // GET and PUT |
| 167 | +} |
| 168 | + |
| 169 | +INSTANTIATE_TEST_SUITE_P(WritableMetricStorageHistogramTestLong, |
| 170 | + WritableMetricStorageHistogramTestFixture, |
| 171 | + ::testing::Values(AggregationTemporality::kCumulative, |
| 172 | + AggregationTemporality::kDelta)); |
| 173 | + |
| 174 | +TEST_P(WritableMetricStorageHistogramTestFixture, DoubleHistogram) |
| 175 | +{ |
| 176 | + AggregationTemporality temporality = GetParam(); |
| 177 | + auto sdk_start_ts = std::chrono::system_clock::now(); |
| 178 | + double expected_total_get_requests = 0; |
| 179 | + double expected_total_put_requests = 0; |
| 180 | + InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kHistogram, |
| 181 | + InstrumentValueType::kDouble}; |
| 182 | + std::map<std::string, std::string> attributes_get = {{"RequestType", "GET"}}; |
| 183 | + std::map<std::string, std::string> attributes_put = {{"RequestType", "PUT"}}; |
| 184 | + |
| 185 | + std::unique_ptr<DefaultAttributesProcessor> default_attributes_processor{ |
| 186 | + new DefaultAttributesProcessor{}}; |
| 187 | + opentelemetry::sdk::metrics::SyncMetricStorage storage( |
| 188 | + instr_desc, AggregationType::kHistogram, default_attributes_processor.get(), |
| 189 | + NoExemplarReservoir::GetNoExemplarReservoir(), |
| 190 | + std::shared_ptr<opentelemetry::sdk::metrics::AggregationConfig>{}); |
| 191 | + |
| 192 | + storage.RecordDouble(10.0, |
| 193 | + KeyValueIterableView<std::map<std::string, std::string>>(attributes_get), |
| 194 | + opentelemetry::context::Context{}); |
| 195 | + expected_total_get_requests += 10; |
| 196 | + |
| 197 | + storage.RecordDouble(30.0, |
| 198 | + KeyValueIterableView<std::map<std::string, std::string>>(attributes_put), |
| 199 | + opentelemetry::context::Context{}); |
| 200 | + expected_total_put_requests += 30; |
| 201 | + |
| 202 | + storage.RecordDouble(20.0, |
| 203 | + KeyValueIterableView<std::map<std::string, std::string>>(attributes_get), |
| 204 | + opentelemetry::context::Context{}); |
| 205 | + expected_total_get_requests += 20; |
| 206 | + |
| 207 | + storage.RecordDouble(40.0, |
| 208 | + KeyValueIterableView<std::map<std::string, std::string>>(attributes_put), |
| 209 | + opentelemetry::context::Context{}); |
| 210 | + expected_total_put_requests += 40; |
| 211 | + |
| 212 | + std::shared_ptr<CollectorHandle> collector(new MockCollectorHandle(temporality)); |
| 213 | + std::vector<std::shared_ptr<CollectorHandle>> collectors; |
| 214 | + collectors.push_back(collector); |
| 215 | + |
| 216 | + // Some computation here |
| 217 | + auto collection_ts = std::chrono::system_clock::now(); |
| 218 | + size_t count_attributes = 0; |
| 219 | + storage.Collect( |
| 220 | + collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData data) { |
| 221 | + for (auto data_attr : data.point_data_attr_) |
| 222 | + { |
| 223 | + auto data = opentelemetry::nostd::get<HistogramPointData>(data_attr.point_data); |
| 224 | + if (opentelemetry::nostd::get<std::string>( |
| 225 | + data_attr.attributes.find("RequestType")->second) == "GET") |
| 226 | + { |
| 227 | + EXPECT_EQ(opentelemetry::nostd::get<double>(data.sum_), expected_total_get_requests); |
| 228 | + count_attributes++; |
| 229 | + } |
| 230 | + else if (opentelemetry::nostd::get<std::string>( |
| 231 | + data_attr.attributes.find("RequestType")->second) == "PUT") |
| 232 | + { |
| 233 | + EXPECT_EQ(opentelemetry::nostd::get<double>(data.sum_), expected_total_put_requests); |
| 234 | + count_attributes++; |
| 235 | + } |
| 236 | + } |
| 237 | + return true; |
| 238 | + }); |
| 239 | + EXPECT_EQ(count_attributes, 2); // GET and PUT |
| 240 | + |
| 241 | + // In case of delta temporarily, subsequent collection would contain new data points, so resetting |
| 242 | + // the counts |
| 243 | + if (temporality == AggregationTemporality::kDelta) |
| 244 | + { |
| 245 | + expected_total_get_requests = 0; |
| 246 | + expected_total_put_requests = 0; |
| 247 | + } |
| 248 | + |
| 249 | + // collect one more time. |
| 250 | + collection_ts = std::chrono::system_clock::now(); |
| 251 | + count_attributes = 0; |
| 252 | + storage.Collect( |
| 253 | + collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData data) { |
| 254 | + for (auto data_attr : data.point_data_attr_) |
| 255 | + { |
| 256 | + auto data = opentelemetry::nostd::get<HistogramPointData>(data_attr.point_data); |
| 257 | + if (opentelemetry::nostd::get<std::string>( |
| 258 | + data_attr.attributes.find("RequestType")->second) == "GET") |
| 259 | + { |
| 260 | + count_attributes++; |
| 261 | + EXPECT_EQ(opentelemetry::nostd::get<double>(data.sum_), expected_total_get_requests); |
| 262 | + } |
| 263 | + else if (opentelemetry::nostd::get<std::string>( |
| 264 | + data_attr.attributes.find("RequestType")->second) == "PUT") |
| 265 | + { |
| 266 | + count_attributes++; |
| 267 | + EXPECT_EQ(opentelemetry::nostd::get<double>(data.sum_), expected_total_put_requests); |
| 268 | + } |
| 269 | + } |
| 270 | + return true; |
| 271 | + }); |
| 272 | + if (temporality == AggregationTemporality::kCumulative) |
| 273 | + { |
| 274 | + EXPECT_EQ(count_attributes, 2); // GET AND PUT |
| 275 | + } |
| 276 | + |
| 277 | + storage.RecordDouble(50.0, |
| 278 | + KeyValueIterableView<std::map<std::string, std::string>>(attributes_get), |
| 279 | + opentelemetry::context::Context{}); |
| 280 | + expected_total_get_requests += 50; |
| 281 | + storage.RecordDouble(40.0, |
| 282 | + KeyValueIterableView<std::map<std::string, std::string>>(attributes_put), |
| 283 | + opentelemetry::context::Context{}); |
| 284 | + expected_total_put_requests += 40; |
| 285 | + |
| 286 | + collection_ts = std::chrono::system_clock::now(); |
| 287 | + count_attributes = 0; |
| 288 | + storage.Collect( |
| 289 | + collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData data) { |
| 290 | + for (auto data_attr : data.point_data_attr_) |
| 291 | + { |
| 292 | + auto data = opentelemetry::nostd::get<HistogramPointData>(data_attr.point_data); |
| 293 | + if (opentelemetry::nostd::get<std::string>( |
| 294 | + data_attr.attributes.find("RequestType")->second) == "GET") |
| 295 | + { |
| 296 | + EXPECT_EQ(opentelemetry::nostd::get<double>(data.sum_), expected_total_get_requests); |
| 297 | + count_attributes++; |
| 298 | + } |
| 299 | + else if (opentelemetry::nostd::get<std::string>( |
| 300 | + data_attr.attributes.find("RequestType")->second) == "PUT") |
| 301 | + { |
| 302 | + EXPECT_EQ(opentelemetry::nostd::get<double>(data.sum_), expected_total_put_requests); |
| 303 | + count_attributes++; |
| 304 | + } |
| 305 | + } |
| 306 | + return true; |
| 307 | + }); |
| 308 | + EXPECT_EQ(count_attributes, 2); // GET and PUT |
| 309 | +} |
| 310 | +INSTANTIATE_TEST_SUITE_P(WritableMetricStorageHistogramTestDouble, |
| 311 | + WritableMetricStorageHistogramTestFixture, |
| 312 | + ::testing::Values(AggregationTemporality::kCumulative, |
| 313 | + AggregationTemporality::kDelta)); |
| 314 | + |
| 315 | +#endif |
0 commit comments