NVIDIA DeepStream SDK API Reference

9.2 Release
9.2/includes/opentelemetry/trace/trace_state.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 <ctype.h>
7 #include <cstddef>
8 #include <string>
9 #include <vector>
10 
11 #include "opentelemetry/common/kv_properties.h"
12 #include "opentelemetry/nostd/function_ref.h"
13 #include "opentelemetry/nostd/shared_ptr.h"
14 #include "opentelemetry/nostd/string_view.h"
15 #include "opentelemetry/nostd/unique_ptr.h"
16 #include "opentelemetry/version.h"
17 
18 #if OPENTELEMETRY_HAVE_WORKING_REGEX
19 # include <regex>
20 #endif
21 
23 namespace trace
24 {
25 
34 class OPENTELEMETRY_EXPORT TraceState
35 {
36 public:
37  static constexpr int kKeyMaxSize = 256;
38  static constexpr int kValueMaxSize = 256;
39  static constexpr int kMaxKeyValuePairs = 32;
40  static constexpr auto kKeyValueSeparator = '=';
41  static constexpr auto kMembersSeparator = ',';
42 
44  {
46  return ts;
47  }
48 
56  {
57 
58  common::KeyValueStringTokenizer kv_str_tokenizer(header);
59  size_t cnt = kv_str_tokenizer.NumTokens(); // upper bound on number of kv pairs
60  if (cnt > kMaxKeyValuePairs)
61  {
62  // trace state should be discarded if count exceeds
63  return GetDefault();
64  }
65 
67  bool kv_valid;
68  nostd::string_view key, value;
69  while (kv_str_tokenizer.next(kv_valid, key, value) && ts->kv_properties_->Size() < cnt)
70  {
71  if (kv_valid == false)
72  {
73  return GetDefault();
74  }
75 
76  if (!IsValidKey(key) || !IsValidValue(value))
77  {
78  // invalid header. return empty TraceState
79  ts->kv_properties_.reset(new common::KeyValueProperties());
80  break;
81  }
82 
83  ts->kv_properties_->AddEntry(key, value);
84  }
85 
86  return ts;
87  }
88 
92  std::string ToHeader() const noexcept
93  {
94  std::string header_s;
95  bool first = true;
96  kv_properties_->GetAllEntries(
97  [&header_s, &first](nostd::string_view key, nostd::string_view value) noexcept {
98  if (!first)
99  {
100  header_s.append(1, kMembersSeparator);
101  }
102  else
103  {
104  first = false;
105  }
106  header_s.append(std::string(key.data(), key.size()));
107  header_s.append(1, kKeyValueSeparator);
108  header_s.append(std::string(value.data(), value.size()));
109  return true;
110  });
111  return header_s;
112  }
113 
118  bool Get(nostd::string_view key, std::string &value) const noexcept
119  {
120  if (!IsValidKey(key))
121  {
122  return false;
123  }
124 
125  return kv_properties_->GetValue(key, value);
126  }
127 
139  const nostd::string_view &value) noexcept
140  {
141  auto curr_size = kv_properties_->Size();
142  if (!IsValidKey(key) || !IsValidValue(value))
143  {
144  // max size reached or invalid key/value. Returning empty TraceState
145  return TraceState::GetDefault();
146  }
147  auto allocate_size = curr_size;
148  if (curr_size < kMaxKeyValuePairs)
149  {
150  allocate_size += 1;
151  }
152  nostd::shared_ptr<TraceState> ts(new TraceState(allocate_size));
153  if (curr_size < kMaxKeyValuePairs)
154  {
155  // add new field first
156  ts->kv_properties_->AddEntry(key, value);
157  }
158  // add rest of the fields.
159  kv_properties_->GetAllEntries([&ts](nostd::string_view key, nostd::string_view value) {
160  ts->kv_properties_->AddEntry(key, value);
161  return true;
162  });
163  return ts;
164  }
165 
173  {
174  if (!IsValidKey(key))
175  {
176  return TraceState::GetDefault();
177  }
178  auto curr_size = kv_properties_->Size();
179  auto allocate_size = curr_size;
180  std::string unused;
181  if (kv_properties_->GetValue(key, unused))
182  {
183  allocate_size -= 1;
184  }
185  nostd::shared_ptr<TraceState> ts(new TraceState(allocate_size));
186  kv_properties_->GetAllEntries(
187  [&ts, &key](nostd::string_view e_key, nostd::string_view e_value) {
188  if (key != e_key)
189  ts->kv_properties_->AddEntry(e_key, e_value);
190  return true;
191  });
192  return ts;
193  }
194 
195  // Returns true if there are no keys, false otherwise.
196  bool Empty() const noexcept { return kv_properties_->Size() == 0; }
197 
198  // @return all key-values entris by repeatedly invoking the function reference passed as argument
199  // for each entry
201  nostd::function_ref<bool(nostd::string_view, nostd::string_view)> callback) const noexcept
202  {
203  return kv_properties_->GetAllEntries(callback);
204  }
213  {
214 #if OPENTELEMETRY_HAVE_WORKING_REGEX
215  return IsValidKeyRegEx(key);
216 #else
217  return IsValidKeyNonRegEx(key);
218 #endif
219  }
220 
225  static bool IsValidValue(nostd::string_view value)
226  {
227 #if OPENTELEMETRY_HAVE_WORKING_REGEX
228  return IsValidValueRegEx(value);
229 #else
230  return IsValidValueNonRegEx(value);
231 #endif
232  }
233 
234 private:
235  TraceState() : kv_properties_(new common::KeyValueProperties()) {}
236  TraceState(size_t size) : kv_properties_(new common::KeyValueProperties(size)) {}
237 
238  static nostd::string_view TrimString(nostd::string_view str, size_t left, size_t right)
239  {
240  while (str[static_cast<std::size_t>(right)] == ' ' && left < right)
241  {
242  right--;
243  }
244  while (str[static_cast<std::size_t>(left)] == ' ' && left < right)
245  {
246  left++;
247  }
248  return str.substr(left, right - left + 1);
249  }
250 
251 #if OPENTELEMETRY_HAVE_WORKING_REGEX
252  static bool IsValidKeyRegEx(nostd::string_view key)
253  {
254  static std::regex reg_key("^[a-z0-9][a-z0-9*_\\-/]{0,255}$");
255  static std::regex reg_key_multitenant(
256  "^[a-z0-9][a-z0-9*_\\-/]{0,240}(@)[a-z0-9][a-z0-9*_\\-/]{0,13}$");
257  std::string key_s(key.data(), key.size());
258  if (std::regex_match(key_s, reg_key) || std::regex_match(key_s, reg_key_multitenant))
259  {
260  return true;
261  }
262  return false;
263  }
264 
265  static bool IsValidValueRegEx(nostd::string_view value)
266  {
267  // Hex 0x20 to 0x2B, 0x2D to 0x3C, 0x3E to 0x7E
268  static std::regex reg_value(
269  "^[\\x20-\\x2B\\x2D-\\x3C\\x3E-\\x7E]{0,255}[\\x21-\\x2B\\x2D-\\x3C\\x3E-\\x7E]$");
270  // Need to benchmark without regex, as a string object is created here.
271  return std::regex_match(std::string(value.data(), value.size()), reg_value);
272  }
273 #else
274  static bool IsValidKeyNonRegEx(nostd::string_view key)
275  {
276  if (key.empty() || key.size() > kKeyMaxSize || !IsLowerCaseAlphaOrDigit(key[0]))
277  {
278  return false;
279  }
280 
281  int ats = 0;
282 
283  for (const char c : key)
284  {
285  if (!IsLowerCaseAlphaOrDigit(c) && c != '_' && c != '-' && c != '@' && c != '*' && c != '/')
286  {
287  return false;
288  }
289  if ((c == '@') && (++ats > 1))
290  {
291  return false;
292  }
293  }
294  return true;
295  }
296 
297  static bool IsValidValueNonRegEx(nostd::string_view value)
298  {
299  if (value.empty() || value.size() > kValueMaxSize)
300  {
301  return false;
302  }
303 
304  for (const char c : value)
305  {
306  if (c < ' ' || c > '~' || c == ',' || c == '=')
307  {
308  return false;
309  }
310  }
311  return true;
312  }
313 #endif
314 
315  static bool IsLowerCaseAlphaOrDigit(char c) { return isdigit(c) || islower(c); }
316 
317 private:
318  // Store entries in a C-style array to avoid using std::array or std::vector.
320 };
321 
322 } // namespace trace
nostd::function_ref
Definition: includes/opentelemetry/nostd/function_ref.h:17
trace
Definition: includes/opentelemetry/context/context_value.h:18
OPENTELEMETRY_EXPORT
#define OPENTELEMETRY_EXPORT
Definition: includes/opentelemetry/common/macros.h:386
trace::TraceState::GetAllEntries
bool GetAllEntries(nostd::function_ref< bool(nostd::string_view, nostd::string_view)> callback) const noexcept
Definition: 9.2/includes/opentelemetry/trace/trace_state.h:200
nostd::string_view::empty
bool empty() const noexcept
Definition: includes/opentelemetry/nostd/string_view.h:58
nostd::unique_ptr< common::KeyValueProperties >
trace::TraceState::Set
nostd::shared_ptr< TraceState > Set(const nostd::string_view &key, const nostd::string_view &value) noexcept
Returns shared_ptr of new TraceState object with following mutations applied to the existing instance...
Definition: 9.2/includes/opentelemetry/trace/trace_state.h:138
common::KeyValueStringTokenizer
Definition: includes/opentelemetry/common/kv_properties.h:30
nostd::shared_ptr
Provide a type-erased version of std::shared_ptr that has ABI stability.
Definition: includes/opentelemetry/nostd/shared_ptr.h:28
trace::TraceState::Get
bool Get(nostd::string_view key, std::string &value) const noexcept
Returns value associated with key passed as argument Returns empty string if key is invalid or not fo...
Definition: 9.2/includes/opentelemetry/trace/trace_state.h:118
trace::TraceState::IsValidKey
static bool IsValidKey(nostd::string_view key)
Returns whether key is a valid key.
Definition: 9.2/includes/opentelemetry/trace/trace_state.h:212
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
nostd::string_view::substr
string_view substr(size_type pos, size_type n=npos) const
Definition: includes/opentelemetry/nostd/string_view.h:70
trace::TraceState::GetDefault
static OPENTELEMETRY_API_SINGLETON nostd::shared_ptr< TraceState > GetDefault()
Definition: 9.2/includes/opentelemetry/trace/trace_state.h:43
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
nostd::string_view::size
size_type size() const noexcept
Definition: includes/opentelemetry/nostd/string_view.h:62
common::KeyValueStringTokenizer::next
bool next(bool &valid_kv, nostd::string_view &key, nostd::string_view &value) noexcept
Definition: includes/opentelemetry/common/kv_properties.h:50
pydeepstreamdoc::tensor::TensorDoc::size
constexpr const char * size
Definition: service-maker/sources/python/src/bindings/docstrings/tensordoc.h:46
OPENTELEMETRY_API_SINGLETON
#define OPENTELEMETRY_API_SINGLETON
Definition: includes/opentelemetry/common/macros.h:335
trace::TraceState::FromHeader
static nostd::shared_ptr< TraceState > FromHeader(nostd::string_view header) noexcept
Returns shared_ptr to a newly created TraceState parsed from the header provided.
Definition: 9.2/includes/opentelemetry/trace/trace_state.h:55
trace::TraceState
TraceState carries tracing-system specific context in a list of key-value pairs.
Definition: includes/opentelemetry/trace/trace_state.h:34
trace::TraceState::Delete
nostd::shared_ptr< TraceState > Delete(const nostd::string_view &key) noexcept
Returns shared_ptr to a new TraceState object after removing the attribute with given key ( if presen...
Definition: 9.2/includes/opentelemetry/trace/trace_state.h:172
trace::TraceState::ToHeader
std::string ToHeader() const noexcept
Creates a w3c tracestate header from TraceState object.
Definition: 9.2/includes/opentelemetry/trace/trace_state.h:92
nostd::string_view::data
const char * data() const noexcept
Definition: includes/opentelemetry/nostd/string_view.h:56
common::KeyValueStringTokenizer::NumTokens
size_t NumTokens() const noexcept
Definition: includes/opentelemetry/common/kv_properties.h:108
trace::TraceState::IsValidValue
static bool IsValidValue(nostd::string_view value)
Returns whether value is a valid value.
Definition: 9.2/includes/opentelemetry/trace/trace_state.h:225
common::KeyValueProperties
Definition: includes/opentelemetry/common/kv_properties.h:136
trace::TraceState::Empty
bool Empty() const noexcept
Definition: 9.2/includes/opentelemetry/trace/trace_state.h:196
common
Definition: includes/opentelemetry/common/attribute_value.h:14
nostd::string_view
Back port of std::string_view to work with pre-cpp-17 compilers.
Definition: includes/opentelemetry/nostd/string_view.h:37