12 #include "opentelemetry/common/key_value_iterable.h"
13 #include "opentelemetry/common/spin_lock_mutex.h"
14 #include "opentelemetry/common/timestamp.h"
15 #include "opentelemetry/context/context.h"
16 #include "opentelemetry/nostd/function_ref.h"
17 #include "opentelemetry/nostd/span.h"
18 #include "opentelemetry/nostd/string_view.h"
19 #include "opentelemetry/sdk/common/attributemap_hash.h"
20 #include "opentelemetry/sdk/metrics/aggregation/aggregation.h"
21 #include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h"
22 #include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
23 #include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h"
24 #include "opentelemetry/sdk/metrics/data/metric_data.h"
25 #include "opentelemetry/sdk/metrics/instruments.h"
26 #include "opentelemetry/sdk/metrics/state/attributes_hashmap.h"
27 #include "opentelemetry/sdk/metrics/state/metric_collector.h"
28 #include "opentelemetry/sdk/metrics/state/metric_storage.h"
29 #include "opentelemetry/sdk/metrics/state/temporal_metric_storage.h"
30 #include "opentelemetry/sdk/metrics/view/attributes_processor.h"
31 #include "opentelemetry/version.h"
33 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
34 # include "opentelemetry/sdk/metrics/exemplar/filter_type.h"
35 # include "opentelemetry/sdk/metrics/exemplar/reservoir.h"
46 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
48 static inline bool EnableExamplarFilter(ExemplarFilterType filter_type,
49 const opentelemetry::context::Context &
context)
51 return filter_type == ExemplarFilterType::kAlwaysOn ||
52 (filter_type == ExemplarFilterType::kTraceBased &&
57 #endif // ENABLE_METRICS_EXEMPLAR_PREVIEW
62 std::shared_ptr<const AttributesProcessor> attributes_processor,
63 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
64 ExemplarFilterType exempler_filter_type,
68 : instrument_descriptor_(instrument_descriptor),
72 attributes_processor_(
std::
move(attributes_processor)),
73 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
74 exemplar_filter_type_(exempler_filter_type),
75 exemplar_reservoir_(exemplar_reservoir),
77 temporal_metric_storage_(instrument_descriptor, aggregation_type, aggregation_config)
79 create_default_aggregation_ = [&, aggregation_type,
80 aggregation_config]() -> std::unique_ptr<Aggregation> {
87 const opentelemetry::context::Context &
context
94 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
95 if (EnableExamplarFilter(exemplar_filter_type_,
context))
97 exemplar_reservoir_->OfferMeasurement(value, {},
context, std::chrono::system_clock::now());
101 std::lock_guard<opentelemetry::common::SpinLockMutex> guard(attribute_hashmap_lock_);
102 attributes_hashmap_->GetOrSetDefault(attr, create_default_aggregation_)->Aggregate(value);
106 const opentelemetry::common::KeyValueIterable &attributes,
107 const opentelemetry::context::Context &
context
114 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
115 if (EnableExamplarFilter(exemplar_filter_type_,
context))
117 exemplar_reservoir_->OfferMeasurement(value, attributes,
context,
118 std::chrono::system_clock::now());
122 std::lock_guard<opentelemetry::common::SpinLockMutex> guard(attribute_hashmap_lock_);
124 ->GetOrSetDefault(attributes, attributes_processor_.get(), create_default_aggregation_)
129 const opentelemetry::context::Context &
context
136 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
137 if (EnableExamplarFilter(exemplar_filter_type_,
context))
139 exemplar_reservoir_->OfferMeasurement(value, {},
context, std::chrono::system_clock::now());
143 std::lock_guard<opentelemetry::common::SpinLockMutex> guard(attribute_hashmap_lock_);
144 attributes_hashmap_->GetOrSetDefault(attr, create_default_aggregation_)->Aggregate(value);
148 const opentelemetry::common::KeyValueIterable &attributes,
149 const opentelemetry::context::Context &
context
156 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
157 if (EnableExamplarFilter(exemplar_filter_type_,
context))
159 exemplar_reservoir_->OfferMeasurement(value, attributes,
context,
160 std::chrono::system_clock::now());
163 std::lock_guard<opentelemetry::common::SpinLockMutex> guard(attribute_hashmap_lock_);
165 ->GetOrSetDefault(attributes, attributes_processor_.get(), create_default_aggregation_)
170 nostd::span<std::shared_ptr<CollectorHandle>> collectors,
171 opentelemetry::common::SystemTimestamp sdk_start_ts,
172 opentelemetry::common::SystemTimestamp collection_ts,
179 std::unique_ptr<AttributesHashMap> attributes_hashmap_;
180 std::function<std::unique_ptr<Aggregation>()> create_default_aggregation_;
181 std::shared_ptr<const AttributesProcessor> attributes_processor_;
182 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
183 ExemplarFilterType exemplar_filter_type_;
187 opentelemetry::common::SpinLockMutex attribute_hashmap_lock_;