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
1 change: 1 addition & 0 deletions sdk/src/configuration/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ cc_library(
"//sdk/src/common:wildcard_match",
"//sdk/src/logs",
"//sdk/src/metrics",
"//sdk/src/resource",
"//sdk/src/trace",
],
)
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/configuration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ target_include_directories(
target_link_libraries(
opentelemetry_configuration_core
PUBLIC opentelemetry_api opentelemetry_common opentelemetry_trace
opentelemetry_metrics opentelemetry_logs
opentelemetry_metrics opentelemetry_logs opentelemetry_resources
PRIVATE)

# Trace SDK component builders
Expand Down
7 changes: 6 additions & 1 deletion sdk/src/configuration/sdk_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@
#include "opentelemetry/version.h"
#include "src/common/wildcard_match.h"

#include "src/resource/detail/percent_decode.h"

#ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
# include "opentelemetry/sdk/metrics/exemplar/filter_type.h"
#endif
Expand Down Expand Up @@ -2552,7 +2554,10 @@ void SdkBuilder::SetResource(
{
if (attribute_valid)
Comment thread
WyshalZyren marked this conversation as resolved.
Resolved
{
opentelemetry::common::AttributeValue wrapped_attribute_value(attribute_value);
std::string decoded_value = opentelemetry::sdk::resource::detail::PercentDecode(
Comment thread
WyshalZyren marked this conversation as resolved.
Resolved
std::string{attribute_value.data(), attribute_value.size()});

opentelemetry::common::AttributeValue wrapped_attribute_value(decoded_value);
list_attributes.SetAttribute(attribute_key, wrapped_attribute_value);
}
else
Comment thread
WyshalZyren marked this conversation as resolved.
Resolved
Expand Down
1 change: 1 addition & 0 deletions sdk/src/resource/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package(default_visibility = ["//visibility:public"])
cc_library(
name = "resource",
srcs = glob(["**/*.cc"]),
hdrs = glob(["**/*.h"]),
include_prefix = "src/resource",
deps = [
"//api",
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/resource/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

add_library(opentelemetry_resources resource.cc resource_detector.cc)
add_library(opentelemetry_resources resource.cc resource_detector.cc
detail/percent_decode.cc)

set_target_properties(opentelemetry_resources PROPERTIES EXPORT_NAME resources)
set_target_version(opentelemetry_resources)
Expand Down
58 changes: 58 additions & 0 deletions sdk/src/resource/detail/percent_decode.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include "percent_decode.h"

#include <cctype>
#include <cstddef>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace resource
{
namespace detail
{
namespace
{

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

} // namespace

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 detail
} // namespace resource
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
25 changes: 25 additions & 0 deletions sdk/src/resource/detail/percent_decode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <string>

#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace resource
{
namespace detail
{

// Decodes valid percent-encoded sequences in value.
// Invalid or incomplete percent-encoded sequences are left unchanged.
std::string PercentDecode(const std::string &value);
Comment thread
WyshalZyren marked this conversation as resolved.
Resolved

} // namespace detail
} // namespace resource
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
52 changes: 11 additions & 41 deletions sdk/src/resource/resource_detector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/sdk/resource/resource_detector.h"
#include "opentelemetry/nostd/variant.h"

#include "opentelemetry/sdk/common/env_variables.h"
#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/semconv/service_attributes.h"
#include "opentelemetry/version.h"

#include <cctype>
#include <cstddef>
#include "src/resource/detail/percent_decode.h"

#include "opentelemetry/nostd/variant.h"

#include <stddef.h>
#include <sstream>
#include <string>

Expand All @@ -22,43 +25,6 @@ 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 All @@ -71,6 +37,7 @@ Resource OTELResourceDetector::Detect() noexcept

bool attributes_exists = opentelemetry::sdk::common::GetStringEnvironmentVariable(
kOtelResourceAttributes, attributes_str);

bool service_name_exists =
opentelemetry::sdk::common::GetStringEnvironmentVariable(kOtelServiceName, service_name);

Expand All @@ -85,14 +52,17 @@ Resource OTELResourceDetector::Detect() noexcept
{
std::istringstream iss(attributes_str);
std::string token;

while (std::getline(iss, token, ','))
{
size_t pos = token.find('=');

if (pos != std::string::npos)
{
std::string key = token.substr(0, pos);
std::string value = token.substr(pos + 1);
attributes[key] = PercentDecode(value);

attributes[key] = detail::PercentDecode(value);
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions sdk/test/configuration/sdk_builder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,22 @@ TEST(SdkBuilder, SetResourceWithoutModel)
EXPECT_EQ(GetStringAttribute(resource, "telemetry.sdk.language"), "cpp");
}

TEST(SdkBuilder, SetResourceAttributesListPercentDecodesValues)
{
SdkBuilder builder(RegistryFactory::Create());

auto model = std::make_unique<config_sdk::ResourceConfiguration>();
model->attributes_list = "key1=hello%20world,key2=a%2Cb,key3=100%25,bad=50%z";

auto resource = opentelemetry::sdk::resource::Resource::GetEmpty();
builder.SetResource(resource, model);

EXPECT_EQ(GetStringAttribute(resource, "key1"), "hello world");
EXPECT_EQ(GetStringAttribute(resource, "key2"), "a,b");
EXPECT_EQ(GetStringAttribute(resource, "key3"), "100%");
EXPECT_EQ(GetStringAttribute(resource, "bad"), "50%z");
}

TEST(SdkBuilder, SetResourceDetectorDispatch)
{
auto registry = RegistryFactory::Create();
Expand Down
17 changes: 17 additions & 0 deletions sdk/test/resource/resource_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "opentelemetry/semconv/service_attributes.h"
#include "opentelemetry/semconv/telemetry_attributes.h"

#include "src/resource/detail/percent_decode.h"

#if defined(_MSC_VER)
# include "opentelemetry/sdk/common/env_variables.h"
using opentelemetry::sdk::common::setenv;
Expand Down Expand Up @@ -238,6 +240,21 @@ TEST(ResourceTest, OtelResourceDetector)
unsetenv("OTEL_RESOURCE_ATTRIBUTES");
}

TEST(ResourceTest, PercentDecode)
{
using opentelemetry::sdk::resource::detail::PercentDecode;

EXPECT_EQ(PercentDecode("hello%20world"), "hello world");
EXPECT_EQ(PercentDecode("a%2Cb"), "a,b");
EXPECT_EQ(PercentDecode("100%25"), "100%");
EXPECT_EQ(PercentDecode("%41%42%43"), "ABC");

EXPECT_EQ(PercentDecode("100%"), "100%");
EXPECT_EQ(PercentDecode("50%z"), "50%z");
EXPECT_EQ(PercentDecode("%GG"), "%GG");
EXPECT_EQ(PercentDecode("plain"), "plain");
}

TEST(ResourceTest, OtelResourceDetectorPercentDecodesValues)
{
std::map<std::string, std::string> expected_attributes = {
Expand Down
Loading