Skip to content

Commit 13f01ca

Browse files
authored
Add exemplar reservoir to async metric storage (#2319)
1 parent 0563a71 commit 13f01ca

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "opentelemetry/nostd/shared_ptr.h"
1111
#include "opentelemetry/sdk/common/attributemap_hash.h"
1212
#include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
13+
#include "opentelemetry/sdk/metrics/exemplar/reservoir.h"
1314
#include "opentelemetry/sdk/metrics/instruments.h"
1415
#include "opentelemetry/sdk/metrics/observer_result.h"
1516
#include "opentelemetry/sdk/metrics/state/attributes_hashmap.h"
@@ -29,11 +30,16 @@ class AsyncMetricStorage : public MetricStorage, public AsyncWritableMetricStora
2930
public:
3031
AsyncMetricStorage(InstrumentDescriptor instrument_descriptor,
3132
const AggregationType aggregation_type,
33+
nostd::shared_ptr<ExemplarReservoir> &&exemplar_reservoir
34+
OPENTELEMETRY_MAYBE_UNUSED,
3235
const AggregationConfig *aggregation_config)
3336
: instrument_descriptor_(instrument_descriptor),
3437
aggregation_type_{aggregation_type},
3538
cumulative_hash_map_(new AttributesHashMap()),
3639
delta_hash_map_(new AttributesHashMap()),
40+
#ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
41+
exemplar_reservoir_(exemplar_reservoir),
42+
#endif
3743
temporal_metric_storage_(instrument_descriptor, aggregation_type, aggregation_config)
3844
{}
3945

@@ -47,6 +53,11 @@ class AsyncMetricStorage : public MetricStorage, public AsyncWritableMetricStora
4753
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(hashmap_lock_);
4854
for (auto &measurement : measurements)
4955
{
56+
#ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
57+
exemplar_reservoir_->OfferMeasurement(measurement.second, {}, {},
58+
std::chrono::system_clock::now());
59+
#endif
60+
5061
auto aggr = DefaultAggregation::CreateAggregation(aggregation_type_, instrument_descriptor_);
5162
aggr->Aggregate(measurement.second);
5263
auto hash = opentelemetry::sdk::common::GetHashForAttributeMap(measurement.first);
@@ -119,6 +130,9 @@ class AsyncMetricStorage : public MetricStorage, public AsyncWritableMetricStora
119130
std::unique_ptr<AttributesHashMap> cumulative_hash_map_;
120131
std::unique_ptr<AttributesHashMap> delta_hash_map_;
121132
opentelemetry::common::SpinLockMutex hashmap_lock_;
133+
#ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
134+
nostd::shared_ptr<ExemplarReservoir> exemplar_reservoir_;
135+
#endif
122136
TemporalMetricStorage temporal_metric_storage_;
123137
};
124138

sdk/src/metrics/meter.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,8 @@ std::unique_ptr<AsyncWritableMetricStorage> Meter::RegisterAsyncMetricStorage(
368368
view_instr_desc.description_ = view.GetDescription();
369369
}
370370
auto storage = std::shared_ptr<AsyncMetricStorage>(new AsyncMetricStorage(
371-
view_instr_desc, view.GetAggregationType(), view.GetAggregationConfig()));
371+
view_instr_desc, view.GetAggregationType(), ExemplarReservoir::GetNoExemplarReservoir(),
372+
view.GetAggregationConfig()));
372373
storage_registry_[instrument_descriptor.name_] = storage;
373374
static_cast<AsyncMultiMetricStorage *>(storages.get())->AddStorage(storage);
374375
return true;

sdk/test/metrics/async_metric_storage_test.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "opentelemetry/common/key_value_iterable_view.h"
77
#include "opentelemetry/sdk/metrics/async_instruments.h"
8+
#include "opentelemetry/sdk/metrics/exemplar/reservoir.h"
89
#include "opentelemetry/sdk/metrics/instruments.h"
910
#include "opentelemetry/sdk/metrics/meter_context.h"
1011
#include "opentelemetry/sdk/metrics/metric_reader.h"
@@ -53,8 +54,8 @@ TEST_P(WritableMetricStorageTestFixture, TestAggregation)
5354
std::vector<std::shared_ptr<CollectorHandle>> collectors;
5455
collectors.push_back(collector);
5556

56-
opentelemetry::sdk::metrics::AsyncMetricStorage storage(instr_desc, AggregationType::kSum,
57-
nullptr);
57+
opentelemetry::sdk::metrics::AsyncMetricStorage storage(
58+
instr_desc, AggregationType::kSum, ExemplarReservoir::GetNoExemplarReservoir(), nullptr);
5859
int64_t get_count1 = 20;
5960
int64_t put_count1 = 10;
6061
std::unordered_map<MetricAttributes, int64_t, AttributeHashGenerator> measurements1 = {
@@ -144,8 +145,8 @@ TEST_P(WritableMetricStorageTestUpDownFixture, TestAggregation)
144145
std::vector<std::shared_ptr<CollectorHandle>> collectors;
145146
collectors.push_back(collector);
146147

147-
opentelemetry::sdk::metrics::AsyncMetricStorage storage(instr_desc, AggregationType::kDefault,
148-
nullptr);
148+
opentelemetry::sdk::metrics::AsyncMetricStorage storage(
149+
instr_desc, AggregationType::kDefault, ExemplarReservoir::GetNoExemplarReservoir(), nullptr);
149150
int64_t get_count1 = 20;
150151
int64_t put_count1 = 10;
151152
std::unordered_map<MetricAttributes, int64_t, AttributeHashGenerator> measurements1 = {
@@ -234,8 +235,9 @@ TEST_P(WritableMetricStorageTestObservableGaugeFixture, TestAggregation)
234235
std::vector<std::shared_ptr<CollectorHandle>> collectors;
235236
collectors.push_back(collector);
236237

237-
opentelemetry::sdk::metrics::AsyncMetricStorage storage(instr_desc, AggregationType::kLastValue,
238-
nullptr);
238+
opentelemetry::sdk::metrics::AsyncMetricStorage storage(
239+
instr_desc, AggregationType::kLastValue, ExemplarReservoir::GetNoExemplarReservoir(),
240+
nullptr);
239241
int64_t freq_cpu0 = 3;
240242
int64_t freq_cpu1 = 5;
241243
std::unordered_map<MetricAttributes, int64_t, AttributeHashGenerator> measurements1 = {

0 commit comments

Comments
 (0)