Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ Increment the:

## [Unreleased]

* [SDK] `OTELResourceDetector` now percent-decodes values parsed from the
`OTEL_RESOURCE_ATTRIBUTES` environment variable, per the W3C Baggage value
grammar the resource spec defers to. A malformed escape sequence is left
in the value as-is rather than dropping the attribute.
[#1536](https://github.com/open-telemetry/opentelemetry-cpp/issues/1536)

* [BUG] Report one outcome per request when a curl session is cancelled after
the response arrives
([#4363](https://github.com/open-telemetry/opentelemetry-cpp/pull/4363))

* [CODE HEALTH] Enable clang-tidy `modernize-deprecated-headers` and replace
deprecated C headers (`stdint.h`, `stddef.h`, `stdlib.h`, `string.h`,
`stdio.h`, `ctype.h`, `limits.h`, `assert.h`) with their C++ equivalents
Expand Down
40 changes: 39 additions & 1 deletion sdk/src/resource/resource_detector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "opentelemetry/semconv/service_attributes.h"
#include "opentelemetry/version.h"

#include <cctype>
#include <cstddef>
#include <sstream>
#include <string>
Expand All @@ -21,6 +22,43 @@ namespace resource
constexpr const char *kOtelResourceAttributes = "OTEL_RESOURCE_ATTRIBUTES";
constexpr const char *kOtelServiceName = "OTEL_SERVICE_NAME";

namespace
{
unsigned char HexDigitValue(unsigned char c) noexcept
{
return static_cast<unsigned char>(std::isdigit(c) ? c - '0' : std::toupper(c) - 'A' + 10);
}

// Percent-decodes a value from OTEL_RESOURCE_ATTRIBUTES, per the W3C Baggage value grammar
// the resource spec defers to
// (https://github.com/open-telemetry/opentelemetry-specification/pull/2670). A malformed escape
// sequence is left in the output as-is rather than dropping the attribute, since this parser has
// never validated attribute syntax.
std::string PercentDecode(const std::string &value)
{
std::string decoded;
decoded.reserve(value.size());
for (std::size_t i = 0; i < value.size(); ++i)
{
if (value[i] == '%' && i + 2 < value.size())
{
unsigned char high = static_cast<unsigned char>(value[i + 1]);
unsigned char low = static_cast<unsigned char>(value[i + 2]);
if (std::isxdigit(high) && std::isxdigit(low))
{
unsigned char unescaped_value =
static_cast<unsigned char>((HexDigitValue(high) << 4) | HexDigitValue(low));
decoded.push_back(static_cast<char>(unescaped_value));
i += 2;
continue;
}
}
decoded.push_back(value[i]);
}
return decoded;
}
} // namespace

Resource ResourceDetector::Create(const ResourceAttributes &attributes,
const std::string &schema_url)
{
Expand Down Expand Up @@ -54,7 +92,7 @@ Resource OTELResourceDetector::Detect() noexcept
{
std::string key = token.substr(0, pos);
std::string value = token.substr(pos + 1);
attributes[key] = value;
attributes[key] = PercentDecode(value);
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions sdk/test/resource/resource_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,51 @@ TEST(ResourceTest, OtelResourceDetector)
unsetenv("OTEL_RESOURCE_ATTRIBUTES");
}

TEST(ResourceTest, OtelResourceDetectorPercentDecodesValues)
{
std::map<std::string, std::string> expected_attributes = {
{"key1", "hello world"}, {"key2", "a,b"}, {"key3", "100%"}};

setenv("OTEL_RESOURCE_ATTRIBUTES", "key1=hello%20world,key2=a%2Cb,key3=100%25", 1);

OTELResourceDetector detector;
auto resource = detector.Detect();
auto received_attributes = resource.GetAttributes();
for (auto &e : received_attributes)
{
EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end());
if (expected_attributes.find(e.first) != expected_attributes.end())
{
EXPECT_EQ(expected_attributes.find(e.first)->second, nostd::get<std::string>(e.second));
}
}
EXPECT_EQ(received_attributes.size(), expected_attributes.size());

unsetenv("OTEL_RESOURCE_ATTRIBUTES");
}

TEST(ResourceTest, OtelResourceDetectorMalformedEscapeLeftAsIs)
{
std::map<std::string, std::string> expected_attributes = {{"key", "100%"}, {"bad", "50%z"}};

setenv("OTEL_RESOURCE_ATTRIBUTES", "key=100%,bad=50%z", 1);

OTELResourceDetector detector;
auto resource = detector.Detect();
auto received_attributes = resource.GetAttributes();
for (auto &e : received_attributes)
{
EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end());
if (expected_attributes.find(e.first) != expected_attributes.end())
{
EXPECT_EQ(expected_attributes.find(e.first)->second, nostd::get<std::string>(e.second));
}
}
EXPECT_EQ(received_attributes.size(), expected_attributes.size());

unsetenv("OTEL_RESOURCE_ATTRIBUTES");
}

TEST(ResourceTest, OtelResourceDetectorServiceNameOverride)
{
std::map<std::string, std::string> expected_attributes = {{"service.name", "new_name"}};
Expand Down
Loading