Skip to content

Commit 84c09d1

Browse files
committed
Add kcumulative and kdelta aggregation temporality test with collect calls for base2 histogram aggregation
1 parent 66c5a6b commit 84c09d1

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

sdk/test/metrics/sync_metric_storage_histogram_test.cc

+146
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,152 @@ TEST_P(WritableMetricStorageHistogramTestFixture, DoubleHistogram)
323323
EXPECT_EQ(count_attributes, 2); // GET and PUT
324324
}
325325
INSTANTIATE_TEST_SUITE_P(WritableMetricStorageHistogramTestDouble,
326+
WritableMetricStorageHistogramTestFixture,
327+
::testing::Values(AggregationTemporality::kCumulative,
328+
AggregationTemporality::kDelta));
329+
TEST_P(WritableMetricStorageHistogramTestFixture, Base2ExponentialDoubleHistogram)
330+
{
331+
AggregationTemporality temporality = GetParam();
332+
auto sdk_start_ts = std::chrono::system_clock::now();
333+
double expected_total_get_requests = 0;
334+
double expected_total_put_requests = 0;
335+
InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kHistogram,
336+
InstrumentValueType::kDouble};
337+
std::map<std::string, std::string> attributes_get = {{"RequestType", "GET"}};
338+
std::map<std::string, std::string> attributes_put = {{"RequestType", "PUT"}};
339+
340+
std::unique_ptr<DefaultAttributesProcessor> default_attributes_processor{
341+
new DefaultAttributesProcessor{}};
342+
opentelemetry::sdk::metrics::SyncMetricStorage storage(
343+
instr_desc, AggregationType::kBase2ExponentialHistogram, default_attributes_processor.get(),
344+
#ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
345+
ExemplarFilterType::kAlwaysOff, ExemplarReservoir::GetNoExemplarReservoir(),
346+
#endif
347+
nullptr);
348+
349+
storage.RecordDouble(10.0,
350+
KeyValueIterableView<std::map<std::string, std::string>>(attributes_get),
351+
opentelemetry::context::Context{});
352+
expected_total_get_requests += 10;
353+
354+
storage.RecordDouble(30.0,
355+
KeyValueIterableView<std::map<std::string, std::string>>(attributes_put),
356+
opentelemetry::context::Context{});
357+
expected_total_put_requests += 30;
358+
359+
storage.RecordDouble(20.0,
360+
KeyValueIterableView<std::map<std::string, std::string>>(attributes_get),
361+
opentelemetry::context::Context{});
362+
expected_total_get_requests += 20;
363+
364+
storage.RecordDouble(40.0,
365+
KeyValueIterableView<std::map<std::string, std::string>>(attributes_put),
366+
opentelemetry::context::Context{});
367+
expected_total_put_requests += 40;
368+
369+
std::shared_ptr<CollectorHandle> collector(new MockCollectorHandle(temporality));
370+
std::vector<std::shared_ptr<CollectorHandle>> collectors;
371+
collectors.push_back(collector);
372+
373+
// Some computation here
374+
auto collection_ts = std::chrono::system_clock::now();
375+
size_t count_attributes = 0;
376+
storage.Collect(
377+
collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData &metric_data) {
378+
for (const auto &data_attr : metric_data.point_data_attr_)
379+
{
380+
const auto &data = opentelemetry::nostd::get<Base2ExponentialHistogramPointData>(data_attr.point_data);
381+
if (opentelemetry::nostd::get<std::string>(
382+
data_attr.attributes.find("RequestType")->second) == "GET")
383+
{
384+
EXPECT_EQ(data.sum_, expected_total_get_requests);
385+
count_attributes++;
386+
}
387+
else if (opentelemetry::nostd::get<std::string>(
388+
data_attr.attributes.find("RequestType")->second) == "PUT")
389+
{
390+
EXPECT_EQ(data.sum_, expected_total_put_requests);
391+
count_attributes++;
392+
}
393+
}
394+
return true;
395+
});
396+
EXPECT_EQ(count_attributes, 2); // GET and PUT
397+
398+
// In case of delta temporarily, subsequent collection would contain new data points, so resetting
399+
// the counts
400+
if (temporality == AggregationTemporality::kDelta)
401+
{
402+
expected_total_get_requests = 0;
403+
expected_total_put_requests = 0;
404+
}
405+
406+
// collect one more time.
407+
collection_ts = std::chrono::system_clock::now();
408+
count_attributes = 0;
409+
storage.Collect(
410+
collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData &metric_data) {
411+
if (temporality == AggregationTemporality::kCumulative)
412+
{
413+
EXPECT_EQ(metric_data.start_ts, sdk_start_ts);
414+
}
415+
for (const auto &data_attr : metric_data.point_data_attr_)
416+
{
417+
const auto &data = opentelemetry::nostd::get<Base2ExponentialHistogramPointData>(data_attr.point_data);
418+
if (opentelemetry::nostd::get<std::string>(
419+
data_attr.attributes.find("RequestType")->second) == "GET")
420+
{
421+
count_attributes++;
422+
EXPECT_EQ(data.sum_, expected_total_get_requests);
423+
}
424+
else if (opentelemetry::nostd::get<std::string>(
425+
data_attr.attributes.find("RequestType")->second) == "PUT")
426+
{
427+
count_attributes++;
428+
EXPECT_EQ(data.sum_, expected_total_put_requests);
429+
}
430+
}
431+
return true;
432+
});
433+
if (temporality == AggregationTemporality::kCumulative)
434+
{
435+
EXPECT_EQ(count_attributes, 2); // GET AND PUT
436+
}
437+
438+
storage.RecordDouble(50.0,
439+
KeyValueIterableView<std::map<std::string, std::string>>(attributes_get),
440+
opentelemetry::context::Context{});
441+
expected_total_get_requests += 50;
442+
storage.RecordDouble(40.0,
443+
KeyValueIterableView<std::map<std::string, std::string>>(attributes_put),
444+
opentelemetry::context::Context{});
445+
expected_total_put_requests += 40;
446+
447+
collection_ts = std::chrono::system_clock::now();
448+
count_attributes = 0;
449+
storage.Collect(
450+
collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData &metric_data) {
451+
for (const auto &data_attr : metric_data.point_data_attr_)
452+
{
453+
const auto &data = opentelemetry::nostd::get<Base2ExponentialHistogramPointData>(data_attr.point_data);
454+
if (opentelemetry::nostd::get<std::string>(
455+
data_attr.attributes.find("RequestType")->second) == "GET")
456+
{
457+
EXPECT_EQ(data.sum_, expected_total_get_requests);
458+
count_attributes++;
459+
}
460+
else if (opentelemetry::nostd::get<std::string>(
461+
data_attr.attributes.find("RequestType")->second) == "PUT")
462+
{
463+
EXPECT_EQ(data.sum_, expected_total_put_requests);
464+
count_attributes++;
465+
}
466+
}
467+
return true;
468+
});
469+
EXPECT_EQ(count_attributes, 2); // GET and PUT
470+
}
471+
INSTANTIATE_TEST_SUITE_P(WritableMetricStorageHistogramTestBase2ExponentialDouble,
326472
WritableMetricStorageHistogramTestFixture,
327473
::testing::Values(AggregationTemporality::kCumulative,
328474
AggregationTemporality::kDelta));

0 commit comments

Comments
 (0)