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
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include <gtest/gtest.h>
#include <string>
#include "opentelemetry/common/attribute_value.h"
#include "opentelemetry/nostd/utility.h"
#include "opentelemetry/sdk/metrics/instruments.h"
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
# include <map>
# include <memory>
# include "common.h"
# include "opentelemetry/common/key_value_iterable_view.h"
# include "opentelemetry/nostd/shared_ptr.h"
# include "opentelemetry/sdk/metrics/data/point_data.h"
# include "opentelemetry/sdk/metrics/state/sync_metric_storage.h"
# include "opentelemetry/sdk/metrics/view/attributes_processor.h"
#endif
using namespace opentelemetry::sdk::metrics;
using namespace opentelemetry::common;
namespace nostd = opentelemetry::nostd;
class WritableMetricStorageTestFixture : public ::testing::TestWithParam<AggregationTemporality>
{};
TEST_P(WritableMetricStorageTestFixture, LongGaugeLastValueAggregation)
{
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
AggregationTemporality temporality = GetParam();
auto sdk_start_ts = std::chrono::system_clock::now();
InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kGauge,
InstrumentValueType::kLong};
std::map<std::string, std::string> attributes_roomA = {{"Room.id", "Rack A"}};
std::map<std::string, std::string> attributes_roomB = {{"Room.id", "Rack B"}};
std::unique_ptr<DefaultAttributesProcessor> default_attributes_processor{
new DefaultAttributesProcessor{}};
opentelemetry::sdk::metrics::SyncMetricStorage storage(
instr_desc, AggregationType::kLastValue, default_attributes_processor.get(),
# ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
ExemplarFilterType::kAlwaysOff, ExemplarReservoir::GetNoExemplarReservoir(),
# endif
nullptr);
int64_t bg_noise_level_1_roomA = 10;
int64_t bg_noise_level_1_roomB = 20;
storage.RecordLong(bg_noise_level_1_roomA,
KeyValueIterableView<std::map<std::string, std::string>>(attributes_roomA),
opentelemetry::context::Context{});
storage.RecordLong(bg_noise_level_1_roomB,
KeyValueIterableView<std::map<std::string, std::string>>(attributes_roomB),
opentelemetry::context::Context{});
int64_t bg_noise_level_2_roomA = 43;
int64_t bg_noise_level_2_roomB = 25;
storage.RecordLong(bg_noise_level_2_roomA,
KeyValueIterableView<std::map<std::string, std::string>>(attributes_roomA),
opentelemetry::context::Context{});
storage.RecordLong(bg_noise_level_2_roomB,
KeyValueIterableView<std::map<std::string, std::string>>(attributes_roomB),
opentelemetry::context::Context{});
std::shared_ptr<CollectorHandle> collector(new MockCollectorHandle(temporality));
std::vector<std::shared_ptr<CollectorHandle>> collectors;
collectors.push_back(collector);
// Some computation here
auto collection_ts = std::chrono::system_clock::now();
size_t count_attributes = 0;
storage.Collect(
collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData &metric_data) {
for (const auto &data_attr : metric_data.point_data_attr_)
{
auto lastvalue_data = opentelemetry::nostd::get<LastValuePointData>(data_attr.point_data);
if (opentelemetry::nostd::get<std::string>(
data_attr.attributes.find("Room.id")->second) == "Rack A")
{
if (temporality == AggregationTemporality::kCumulative)
{
EXPECT_EQ(opentelemetry::nostd::get<int64_t>(lastvalue_data.value_),
bg_noise_level_2_roomA);
}
count_attributes++;
}
else if (opentelemetry::nostd::get<std::string>(
data_attr.attributes.find("Room.id")->second) == "Rack B")
{
if (temporality == AggregationTemporality::kCumulative)
{
EXPECT_EQ(opentelemetry::nostd::get<int64_t>(lastvalue_data.value_),
bg_noise_level_2_roomB);
}
count_attributes++;
}
}
return true;
});
EXPECT_EQ(count_attributes, 2); // RackA and RackB
#else
EXPECT_TRUE(true);
#endif
}
INSTANTIATE_TEST_SUITE_P(WritableMetricStorageTestLong,
WritableMetricStorageTestFixture,
::testing::Values(AggregationTemporality::kCumulative));
TEST_P(WritableMetricStorageTestFixture, DoubleGaugeLastValueAggregation)
{
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
AggregationTemporality temporality = GetParam();
auto sdk_start_ts = std::chrono::system_clock::now();
InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kGauge,
InstrumentValueType::kDouble};
std::map<std::string, std::string> attributes_roomA = {{"Room.id", "Rack A"}};
std::map<std::string, std::string> attributes_roomB = {{"Room.id", "Rack B"}};
std::unique_ptr<DefaultAttributesProcessor> default_attributes_processor{
new DefaultAttributesProcessor{}};
opentelemetry::sdk::metrics::SyncMetricStorage storage(
instr_desc, AggregationType::kLastValue, default_attributes_processor.get(),
# ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
ExemplarFilterType::kAlwaysOff, ExemplarReservoir::GetNoExemplarReservoir(),
# endif
nullptr);
double bg_noise_level_1_roomA = 4.3;
double bg_noise_level_1_roomB = 2.5;
storage.RecordDouble(bg_noise_level_1_roomA,
KeyValueIterableView<std::map<std::string, std::string>>(attributes_roomA),
opentelemetry::context::Context{});
storage.RecordDouble(bg_noise_level_1_roomB,
KeyValueIterableView<std::map<std::string, std::string>>(attributes_roomB),
opentelemetry::context::Context{});
double bg_noise_level_2_roomA = 10.5;
double bg_noise_level_2_roomB = 20.5;
storage.RecordDouble(bg_noise_level_2_roomA,
KeyValueIterableView<std::map<std::string, std::string>>(attributes_roomA),
opentelemetry::context::Context{});
storage.RecordDouble(bg_noise_level_2_roomB,
KeyValueIterableView<std::map<std::string, std::string>>(attributes_roomB),
opentelemetry::context::Context{});
std::shared_ptr<CollectorHandle> collector(new MockCollectorHandle(temporality));
std::vector<std::shared_ptr<CollectorHandle>> collectors;
collectors.push_back(collector);
// Some computation here
auto collection_ts = std::chrono::system_clock::now();
size_t count_attributes = 0;
storage.Collect(
collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData &metric_data) {
for (const auto &data_attr : metric_data.point_data_attr_)
{
auto lastvalue_data = opentelemetry::nostd::get<LastValuePointData>(data_attr.point_data);
if (opentelemetry::nostd::get<std::string>(
data_attr.attributes.find("Room.id")->second) == "Rack A")
{
if (temporality == AggregationTemporality::kCumulative)
{
EXPECT_DOUBLE_EQ(opentelemetry::nostd::get<double>(lastvalue_data.value_),
bg_noise_level_2_roomA);
}
count_attributes++;
}
else if (opentelemetry::nostd::get<std::string>(
data_attr.attributes.find("Room.id")->second) == "Rack B")
{
if (temporality == AggregationTemporality::kCumulative)
{
EXPECT_DOUBLE_EQ(opentelemetry::nostd::get<double>(lastvalue_data.value_),
bg_noise_level_2_roomB);
}
count_attributes++;
}
}
return true;
});
EXPECT_EQ(count_attributes, 2); // RackA and RackB
#else
EXPECT_TRUE(true);
#endif
}
INSTANTIATE_TEST_SUITE_P(WritableMetricStorageTestDouble,
WritableMetricStorageTestFixture,
::testing::Values(AggregationTemporality::kCumulative));
|