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"
18 #if OPENTELEMETRY_HAVE_WORKING_REGEX
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 =
',';
59 size_t cnt = kv_str_tokenizer.
NumTokens();
60 if (cnt > kMaxKeyValuePairs)
69 while (kv_str_tokenizer.
next(kv_valid, key, value) && ts->kv_properties_->Size() < cnt)
71 if (kv_valid ==
false)
76 if (!IsValidKey(key) || !IsValidValue(value))
83 ts->kv_properties_->AddEntry(key, value);
96 kv_properties_->GetAllEntries(
100 header_s.append(1, kMembersSeparator);
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()));
120 if (!IsValidKey(key))
125 return kv_properties_->GetValue(key, value);
141 auto curr_size = kv_properties_->Size();
142 if (!IsValidKey(key) || !IsValidValue(value))
147 auto allocate_size = curr_size;
148 if (curr_size < kMaxKeyValuePairs)
153 if (curr_size < kMaxKeyValuePairs)
156 ts->kv_properties_->AddEntry(key, value);
160 ts->kv_properties_->AddEntry(key, value);
174 if (!IsValidKey(key))
178 auto curr_size = kv_properties_->Size();
179 auto allocate_size = curr_size;
181 if (kv_properties_->GetValue(key, unused))
186 kv_properties_->GetAllEntries(
189 ts->kv_properties_->AddEntry(e_key, e_value);
196 bool Empty() const noexcept {
return kv_properties_->Size() == 0; }
203 return kv_properties_->GetAllEntries(callback);
214 #if OPENTELEMETRY_HAVE_WORKING_REGEX
215 return IsValidKeyRegEx(key);
217 return IsValidKeyNonRegEx(key);
227 #if OPENTELEMETRY_HAVE_WORKING_REGEX
228 return IsValidValueRegEx(value);
230 return IsValidValueNonRegEx(value);
236 TraceState(
size_t size) : kv_properties_(new
common::KeyValueProperties(
size)) {}
240 while (str[
static_cast<std::size_t
>(right)] ==
' ' && left < right)
244 while (str[
static_cast<std::size_t
>(left)] ==
' ' && left < right)
248 return str.
substr(left, right - left + 1);
251 #if OPENTELEMETRY_HAVE_WORKING_REGEX
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))
268 static std::regex reg_value(
269 "^[\\x20-\\x2B\\x2D-\\x3C\\x3E-\\x7E]{0,255}[\\x21-\\x2B\\x2D-\\x3C\\x3E-\\x7E]$");
271 return std::regex_match(std::string(value.
data(), value.
size()), reg_value);
276 if (key.
empty() || key.
size() > kKeyMaxSize || !IsLowerCaseAlphaOrDigit(key[0]))
283 for (
const char c : key)
285 if (!IsLowerCaseAlphaOrDigit(c) && c !=
'_' && c !=
'-' && c !=
'@' && c !=
'*' && c !=
'/')
289 if ((c ==
'@') && (++ats > 1))
299 if (value.
empty() || value.
size() > kValueMaxSize)
304 for (
const char c : value)
306 if (c < ' ' || c >
'~' || c ==
',' || c ==
'=')
315 static bool IsLowerCaseAlphaOrDigit(
char c) {
return isdigit(c) || islower(c); }