22// SPDX-License-Identifier: Apache-2.0
33
44#include " opentelemetry/sdk/resource/resource_detector.h"
5- # include " opentelemetry/nostd/variant.h "
5+
66#include " opentelemetry/sdk/common/env_variables.h"
77#include " opentelemetry/sdk/resource/resource.h"
88#include " opentelemetry/semconv/service_attributes.h"
99#include " opentelemetry/version.h"
1010
11- #include < cctype>
12- #include < cstddef>
11+ #include " src/resource/detail/percent_decode.h"
12+
13+ #include " opentelemetry/nostd/variant.h"
14+
15+ #include < stddef.h>
1316#include < sstream>
1417#include < string>
1518
@@ -22,43 +25,6 @@ namespace resource
2225constexpr const char *kOtelResourceAttributes = " OTEL_RESOURCE_ATTRIBUTES" ;
2326constexpr const char *kOtelServiceName = " OTEL_SERVICE_NAME" ;
2427
25- namespace
26- {
27- unsigned char HexDigitValue (unsigned char c) noexcept
28- {
29- return static_cast <unsigned char >(std::isdigit (c) ? c - ' 0' : std::toupper (c) - ' A' + 10 );
30- }
31-
32- // Percent-decodes a value from OTEL_RESOURCE_ATTRIBUTES, per the W3C Baggage value grammar
33- // the resource spec defers to
34- // (https://github.com/open-telemetry/opentelemetry-specification/pull/2670). A malformed escape
35- // sequence is left in the output as-is rather than dropping the attribute, since this parser has
36- // never validated attribute syntax.
37- std::string PercentDecode (const std::string &value)
38- {
39- std::string decoded;
40- decoded.reserve (value.size ());
41- for (std::size_t i = 0 ; i < value.size (); ++i)
42- {
43- if (value[i] == ' %' && i + 2 < value.size ())
44- {
45- unsigned char high = static_cast <unsigned char >(value[i + 1 ]);
46- unsigned char low = static_cast <unsigned char >(value[i + 2 ]);
47- if (std::isxdigit (high) && std::isxdigit (low))
48- {
49- unsigned char unescaped_value =
50- static_cast <unsigned char >((HexDigitValue (high) << 4 ) | HexDigitValue (low));
51- decoded.push_back (static_cast <char >(unescaped_value));
52- i += 2 ;
53- continue ;
54- }
55- }
56- decoded.push_back (value[i]);
57- }
58- return decoded;
59- }
60- } // namespace
61-
6228Resource ResourceDetector::Create (const ResourceAttributes &attributes,
6329 const std::string &schema_url)
6430{
@@ -71,6 +37,7 @@ Resource OTELResourceDetector::Detect() noexcept
7137
7238 bool attributes_exists = opentelemetry::sdk::common::GetStringEnvironmentVariable (
7339 kOtelResourceAttributes , attributes_str);
40+
7441 bool service_name_exists =
7542 opentelemetry::sdk::common::GetStringEnvironmentVariable (kOtelServiceName , service_name);
7643
@@ -85,14 +52,17 @@ Resource OTELResourceDetector::Detect() noexcept
8552 {
8653 std::istringstream iss (attributes_str);
8754 std::string token;
55+
8856 while (std::getline (iss, token, ' ,' ))
8957 {
9058 size_t pos = token.find (' =' );
59+
9160 if (pos != std::string::npos)
9261 {
9362 std::string key = token.substr (0 , pos);
9463 std::string value = token.substr (pos + 1 );
95- attributes[key] = PercentDecode (value);
64+
65+ attributes[key] = detail::PercentDecode (value);
9666 }
9767 }
9868 }
0 commit comments