@@ -30,6 +30,10 @@ using M = std::map<std::string, std::string>;
30
30
class WritableMetricStorageTestFixture : public ::testing::TestWithParam<AggregationTemporality>
31
31
{};
32
32
33
+ class WritableMetricStorageTestUpDownFixture
34
+ : public ::testing::TestWithParam<AggregationTemporality>
35
+ {};
36
+
33
37
class WritableMetricStorageTestObservableGaugeFixture
34
38
: public ::testing::TestWithParam<AggregationTemporality>
35
39
{};
@@ -124,6 +128,97 @@ INSTANTIATE_TEST_SUITE_P(WritableMetricStorageTestLong,
124
128
::testing::Values (AggregationTemporality::kCumulative ,
125
129
AggregationTemporality::kDelta ));
126
130
131
+ TEST_P (WritableMetricStorageTestUpDownFixture, TestAggregation)
132
+ {
133
+ AggregationTemporality temporality = GetParam ();
134
+
135
+ InstrumentDescriptor instr_desc = {" name" , " desc" , " 1unit" ,
136
+ InstrumentType::kObservableUpDownCounter ,
137
+ InstrumentValueType::kLong };
138
+
139
+ auto sdk_start_ts = std::chrono::system_clock::now ();
140
+ // Some computation here
141
+ auto collection_ts = std::chrono::system_clock::now () + std::chrono::seconds (5 );
142
+
143
+ std::shared_ptr<CollectorHandle> collector (new MockCollectorHandle (temporality));
144
+ std::vector<std::shared_ptr<CollectorHandle>> collectors;
145
+ collectors.push_back (collector);
146
+
147
+ opentelemetry::sdk::metrics::AsyncMetricStorage storage (instr_desc, AggregationType::kDefault ,
148
+ nullptr );
149
+ int64_t get_count1 = 20 ;
150
+ int64_t put_count1 = 10 ;
151
+ std::unordered_map<MetricAttributes, int64_t , AttributeHashGenerator> measurements1 = {
152
+ {{{" RequestType" , " GET" }}, get_count1}, {{{" RequestType" , " PUT" }}, put_count1}};
153
+ storage.RecordLong (measurements1,
154
+ opentelemetry::common::SystemTimestamp (std::chrono::system_clock::now ()));
155
+
156
+ storage.Collect (
157
+ collector.get (), collectors, sdk_start_ts, collection_ts, [&](const MetricData &metric_data) {
158
+ for (const auto &data_attr : metric_data.point_data_attr_ )
159
+ {
160
+ const auto &data = opentelemetry::nostd::get<SumPointData>(data_attr.point_data );
161
+ if (opentelemetry::nostd::get<std::string>(
162
+ data_attr.attributes .find (" RequestType" )->second ) == " GET" )
163
+ {
164
+ EXPECT_EQ (opentelemetry::nostd::get<int64_t >(data.value_ ), get_count1);
165
+ }
166
+ else if (opentelemetry::nostd::get<std::string>(
167
+ data_attr.attributes .find (" RequestType" )->second ) == " PUT" )
168
+ {
169
+ EXPECT_EQ (opentelemetry::nostd::get<int64_t >(data.value_ ), put_count1);
170
+ }
171
+ }
172
+ return true ;
173
+ });
174
+ // subsequent recording after collection shouldn't fail
175
+ // monotonic increasing values;
176
+ int64_t get_count2 = -50 ;
177
+ int64_t put_count2 = -70 ;
178
+
179
+ std::unordered_map<MetricAttributes, int64_t , AttributeHashGenerator> measurements2 = {
180
+ {{{" RequestType" , " GET" }}, get_count2}, {{{" RequestType" , " PUT" }}, put_count2}};
181
+ storage.RecordLong (measurements2,
182
+ opentelemetry::common::SystemTimestamp (std::chrono::system_clock::now ()));
183
+ storage.Collect (
184
+ collector.get (), collectors, sdk_start_ts, collection_ts, [&](const MetricData &metric_data) {
185
+ for (const auto &data_attr : metric_data.point_data_attr_ )
186
+ {
187
+ const auto &data = opentelemetry::nostd::get<SumPointData>(data_attr.point_data );
188
+ if (opentelemetry::nostd::get<std::string>(
189
+ data_attr.attributes .find (" RequestType" )->second ) == " GET" )
190
+ {
191
+ if (temporality == AggregationTemporality::kCumulative )
192
+ {
193
+ EXPECT_EQ (opentelemetry::nostd::get<int64_t >(data.value_ ), get_count2);
194
+ }
195
+ else
196
+ {
197
+ EXPECT_EQ (opentelemetry::nostd::get<int64_t >(data.value_ ), get_count2 - get_count1);
198
+ }
199
+ }
200
+ else if (opentelemetry::nostd::get<std::string>(
201
+ data_attr.attributes .find (" RequestType" )->second ) == " PUT" )
202
+ {
203
+ if (temporality == AggregationTemporality::kCumulative )
204
+ {
205
+ EXPECT_EQ (opentelemetry::nostd::get<int64_t >(data.value_ ), put_count2);
206
+ }
207
+ else
208
+ {
209
+ EXPECT_EQ (opentelemetry::nostd::get<int64_t >(data.value_ ), put_count2 - put_count1);
210
+ }
211
+ }
212
+ }
213
+ return true ;
214
+ });
215
+ }
216
+
217
+ INSTANTIATE_TEST_SUITE_P (WritableMetricStorageTestUpDownLong,
218
+ WritableMetricStorageTestUpDownFixture,
219
+ ::testing::Values (AggregationTemporality::kCumulative ,
220
+ AggregationTemporality::kDelta ));
221
+
127
222
TEST_P (WritableMetricStorageTestObservableGaugeFixture, TestAggregation)
128
223
{
129
224
AggregationTemporality temporality = GetParam ();
0 commit comments