NVIDIA DeepStream SDK API Reference

9.2 Release
9.2/includes/opentelemetry/sdk/common/attribute_utils.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 <cstdint>
7 #include <initializer_list>
8 #include <map>
9 #include <string>
10 #include <unordered_map>
11 #include <utility>
12 #include <vector>
13 
14 #include "opentelemetry/common/attribute_value.h"
15 #include "opentelemetry/common/key_value_iterable.h"
16 #include "opentelemetry/nostd/span.h"
17 #include "opentelemetry/nostd/string_view.h"
18 #include "opentelemetry/nostd/variant.h"
19 #include "opentelemetry/version.h"
20 
22 namespace sdk
23 {
24 namespace common
25 {
37  int32_t,
38  uint32_t,
39  int64_t,
40  double,
41  std::string,
42  std::vector<bool>,
43  std::vector<int32_t>,
44  std::vector<uint32_t>,
45  std::vector<int64_t>,
46  std::vector<double>,
47  std::vector<std::string>,
48  uint64_t,
49  std::vector<uint64_t>,
50  std::vector<uint8_t>>;
51 
53 {
54  kTypeBool,
55  kTypeInt,
56  kTypeUInt,
57  kTypeInt64,
69 };
70 
74 struct AttributeConverter
75 {
83  {
84  return OwnedAttributeValue(std::string(v));
85  }
86  OwnedAttributeValue operator()(std::string v) { return OwnedAttributeValue(v); }
87  OwnedAttributeValue operator()(const char *v) { return OwnedAttributeValue(std::string(v)); }
88  OwnedAttributeValue operator()(nostd::span<const uint8_t> v) { return convertSpan<uint8_t>(v); }
89  OwnedAttributeValue operator()(nostd::span<const bool> v) { return convertSpan<bool>(v); }
90  OwnedAttributeValue operator()(nostd::span<const int32_t> v) { return convertSpan<int32_t>(v); }
91  OwnedAttributeValue operator()(nostd::span<const uint32_t> v) { return convertSpan<uint32_t>(v); }
92  OwnedAttributeValue operator()(nostd::span<const int64_t> v) { return convertSpan<int64_t>(v); }
93  OwnedAttributeValue operator()(nostd::span<const uint64_t> v) { return convertSpan<uint64_t>(v); }
94  OwnedAttributeValue operator()(nostd::span<const double> v) { return convertSpan<double>(v); }
96  {
97  return convertSpan<std::string>(v);
98  }
99 
100  template <typename T, typename U = T>
102  {
103  const std::vector<T> copy(vals.begin(), vals.end());
104  return OwnedAttributeValue(std::move(copy));
105  }
106 };
107 
113 struct AttributeEqualToVisitor
114 {
115  // Different types are not equal including containers of different element types
116  template <typename T, typename U>
117  bool operator()(const T &, const U &) const noexcept
118  {
119  return false;
120  }
121 
122  // Compare the same arithmetic types
123  template <typename T>
124  bool operator()(const T &owned_value, const T &value) const noexcept
125  {
126  return owned_value == value;
127  }
128 
129  // Compare std::string and const char*
130  bool operator()(const std::string &owned_value, const char *value) const noexcept
131  {
132  return owned_value == value;
133  }
134 
135  // Compare std::string and nostd::string_view
136  bool operator()(const std::string &owned_value, nostd::string_view value) const noexcept
137  {
138  return owned_value == value;
139  }
140 
141  // Compare std::vector<std::string> and nostd::span<const nostd::string_view>
142  bool operator()(const std::vector<std::string> &owned_value,
143  const nostd::span<const nostd::string_view> &value) const noexcept
144  {
145  return owned_value.size() == value.size() &&
146  std::equal(owned_value.begin(), owned_value.end(), value.begin(),
147  [](const std::string &owned_element, nostd::string_view element) {
148  return owned_element == element;
149  });
150  }
151 
152  // Compare nostd::span<const T> and std::vector<T> for arithmetic types
153  template <typename T>
154  bool operator()(const std::vector<T> &owned_value,
155  const nostd::span<const T> &value) const noexcept
156  {
157  return owned_value.size() == value.size() &&
158  std::equal(owned_value.begin(), owned_value.end(), value.begin());
159  }
160 };
161 
165 class AttributeMap : public std::unordered_map<std::string, OwnedAttributeValue>
166 {
167 public:
168  // Construct empty attribute map
169  AttributeMap() : std::unordered_map<std::string, OwnedAttributeValue>() {}
170 
171  // Construct attribute map and populate with attributes
172  AttributeMap(const opentelemetry::common::KeyValueIterable &attributes) : AttributeMap()
173  {
174  attributes.ForEachKeyValue(
176  SetAttribute(key, value);
177  return true;
178  });
179  }
180 
181  // Construct attribute map and populate with optional attributes
182  AttributeMap(const opentelemetry::common::KeyValueIterable *attributes) : AttributeMap()
183  {
184  if (attributes != nullptr)
185  {
186  attributes->ForEachKeyValue(
188  SetAttribute(key, value);
189  return true;
190  });
191  }
192  }
193 
194  // Construct map from initializer list by applying `SetAttribute` transform for every attribute
196  std::initializer_list<std::pair<nostd::string_view, opentelemetry::common::AttributeValue>>
197  attributes)
198  : AttributeMap()
199  {
200  for (auto &kv : attributes)
201  {
202  SetAttribute(kv.first, kv.second);
203  }
204  }
205 
206  // Returns a reference to this map
207  const std::unordered_map<std::string, OwnedAttributeValue> &GetAttributes() const noexcept
208  {
209  return (*this);
210  }
211 
212  // Convert non-owning key-value to owning std::string(key) and OwnedAttributeValue(value)
214  const opentelemetry::common::AttributeValue &value) noexcept
215  {
216  (*this)[std::string(key)] = nostd::visit(converter_, value);
217  }
218 
219  // Compare the attributes of this map with another KeyValueIterable
220  bool EqualTo(const opentelemetry::common::KeyValueIterable &attributes) const noexcept
221  {
222  if (attributes.size() != this->size())
223  {
224  return false;
225  }
226 
227  const bool is_equal = attributes.ForEachKeyValue(
228  [this](nostd::string_view key,
229  const opentelemetry::common::AttributeValue &value) noexcept {
230  // Perform a linear search to find the key assuming the map is small
231  // This avoids temporary string creation from this->find(std::string(key))
232  for (const auto &kv : *this)
233  {
234  if (kv.first == key)
235  {
236  // Order of arguments is important here. OwnedAttributeValue is first then
237  // AttributeValue AttributeEqualToVisitor does not support the reverse order
238  return nostd::visit(equal_to_visitor_, kv.second, value);
239  }
240  }
241  return false;
242  });
243 
244  return is_equal;
245  }
246 
247 private:
248  AttributeConverter converter_;
249  AttributeEqualToVisitor equal_to_visitor_;
250 };
251 
255 class OrderedAttributeMap : public std::map<std::string, OwnedAttributeValue>
256 {
257 public:
258  // Contruct empty attribute map
260 
261  // Contruct attribute map and populate with attributes
262  OrderedAttributeMap(const opentelemetry::common::KeyValueIterable &attributes)
264  {
265  attributes.ForEachKeyValue(
267  SetAttribute(key, value);
268  return true;
269  });
270  }
271 
272  // Construct map from initializer list by applying `SetAttribute` transform for every attribute
274  std::initializer_list<std::pair<nostd::string_view, opentelemetry::common::AttributeValue>>
275  attributes)
277  {
278  for (auto &kv : attributes)
279  {
280  SetAttribute(kv.first, kv.second);
281  }
282  }
283 
284  // Returns a reference to this map
285  const std::map<std::string, OwnedAttributeValue> &GetAttributes() const noexcept
286  {
287  return (*this);
288  }
289 
290  // Convert non-owning key-value to owning std::string(key) and OwnedAttributeValue(value)
292  const opentelemetry::common::AttributeValue &value) noexcept
293  {
294  (*this)[std::string(key)] = nostd::visit(converter_, value);
295  }
296 
297 private:
298  AttributeConverter converter_;
299 };
300 
301 } // namespace common
302 } // namespace sdk
sdk::common::AttributeMap::AttributeMap
AttributeMap(const opentelemetry::common::KeyValueIterable *attributes)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:182
nostd::span::end
T * end() const noexcept
Definition: includes/opentelemetry/nostd/span.h:184
sdk::common::OrderedAttributeMap
Class for storing attributes.
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:255
sdk::common::AttributeConverter::operator()
OwnedAttributeValue operator()(nostd::span< const bool > v)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:89
sdk::common::AttributeConverter::operator()
OwnedAttributeValue operator()(nostd::string_view v)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:82
sdk::common::AttributeConverter::operator()
OwnedAttributeValue operator()(int32_t v)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:77
sdk::common::AttributeMap::AttributeMap
AttributeMap()
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:169
sdk::common::OrderedAttributeMap::OrderedAttributeMap
OrderedAttributeMap()
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:259
sdk::common::kTypeUInt
@ kTypeUInt
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:56
absl::OTABSL_OPTION_NAMESPACE_NAME::variant
Definition: includes/opentelemetry/nostd/variant.h:61
sdk::common::AttributeMap::EqualTo
bool EqualTo(const opentelemetry::common::KeyValueIterable &attributes) const noexcept
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:220
nostd::span::begin
T * begin() const noexcept
Definition: includes/opentelemetry/nostd/span.h:182
sdk::common::OrderedAttributeMap::SetAttribute
void SetAttribute(nostd::string_view key, const opentelemetry::common::AttributeValue &value) noexcept
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:291
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
nostd::span
Back port of std::span.
Definition: includes/opentelemetry/nostd/span.h:51
common::AttributeValue
nostd::variant< bool, int32_t, int64_t, uint32_t, double, const char *, nostd::string_view, nostd::span< const bool >, nostd::span< const int32_t >, nostd::span< const int64_t >, nostd::span< const uint32_t >, nostd::span< const double >, nostd::span< const nostd::string_view >, uint64_t, nostd::span< const uint64_t >, nostd::span< const uint8_t > > AttributeValue
OpenTelemetry signals can be enriched by adding attributes.
Definition: includes/opentelemetry/common/attribute_value.h:58
sdk::common::kTypeSpanDouble
@ kTypeSpanDouble
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:64
sdk::common::kTypeInt64
@ kTypeInt64
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:57
sdk::common::AttributeMap::AttributeMap
AttributeMap(std::initializer_list< std::pair< nostd::string_view, opentelemetry::common::AttributeValue >> attributes)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:195
sdk::common::AttributeMap::GetAttributes
const std::unordered_map< std::string, OwnedAttributeValue > & GetAttributes() const noexcept
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:207
sdk::common::AttributeConverter::operator()
OwnedAttributeValue operator()(std::string v)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:86
sdk::common::AttributeMap::SetAttribute
void SetAttribute(nostd::string_view key, const opentelemetry::common::AttributeValue &value) noexcept
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:213
sdk::common::kTypeUInt64
@ kTypeUInt64
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:66
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
sdk::common::AttributeEqualToVisitor::operator()
bool operator()(const std::string &owned_value, nostd::string_view value) const noexcept
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:136
sdk::common::AttributeConverter::operator()
OwnedAttributeValue operator()(nostd::span< const uint64_t > v)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:93
sdk::common::AttributeConverter
Creates an owned copy (OwnedAttributeValue) of a non-owning AttributeValue.
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:74
sdk::common::AttributeEqualToVisitor::operator()
bool operator()(const std::vector< T > &owned_value, const nostd::span< const T > &value) const noexcept
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:154
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::common::AttributeConverter::convertSpan
OwnedAttributeValue convertSpan(nostd::span< const U > vals)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:101
sdk::common::AttributeEqualToVisitor
Evaluates if an owned value (from an OwnedAttributeValue) is equal to another value (from a non-ownin...
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:113
sdk::common::kTypeSpanBool
@ kTypeSpanBool
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:60
sdk::common::AttributeMap::AttributeMap
AttributeMap(const opentelemetry::common::KeyValueIterable &attributes)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:172
sdk::common::AttributeEqualToVisitor::operator()
bool operator()(const std::vector< std::string > &owned_value, const nostd::span< const nostd::string_view > &value) const noexcept
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:142
sdk::common::AttributeConverter::operator()
OwnedAttributeValue operator()(int64_t v)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:79
sdk::common::AttributeConverter::operator()
OwnedAttributeValue operator()(nostd::span< const uint8_t > v)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:88
sdk::common::AttributeConverter::operator()
OwnedAttributeValue operator()(nostd::span< const int32_t > v)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:90
sdk::common::kTypeSpanUInt64
@ kTypeSpanUInt64
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:67
nostd::visit
constexpr auto visit(_Callable &&_Obj, _Variants &&..._Args)
Definition: includes/opentelemetry/std/variant.h:196
sdk::common::OwnedAttributeValue
nostd::variant< bool, int32_t, uint32_t, int64_t, double, std::string, std::vector< bool >, std::vector< int32_t >, std::vector< uint32_t >, std::vector< int64_t >, std::vector< double >, std::vector< std::string >, uint64_t, std::vector< uint64_t >, std::vector< uint8_t > > OwnedAttributeValue
A counterpart to AttributeValue that makes sure a value is owned.
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:50
sdk::common::AttributeConverter::operator()
OwnedAttributeValue operator()(nostd::span< const double > v)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:94
std
Extensions to the C++ standard library.
Definition: includes/opentelemetry/nostd/string_view.h:212
sdk::common::kTypeSpanString
@ kTypeSpanString
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:65
sdk
Definition: includes/opentelemetry/sdk/common/atomic_shared_ptr.h:13
sdk::common::kTypeSpanInt
@ kTypeSpanInt
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:61
sdk::common::AttributeConverter::operator()
OwnedAttributeValue operator()(const char *v)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:87
sdk::common::AttributeConverter::operator()
OwnedAttributeValue operator()(uint32_t v)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:78
sdk::common::AttributeConverter::operator()
OwnedAttributeValue operator()(nostd::span< const nostd::string_view > v)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:95
sdk::common::AttributeConverter::operator()
OwnedAttributeValue operator()(nostd::span< const uint32_t > v)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:91
sdk::common::OrderedAttributeMap::OrderedAttributeMap
OrderedAttributeMap(std::initializer_list< std::pair< nostd::string_view, opentelemetry::common::AttributeValue >> attributes)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:273
sdk::common::kTypeSpanUInt
@ kTypeSpanUInt
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:62
sdk::common::OwnedAttributeType
OwnedAttributeType
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:52
sdk::common::OrderedAttributeMap::OrderedAttributeMap
OrderedAttributeMap(const opentelemetry::common::KeyValueIterable &attributes)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:262
sdk::common::AttributeConverter::operator()
OwnedAttributeValue operator()(double v)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:81
sdk::common::OrderedAttributeMap::GetAttributes
const std::map< std::string, OwnedAttributeValue > & GetAttributes() const noexcept
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:285
sdk::common::AttributeConverter::operator()
OwnedAttributeValue operator()(uint64_t v)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:80
sdk::common::kTypeSpanByte
@ kTypeSpanByte
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:68
sdk::common::AttributeEqualToVisitor::operator()
bool operator()(const T &owned_value, const T &value) const noexcept
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:124
sdk::common::AttributeConverter::operator()
OwnedAttributeValue operator()(bool v)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:76
sdk::common::AttributeMap
Class for storing attributes.
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:165
sdk::common::kTypeDouble
@ kTypeDouble
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:58
sdk::common::kTypeString
@ kTypeString
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:59
sdk::common::kTypeSpanInt64
@ kTypeSpanInt64
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:63
sdk::common::kTypeInt
@ kTypeInt
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:55
sdk::common::AttributeConverter::operator()
OwnedAttributeValue operator()(nostd::span< const int64_t > v)
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:92
common
Definition: includes/opentelemetry/common/attribute_value.h:14
sdk::common::kTypeBool
@ kTypeBool
Definition: includes/opentelemetry/sdk/common/attribute_utils.h:54
sdk::common::AttributeEqualToVisitor::operator()
bool operator()(const std::string &owned_value, const char *value) const noexcept
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:130
nostd::string_view
Back port of std::string_view to work with pre-cpp-17 compilers.
Definition: includes/opentelemetry/nostd/string_view.h:37
sdk::common::AttributeEqualToVisitor::operator()
bool operator()(const T &, const U &) const noexcept
Definition: 9.2/includes/opentelemetry/sdk/common/attribute_utils.h:117