NVIDIA DeepStream SDK API Reference

9.2 Release
includes/opentelemetry/sdk/metrics/instruments.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 <algorithm>
7 #include <cctype>
8 #include <functional>
9 #include <string>
10 
11 #include "opentelemetry/sdk/metrics/state/filtered_ordered_attribute_map.h"
12 #include "opentelemetry/version.h"
13 
15 namespace sdk
16 {
17 namespace metrics
18 {
19 enum class InstrumentType
20 {
21  kCounter,
22  kHistogram,
27  kGauge
28 };
29 
30 enum class InstrumentClass
31 {
32  kSync,
33  kAsync
34 };
35 
37 {
38  kInt,
39  kLong,
40  kFloat,
41  kDouble
42 };
43 
44 enum class AggregationType
45 {
46  kDrop,
47  kHistogram,
48  kLastValue,
49  kSum,
50  kDefault,
52 };
53 
55 {
57  kDelta,
59 };
60 
62 {
63  std::string name_;
64  std::string description_;
65  std::string unit_;
68 };
69 
71 {
72  // Case-insensitive comparison of two ASCII strings used to evaluate equality of instrument names
73  static bool CaseInsensitiveAsciiEquals(const std::string &lhs, const std::string &rhs) noexcept
74  {
75  return lhs.size() == rhs.size() &&
76  std::equal(lhs.begin(), lhs.end(), rhs.begin(), [](char a, char b) {
77  return std::tolower(static_cast<unsigned char>(a)) ==
78  std::tolower(static_cast<unsigned char>(b));
79  });
80  }
81 
82  // Implementation of the specification requirements on duplicate instruments
83  // An instrument is a duplicate if it has the same name (case-insensitive) as another instrument,
84  // but different instrument kind, unit, or description.
85  // https://github.com/open-telemetry/opentelemetry-specification/blob/9c8c30631b0e288de93df7452f91ed47f6fba330/specification/metrics/sdk.md?plain=1#L869
86  static bool IsDuplicate(const InstrumentDescriptor &lhs, const InstrumentDescriptor &rhs) noexcept
87  {
88  // Not a duplicate if case-insensitive names are not equal
90  {
91  return false;
92  }
93 
94  // Duplicate if names equal and kinds (Type and ValueType) are not equal
95  if (lhs.type_ != rhs.type_ || lhs.value_type_ != rhs.value_type_)
96  {
97  return true;
98  }
99 
100  // Duplicate if names equal and units (case-sensitive) are not equal
101  if (lhs.unit_ != rhs.unit_)
102  {
103  return true;
104  }
105 
106  // Duplicate if names equal and descriptions (case-sensitive) are not equal
107  if (lhs.description_ != rhs.description_)
108  {
109  return true;
110  }
111 
112  // All identifying fields are equal
113  // These are identical instruments or only have a name case conflict
114  return false;
115  }
116 
118  {
119  switch (type)
120  {
122  return "Counter";
124  return "UpDownCounter";
126  return "Histogram";
128  return "ObservableCounter";
130  return "ObservableUpDownCounter";
132  return "ObservableGauge";
134  return "Gauge";
135  default:
136  return "Unknown";
137  }
138  }
139 
141  InstrumentValueType value_type) noexcept
142  {
143  switch (value_type)
144  {
146  return "Int";
148  return "Long";
150  return "Float";
152  return "Double";
153  default:
154  return "Unknown";
155  }
156  }
157 };
158 
160 {
161  bool operator()(const InstrumentDescriptor &lhs, const InstrumentDescriptor &rhs) const noexcept
162  {
163  // Names (case-insensitive)
164  if (!InstrumentDescriptorUtil::CaseInsensitiveAsciiEquals(lhs.name_, rhs.name_))
165  {
166  return false;
167  }
168 
169  // Kinds (Type and ValueType)
170  if (lhs.type_ != rhs.type_ || lhs.value_type_ != rhs.value_type_)
171  {
172  return false;
173  }
174 
175  // Units (case-sensitive)
176  if (lhs.unit_ != rhs.unit_)
177  {
178  return false;
179  }
180 
181  // Descriptions (case-sensitive)
182  if (lhs.description_ != rhs.description_)
183  {
184  return false;
185  }
186 
187  // All identifying fields are equal
188  return true;
189  }
190 };
191 
192 // Hash for InstrumentDescriptor
193 // Identical instruments must have the same hash value
194 // Two instruments are identical when all identifying fields (case-insensitive name , kind,
195 // description, unit) are equal.
197 {
198  std::size_t operator()(const InstrumentDescriptor &instrument_descriptor) const noexcept
199  {
200  std::size_t hashcode{};
201 
202  for (char c : instrument_descriptor.name_)
203  {
204  sdk::common::GetHash(hashcode,
205  static_cast<char>(std::tolower(static_cast<unsigned char>(c))));
206  }
207 
208  sdk::common::GetHash(hashcode, instrument_descriptor.description_);
209  sdk::common::GetHash(hashcode, instrument_descriptor.unit_);
210  sdk::common::GetHash(hashcode, static_cast<uint32_t>(instrument_descriptor.type_));
211  sdk::common::GetHash(hashcode, static_cast<uint32_t>(instrument_descriptor.value_type_));
212  return hashcode;
213  }
214 };
215 
216 using MetricAttributes = opentelemetry::sdk::metrics::FilteredOrderedAttributeMap;
217 using MetricAttributesHash = opentelemetry::sdk::metrics::FilteredOrderedAttributeMapHash;
219 
220 /*class InstrumentSelector {
221 public:
222 InstrumentSelector(opentelemetry::nostd::string_view name,
223 opentelemetry::sdk::metrics::InstrumentType type): name_(name.data()), type_(type) {} InstrumentType
224 GetType(){return type_;} std::string GetNameFilter() { return name_;}
225 
226 private:
227 std::string name_;
228 InstrumentType type_;
229 };*/
230 } // namespace metrics
231 } // namespace sdk
sdk::metrics::InstrumentDescriptor::type_
InstrumentType type_
Definition: includes/opentelemetry/sdk/metrics/instruments.h:66
sdk::metrics::AggregationType
AggregationType
Definition: includes/opentelemetry/sdk/metrics/instruments.h:44
sdk::metrics::InstrumentClass
InstrumentClass
Definition: includes/opentelemetry/sdk/metrics/instruments.h:30
sdk::metrics::InstrumentEqualNameCaseInsensitive
Definition: includes/opentelemetry/sdk/metrics/instruments.h:159
sdk::metrics::InstrumentValueType::kDouble
@ kDouble
sdk::metrics::AggregationType::kDrop
@ kDrop
sdk::metrics::InstrumentType::kObservableGauge
@ kObservableGauge
sdk::metrics::InstrumentType::kCounter
@ kCounter
sdk::metrics::InstrumentDescriptorHash::operator()
std::size_t operator()(const InstrumentDescriptor &instrument_descriptor) const noexcept
Definition: includes/opentelemetry/sdk/metrics/instruments.h:198
sdk::metrics::InstrumentDescriptorHash
Definition: includes/opentelemetry/sdk/metrics/instruments.h:196
sdk::metrics::AggregationTemporality
AggregationTemporality
Definition: includes/opentelemetry/sdk/metrics/instruments.h:54
sdk::metrics::InstrumentDescriptorUtil
Definition: includes/opentelemetry/sdk/metrics/instruments.h:70
sdk::metrics::InstrumentEqualNameCaseInsensitive::operator()
bool operator()(const InstrumentDescriptor &lhs, const InstrumentDescriptor &rhs) const noexcept
Definition: includes/opentelemetry/sdk/metrics/instruments.h:161
sdk::metrics::InstrumentDescriptorUtil::CaseInsensitiveAsciiEquals
static bool CaseInsensitiveAsciiEquals(const std::string &lhs, const std::string &rhs) noexcept
Definition: includes/opentelemetry/sdk/metrics/instruments.h:73
sdk::metrics::AggregationType::kLastValue
@ kLastValue
sdk::metrics::InstrumentDescriptorUtil::GetInstrumentTypeString
static opentelemetry::nostd::string_view GetInstrumentTypeString(InstrumentType type) noexcept
Definition: includes/opentelemetry/sdk/metrics/instruments.h:117
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
sdk::metrics::InstrumentClass::kSync
@ kSync
sdk::metrics::AggregationTemporalitySelector
std::function< AggregationTemporality(InstrumentType)> AggregationTemporalitySelector
Definition: includes/opentelemetry/sdk/metrics/instruments.h:218
sdk::metrics::AggregationType::kHistogram
@ kHistogram
sdk::metrics::InstrumentDescriptor::description_
std::string description_
Definition: includes/opentelemetry/sdk/metrics/instruments.h:64
sdk::metrics::InstrumentValueType::kLong
@ kLong
sdk::metrics::InstrumentType::kHistogram
@ kHistogram
sdk::metrics::MetricAttributes
opentelemetry::sdk::metrics::FilteredOrderedAttributeMap MetricAttributes
Definition: includes/opentelemetry/sdk/metrics/instruments.h:216
sdk::metrics::InstrumentClass::kAsync
@ kAsync
sdk::metrics::AggregationTemporality::kCumulative
@ kCumulative
sdk::metrics::InstrumentValueType
InstrumentValueType
Definition: includes/opentelemetry/sdk/metrics/instruments.h:36
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
sdk::metrics::AggregationTemporality::kUnspecified
@ kUnspecified
sdk::metrics::InstrumentDescriptor::unit_
std::string unit_
Definition: includes/opentelemetry/sdk/metrics/instruments.h:65
sdk::metrics::InstrumentDescriptorUtil::GetInstrumentValueTypeString
static opentelemetry::nostd::string_view GetInstrumentValueTypeString(InstrumentValueType value_type) noexcept
Definition: includes/opentelemetry/sdk/metrics/instruments.h:140
sdk::metrics::MetricAttributesHash
opentelemetry::sdk::metrics::FilteredOrderedAttributeMapHash MetricAttributesHash
Definition: includes/opentelemetry/sdk/metrics/instruments.h:217
sdk::metrics::InstrumentValueType::kInt
@ kInt
sdk::metrics::InstrumentType::kGauge
@ kGauge
sdk::metrics::AggregationType::kBase2ExponentialHistogram
@ kBase2ExponentialHistogram
sdk::metrics::InstrumentDescriptor::name_
std::string name_
Definition: includes/opentelemetry/sdk/metrics/instruments.h:63
sdk::metrics::InstrumentType::kUpDownCounter
@ kUpDownCounter
sdk::metrics::InstrumentDescriptorUtil::IsDuplicate
static bool IsDuplicate(const InstrumentDescriptor &lhs, const InstrumentDescriptor &rhs) noexcept
Definition: includes/opentelemetry/sdk/metrics/instruments.h:86
sdk::metrics::AggregationTemporality::kDelta
@ kDelta
sdk
Definition: includes/opentelemetry/sdk/common/atomic_shared_ptr.h:13
sdk::metrics::AggregationType::kSum
@ kSum
metrics
Definition: includes/opentelemetry/metrics/async_instruments.h:10
sdk::metrics::InstrumentType::kObservableUpDownCounter
@ kObservableUpDownCounter
sdk::metrics::InstrumentDescriptor
Definition: includes/opentelemetry/sdk/metrics/instruments.h:61
sdk::metrics::AggregationType::kDefault
@ kDefault
sdk::metrics::InstrumentValueType::kFloat
@ kFloat
sdk::metrics::InstrumentType
InstrumentType
Definition: includes/opentelemetry/sdk/metrics/instruments.h:19
sdk::metrics::InstrumentDescriptor::value_type_
InstrumentValueType value_type_
Definition: includes/opentelemetry/sdk/metrics/instruments.h:67
nostd::string_view
std::string_view string_view
Definition: includes/opentelemetry/std/string_view.h:18
sdk::metrics::InstrumentType::kObservableCounter
@ kObservableCounter
sdk::common::GetHash
void GetHash(size_t &seed, const T &arg)
Definition: includes/opentelemetry/sdk/common/attributemap_hash.h:24