Skip to content

Commit 4fc1b77

Browse files
authored
[SDK] Percent-decode values from OTEL_RESOURCE_ATTRIBUTES (#4364)
1 parent 5cfda3c commit 4fc1b77

3 files changed

Lines changed: 90 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ Increment the:
1515

1616
## [Unreleased]
1717

18+
* [SDK] `OTELResourceDetector` now percent-decodes values parsed from the
19+
`OTEL_RESOURCE_ATTRIBUTES` environment variable, per the W3C Baggage value
20+
grammar the resource spec defers to. A malformed escape sequence is left
21+
in the value as-is rather than dropping the attribute.
22+
[#1536](https://github.com/open-telemetry/opentelemetry-cpp/issues/1536)
23+
1824
* [BUG] Report one outcome per request when a curl session is cancelled after
1925
the response arrives
2026
([#4363](https://github.com/open-telemetry/opentelemetry-cpp/pull/4363))
21-
2227
* [CODE HEALTH] Enable clang-tidy `modernize-deprecated-headers` and replace
2328
deprecated C headers (`stdint.h`, `stddef.h`, `stdlib.h`, `string.h`,
2429
`stdio.h`, `ctype.h`, `limits.h`, `assert.h`) with their C++ equivalents

‎sdk/src/resource/resource_detector.cc‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "opentelemetry/semconv/service_attributes.h"
99
#include "opentelemetry/version.h"
1010

11+
#include <cctype>
1112
#include <cstddef>
1213
#include <sstream>
1314
#include <string>
@@ -21,6 +22,43 @@ namespace resource
2122
constexpr const char *kOtelResourceAttributes = "OTEL_RESOURCE_ATTRIBUTES";
2223
constexpr const char *kOtelServiceName = "OTEL_SERVICE_NAME";
2324

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+
2462
Resource ResourceDetector::Create(const ResourceAttributes &attributes,
2563
const std::string &schema_url)
2664
{
@@ -54,7 +92,7 @@ Resource OTELResourceDetector::Detect() noexcept
5492
{
5593
std::string key = token.substr(0, pos);
5694
std::string value = token.substr(pos + 1);
57-
attributes[key] = value;
95+
attributes[key] = PercentDecode(value);
5896
}
5997
}
6098
}

‎sdk/test/resource/resource_test.cc‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,51 @@ TEST(ResourceTest, OtelResourceDetector)
238238
unsetenv("OTEL_RESOURCE_ATTRIBUTES");
239239
}
240240

241+
TEST(ResourceTest, OtelResourceDetectorPercentDecodesValues)
242+
{
243+
std::map<std::string, std::string> expected_attributes = {
244+
{"key1", "hello world"}, {"key2", "a,b"}, {"key3", "100%"}};
245+
246+
setenv("OTEL_RESOURCE_ATTRIBUTES", "key1=hello%20world,key2=a%2Cb,key3=100%25", 1);
247+
248+
OTELResourceDetector detector;
249+
auto resource = detector.Detect();
250+
auto received_attributes = resource.GetAttributes();
251+
for (auto &e : received_attributes)
252+
{
253+
EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end());
254+
if (expected_attributes.find(e.first) != expected_attributes.end())
255+
{
256+
EXPECT_EQ(expected_attributes.find(e.first)->second, nostd::get<std::string>(e.second));
257+
}
258+
}
259+
EXPECT_EQ(received_attributes.size(), expected_attributes.size());
260+
261+
unsetenv("OTEL_RESOURCE_ATTRIBUTES");
262+
}
263+
264+
TEST(ResourceTest, OtelResourceDetectorMalformedEscapeLeftAsIs)
265+
{
266+
std::map<std::string, std::string> expected_attributes = {{"key", "100%"}, {"bad", "50%z"}};
267+
268+
setenv("OTEL_RESOURCE_ATTRIBUTES", "key=100%,bad=50%z", 1);
269+
270+
OTELResourceDetector detector;
271+
auto resource = detector.Detect();
272+
auto received_attributes = resource.GetAttributes();
273+
for (auto &e : received_attributes)
274+
{
275+
EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end());
276+
if (expected_attributes.find(e.first) != expected_attributes.end())
277+
{
278+
EXPECT_EQ(expected_attributes.find(e.first)->second, nostd::get<std::string>(e.second));
279+
}
280+
}
281+
EXPECT_EQ(received_attributes.size(), expected_attributes.size());
282+
283+
unsetenv("OTEL_RESOURCE_ATTRIBUTES");
284+
}
285+
241286
TEST(ResourceTest, OtelResourceDetectorServiceNameOverride)
242287
{
243288
std::map<std::string, std::string> expected_attributes = {{"service.name", "new_name"}};

0 commit comments

Comments
 (0)