10 #include <unordered_map>
13 #include "opentelemetry/common/key_value_iterable.h"
14 #include "opentelemetry/nostd/function_ref.h"
15 #include "opentelemetry/sdk/common/attribute_utils.h"
16 #include "opentelemetry/sdk/common/attributemap_hash.h"
17 #include "opentelemetry/sdk/metrics/aggregation/aggregation.h"
18 #include "opentelemetry/sdk/metrics/state/filtered_ordered_attribute_map.h"
19 #include "opentelemetry/sdk/metrics/view/attributes_processor.h"
20 #include "opentelemetry/version.h"
28 using opentelemetry::sdk::common::OrderedAttributeMap;
46 template <
typename CustomHash = MetricAttributesHash>
51 : attributes_limit_(attributes_limit)
55 hash_map_.reserve(attributes_limit_);
61 auto it = hash_map_.find(attributes);
62 if (it != hash_map_.end())
64 return it->second.get();
75 return hash_map_.find(attributes) != hash_map_.end();
84 const opentelemetry::common::KeyValueIterable &attributes,
92 auto it = hash_map_.find(attr);
93 if (it != hash_map_.end())
95 return it->second.get();
98 if (IsOverflowAttributes(attr))
100 return GetOrSetOveflowAttributes(aggregation_callback);
103 auto result = hash_map_.emplace(
std::move(attr), aggregation_callback());
104 return result.first->second.get();
111 auto it = hash_map_.find(attributes);
112 if (it != hash_map_.end())
114 return it->second.get();
117 if (IsOverflowAttributes(attributes))
119 return GetOrSetOveflowAttributes(aggregation_callback);
122 hash_map_[attributes] = aggregation_callback();
123 return hash_map_[attributes].get();
130 auto it = hash_map_.find(attributes);
131 if (it != hash_map_.end())
133 return it->second.get();
136 if (IsOverflowAttributes(attributes))
138 return GetOrSetOveflowAttributes(aggregation_callback);
141 auto result = hash_map_.emplace(
std::move(attributes), aggregation_callback());
142 return result.first->second.get();
147 void Set(
const opentelemetry::common::KeyValueIterable &attributes,
149 std::unique_ptr<Aggregation> aggr)
156 auto it = hash_map_.find(attributes);
157 if (it != hash_map_.end())
161 else if (IsOverflowAttributes(attributes))
173 auto it = hash_map_.find(attributes);
174 if (it != hash_map_.end())
178 else if (IsOverflowAttributes(attributes))
194 for (
auto &kv : hash_map_)
196 if (!callback(kv.first, *(kv.second.get())))
207 size_t Size() {
return hash_map_.size(); }
210 size_t BucketCount() {
return hash_map_.bucket_count(); }
211 size_t BucketSize(
size_t n) {
return hash_map_.bucket_size(n); }
215 std::unordered_map<MetricAttributes, std::unique_ptr<Aggregation>, CustomHash> hash_map_;
216 size_t attributes_limit_;
218 Aggregation *GetOrSetOveflowAttributes(
221 auto agg = aggregation_callback();
222 return GetOrSetOveflowAttributes(
std::move(agg));
225 Aggregation *GetOrSetOveflowAttributes(std::unique_ptr<Aggregation> agg)
228 if (it != hash_map_.end())
230 return it->second.get();
234 return result.first->second.get();
251 return hash_map_.size() >= attributes_limit_;
256 return (hash_map_.size() + 1) >= attributes_limit_;