NVIDIA DeepStream SDK API Reference

9.2 Release
includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h
Go to the documentation of this file.
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #include <stddef.h>
7 #include <functional>
8 #include <memory>
9 #include <string>
10 #include <unordered_map>
11 #include <utility>
12 
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"
21 
23 namespace sdk
24 {
25 namespace metrics
26 {
27 
28 using opentelemetry::sdk::common::OrderedAttributeMap;
29 
30 constexpr size_t kAggregationCardinalityLimit = 2000;
31 const std::string kAttributesLimitOverflowKey = "otel.metrics.overflow";
35  kAttributesLimitOverflowValue}}; // precalculated for optimization
36 
38 {
39 public:
40  size_t operator()(const MetricAttributes &attributes) const
41  {
43  }
44 };
45 
46 template <typename CustomHash = MetricAttributesHash>
48 {
49 public:
51  : attributes_limit_(attributes_limit)
52  {
53  if (attributes_limit_ > kAggregationCardinalityLimit)
54  {
55  hash_map_.reserve(attributes_limit_);
56  }
57  }
58 
59  Aggregation *Get(const MetricAttributes &attributes) const
60  {
61  auto it = hash_map_.find(attributes);
62  if (it != hash_map_.end())
63  {
64  return it->second.get();
65  }
66  return nullptr;
67  }
68 
73  bool Has(const MetricAttributes &attributes) const
74  {
75  return hash_map_.find(attributes) != hash_map_.end();
76  }
77 
84  const opentelemetry::common::KeyValueIterable &attributes,
85  const AttributesProcessor *attributes_processor,
86  nostd::function_ref<std::unique_ptr<Aggregation>()> aggregation_callback)
87  {
88  // TODO: avoid constructing MetricAttributes from KeyValueIterable for
89  // hash_map_.find which is a heavy operation
90  MetricAttributes attr{attributes, attributes_processor};
91 
92  auto it = hash_map_.find(attr);
93  if (it != hash_map_.end())
94  {
95  return it->second.get();
96  }
97 
98  if (IsOverflowAttributes(attr))
99  {
100  return GetOrSetOveflowAttributes(aggregation_callback);
101  }
102 
103  auto result = hash_map_.emplace(std::move(attr), aggregation_callback());
104  return result.first->second.get();
105  }
106 
108  const MetricAttributes &attributes,
109  nostd::function_ref<std::unique_ptr<Aggregation>()> aggregation_callback)
110  {
111  auto it = hash_map_.find(attributes);
112  if (it != hash_map_.end())
113  {
114  return it->second.get();
115  }
116 
117  if (IsOverflowAttributes(attributes))
118  {
119  return GetOrSetOveflowAttributes(aggregation_callback);
120  }
121 
122  hash_map_[attributes] = aggregation_callback();
123  return hash_map_[attributes].get();
124  }
125 
127  MetricAttributes &&attributes,
128  nostd::function_ref<std::unique_ptr<Aggregation>()> aggregation_callback)
129  {
130  auto it = hash_map_.find(attributes);
131  if (it != hash_map_.end())
132  {
133  return it->second.get();
134  }
135 
136  if (IsOverflowAttributes(attributes))
137  {
138  return GetOrSetOveflowAttributes(aggregation_callback);
139  }
140 
141  auto result = hash_map_.emplace(std::move(attributes), aggregation_callback());
142  return result.first->second.get();
143  }
147  void Set(const opentelemetry::common::KeyValueIterable &attributes,
148  const AttributesProcessor *attributes_processor,
149  std::unique_ptr<Aggregation> aggr)
150  {
151  Set(MetricAttributes{attributes, attributes_processor}, std::move(aggr));
152  }
153 
154  void Set(const MetricAttributes &attributes, std::unique_ptr<Aggregation> aggr)
155  {
156  auto it = hash_map_.find(attributes);
157  if (it != hash_map_.end())
158  {
159  it->second = std::move(aggr);
160  }
161  else if (IsOverflowAttributes(attributes))
162  {
163  hash_map_[kOverflowAttributes] = std::move(aggr);
164  }
165  else
166  {
167  hash_map_[attributes] = std::move(aggr);
168  }
169  }
170 
171  void Set(MetricAttributes &&attributes, std::unique_ptr<Aggregation> aggr)
172  {
173  auto it = hash_map_.find(attributes);
174  if (it != hash_map_.end())
175  {
176  it->second = std::move(aggr);
177  }
178  else if (IsOverflowAttributes(attributes))
179  {
180  hash_map_[kOverflowAttributes] = std::move(aggr);
181  }
182  else
183  {
184  hash_map_[std::move(attributes)] = std::move(aggr);
185  }
186  }
187 
192  nostd::function_ref<bool(const MetricAttributes &, Aggregation &)> callback) const
193  {
194  for (auto &kv : hash_map_)
195  {
196  if (!callback(kv.first, *(kv.second.get())))
197  {
198  return false; // callback is not prepared to consume data
199  }
200  }
201  return true;
202  }
203 
207  size_t Size() { return hash_map_.size(); }
208 
209 #ifdef UNIT_TESTING
210  size_t BucketCount() { return hash_map_.bucket_count(); }
211  size_t BucketSize(size_t n) { return hash_map_.bucket_size(n); }
212 #endif
213 
214 private:
215  std::unordered_map<MetricAttributes, std::unique_ptr<Aggregation>, CustomHash> hash_map_;
216  size_t attributes_limit_;
217 
218  Aggregation *GetOrSetOveflowAttributes(
219  nostd::function_ref<std::unique_ptr<Aggregation>()> aggregation_callback)
220  {
221  auto agg = aggregation_callback();
222  return GetOrSetOveflowAttributes(std::move(agg));
223  }
224 
225  Aggregation *GetOrSetOveflowAttributes(std::unique_ptr<Aggregation> agg)
226  {
227  auto it = hash_map_.find(kOverflowAttributes);
228  if (it != hash_map_.end())
229  {
230  return it->second.get();
231  }
232 
233  auto result = hash_map_.emplace(kOverflowAttributes, std::move(agg));
234  return result.first->second.get();
235  }
236 
237  bool IsOverflowAttributes(const MetricAttributes &attributes) const
238  {
239  // If the incoming attributes are exactly the overflow sentinel, route
240  // directly to the overflow entry.
241  if (attributes == kOverflowAttributes)
242  {
243  return true;
244  }
245  // Determine if overflow entry already exists.
246  bool has_overflow = (hash_map_.find(kOverflowAttributes) != hash_map_.end());
247  // If overflow already present, total size already includes it; trigger overflow
248  // when current size (including overflow) is >= limit.
249  if (has_overflow)
250  {
251  return hash_map_.size() >= attributes_limit_;
252  }
253  // If overflow not present yet, simulate adding a new distinct key. If that
254  // would exceed the limit, we redirect to overflow instead of creating a
255  // new real attribute entry.
256  return (hash_map_.size() + 1) >= attributes_limit_;
257  }
258 };
259 
261 
262 } // namespace metrics
263 
264 } // namespace sdk
nostd::function_ref
Definition: includes/opentelemetry/nostd/function_ref.h:17
sdk::metrics::AttributesHashMapWithCustomHash::Get
Aggregation * Get(const MetricAttributes &attributes) const
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:59
sdk::metrics::AttributeHashGenerator
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:37
sdk::metrics::AttributesHashMapWithCustomHash::AttributesHashMapWithCustomHash
AttributesHashMapWithCustomHash(size_t attributes_limit=kAggregationCardinalityLimit)
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:50
sdk::metrics::kOverflowAttributes
const MetricAttributes kOverflowAttributes
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:33
sdk::metrics::AttributesHashMapWithCustomHash::GetOrSetDefault
Aggregation * GetOrSetDefault(const MetricAttributes &attributes, nostd::function_ref< std::unique_ptr< Aggregation >()> aggregation_callback)
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:107
sdk::common::GetHashForAttributeMap
size_t GetHashForAttributeMap(const OrderedAttributeMap &attribute_map)
Definition: includes/opentelemetry/sdk/common/attributemap_hash.h:62
sdk::metrics::AttributesHashMapWithCustomHash::Size
size_t Size()
Return the size of hash.
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:207
sdk::metrics::AttributesHashMapWithCustomHash::GetAllEnteries
bool GetAllEnteries(nostd::function_ref< bool(const MetricAttributes &, Aggregation &)> callback) const
Iterate the hash to yield key and value stored in hash.
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:191
sdk::metrics::kAttributesLimitOverflowValue
const bool kAttributesLimitOverflowValue
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:32
sdk::metrics::AttributesHashMapWithCustomHash::Set
void Set(const MetricAttributes &attributes, std::unique_ptr< Aggregation > aggr)
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:154
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
sdk::metrics::AttributesHashMapWithCustomHash
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:47
sdk::metrics::AttributesHashMapWithCustomHash::Set
void Set(const opentelemetry::common::KeyValueIterable &attributes, const AttributesProcessor *attributes_processor, std::unique_ptr< Aggregation > aggr)
Set the value for given key, overwriting the value if already present.
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:147
sdk::metrics::AttributesHashMapWithCustomHash::Set
void Set(MetricAttributes &&attributes, std::unique_ptr< Aggregation > aggr)
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:171
sdk::metrics::AttributesHashMapWithCustomHash::GetOrSetDefault
Aggregation * GetOrSetDefault(MetricAttributes &&attributes, nostd::function_ref< std::unique_ptr< Aggregation >()> aggregation_callback)
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:126
sdk::metrics::MetricAttributes
opentelemetry::sdk::metrics::FilteredOrderedAttributeMap MetricAttributes
Definition: includes/opentelemetry/sdk/metrics/instruments.h:216
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
absl::move
constexpr absl::OTABSL_OPTION_NAMESPACE_NAME::remove_reference_t< T > && move(T &&t) noexcept
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:199
sdk::metrics::Aggregation
Definition: includes/opentelemetry/sdk/metrics/aggregation/aggregation.h:16
sdk::metrics::AttributesHashMapWithCustomHash::Has
bool Has(const MetricAttributes &attributes) const
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:73
sdk::metrics::AttributesProcessor
The AttributesProcessor is responsible for customizing which attribute(s) are to be reported as metri...
Definition: includes/opentelemetry/sdk/metrics/view/attributes_processor.h:37
sdk
Definition: includes/opentelemetry/sdk/common/atomic_shared_ptr.h:13
sdk::metrics::AttributesHashMapWithCustomHash::GetOrSetDefault
Aggregation * GetOrSetDefault(const opentelemetry::common::KeyValueIterable &attributes, const AttributesProcessor *attributes_processor, nostd::function_ref< std::unique_ptr< Aggregation >()> aggregation_callback)
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:83
metrics
Definition: includes/opentelemetry/metrics/async_instruments.h:10
sdk::metrics::AttributeHashGenerator::operator()
size_t operator()(const MetricAttributes &attributes) const
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:40
sdk::metrics::kAggregationCardinalityLimit
constexpr size_t kAggregationCardinalityLimit
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:30
sdk::metrics::kAttributesLimitOverflowKey
const std::string kAttributesLimitOverflowKey
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:31