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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ Increment the:

* [RESOURCE DETECTOR] Add the host resource detector
[#4413](https://github.com/open-telemetry/opentelemetry-cpp/issues/4413)

* [CONFIGURATION] Support environment variable substitution for attributes
[#4474](https://github.com/open-telemetry/opentelemetry-cpp/pull/4474)

* [RESOURCE DETECTOR] Add the service resource detector and builder
[#4414](https://github.com/open-telemetry/opentelemetry-cpp/issues/4414)
* [CONFIGURATION] deprecate config builder cmake components
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class DocumentNode
virtual std::size_t AsInteger() const = 0;
virtual double AsDouble() const = 0;
virtual std::string AsString() const = 0;
virtual bool IsNull() const = 0;

virtual std::unique_ptr<DocumentNode> GetRequiredChildNode(const std::string &name) const = 0;
virtual std::unique_ptr<DocumentNode> GetChildNode(const std::string &name) const = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class RymlDocumentNode : public DocumentNode
std::size_t AsInteger() const override;
double AsDouble() const override;
std::string AsString() const override;
bool IsNull() const override;

std::unique_ptr<DocumentNode> GetRequiredChildNode(const std::string &name) const override;
std::unique_ptr<DocumentNode> GetChildNode(const std::string &name) const override;
Expand Down
32 changes: 29 additions & 3 deletions sdk/src/configuration/configuration_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2477,10 +2477,13 @@ std::unique_ptr<StringAttributeValueConfiguration>
ConfigurationParser::ParseStringAttributeValueConfiguration(
const std::unique_ptr<DocumentNode> &node) const
{
auto model = std::make_unique<StringAttributeValueConfiguration>();

auto model = std::make_unique<StringAttributeValueConfiguration>();
model->value = node->AsString();

// Empty string (YAML null or unset env var) is treated as null per schema nullBehavior.
if (model->value.empty())
{
return nullptr;
}
return model;
}

Expand Down Expand Up @@ -2613,6 +2616,21 @@ std::unique_ptr<AttributesConfiguration> ConfigurationParser::ParseAttributesCon
std::unique_ptr<AttributeValueConfiguration> value_model;

name = name_child->AsString();

// An empty name is an invalid attribute key per the OTel spec.
if (name.empty())
{
std::string message("Attribute name must not be empty (check for unset env var)");
throw InvalidSchemaException(name_child->Location(), message);
}

// Per schema nullBehavior: skip entries with a null value.
if (value_child->IsNull())
{
OTEL_INTERNAL_LOG_DEBUG("Skipping attribute '" << name << "' with null value");
continue;
}

if (type_child)
{
type = type_child->AsString();
Expand Down Expand Up @@ -2669,6 +2687,14 @@ std::unique_ptr<AttributesConfiguration> ConfigurationParser::ParseAttributesCon
throw InvalidSchemaException(node->Location(), message);
}

// Per schema nullBehavior: skip entries with a null/empty value (e.g. string from unset env
// var).
if (value_model == nullptr)
{
OTEL_INTERNAL_LOG_DEBUG("Skipping attribute '" << name << "' with null/empty value");
continue;
}

std::pair<std::string, std::unique_ptr<AttributeValueConfiguration>> entry(
name, std::move(value_model));
model->kv_map.insert(std::move(entry));
Expand Down
20 changes: 20 additions & 0 deletions sdk/src/configuration/ryml_document_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ bool RymlDocumentNode::AsBoolean() const
}
ryml::csubstr view = node_.val();
std::string value(view.str, view.len);
value = DoSubstitution(value);
return BooleanFromString(value);
}

Expand All @@ -93,6 +94,7 @@ size_t RymlDocumentNode::AsInteger() const
}
ryml::csubstr view = node_.val();
std::string value(view.str, view.len);
value = DoSubstitution(value);
return IntegerFromString(value);
}

Expand All @@ -106,6 +108,7 @@ double RymlDocumentNode::AsDouble() const
}
ryml::csubstr view = node_.val();
std::string value(view.str, view.len);
value = DoSubstitution(value);
return DoubleFromString(value);
}

Expand All @@ -119,9 +122,21 @@ std::string RymlDocumentNode::AsString() const
}
ryml::csubstr view = node_.val();
std::string value(view.str, view.len);
value = DoSubstitution(value);
return value;
}

bool RymlDocumentNode::IsNull() const
{
OTEL_INTERNAL_LOG_DEBUG("RymlDocumentNode::IsNull()");

if (!node_.is_val() && !node_.is_keyval())
{
return false;
}
return node_.val_is_null();
}

ryml::ConstNodeRef RymlDocumentNode::GetRequiredRymlChildNode(const std::string &name) const
{
if (!node_.is_map())
Expand Down Expand Up @@ -383,6 +398,11 @@ std::string RymlDocumentNode::GetString(const std::string &name,

value = DoSubstitution(value);

if (value.empty())
{
return default_value;
}

return value;
}

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 @@ -2576,7 +2576,12 @@ void SdkBuilder::SetResource(
{
for (const auto &kv : opt_model->attributes->kv_map)
{
SetResourceAttribute(sdk_attributes, kv.first, kv.second.get());
// kv_map values may be nullptr for programmatically-set null entries.
// The default behavior for resource.attributes is to ignore null values.
if (kv.second != nullptr)
{
SetResourceAttribute(sdk_attributes, kv.first, kv.second.get());
}
}
}

Expand Down
95 changes: 90 additions & 5 deletions sdk/test/configuration/sdk_builder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1260,18 +1260,24 @@ class TestProcessResourceDetectorBuilder : public config_sdk::ProcessResourceDet
class TestExtensionResourceDetectorBuilder : public config_sdk::ExtensionResourceDetectorBuilder
{
public:
explicit TestExtensionResourceDetectorBuilder(
opentelemetry::sdk::resource::ResourceAttributes attributes)
: attributes_(std::move(attributes))
{}

std::unique_ptr<opentelemetry::sdk::resource::ResourceDetector> Build(
const config_sdk::ExtensionResourceDetectorConfiguration *model) const override
{
called = true;
name = model->name;
return std::make_unique<TestResourceDetector>(
opentelemetry::sdk::resource::ResourceAttributes{{"custom.key", "custom-value"}},
std::string{});
return std::make_unique<TestResourceDetector>(attributes_, std::string{});
}

mutable bool called{false};
mutable std::string name;

private:
opentelemetry::sdk::resource::ResourceAttributes attributes_;
};

std::string GetStringAttribute(const opentelemetry::sdk::resource::Resource &resource,
Expand Down Expand Up @@ -1342,8 +1348,9 @@ TEST(SdkBuilder, SetResourceDetectorDispatch)

TEST(SdkBuilder, SetResourceExtensionDetector)
{
auto registry = RegistryFactory::Create();
auto extension_builder = std::make_unique<TestExtensionResourceDetectorBuilder>();
auto registry = RegistryFactory::Create();
auto extension_builder = std::make_unique<TestExtensionResourceDetectorBuilder>(
opentelemetry::sdk::resource::ResourceAttributes{{"custom.key", "custom-value"}});
auto *extension_builder_p = extension_builder.get();
registry->SetExtensionResourceDetectorBuilder("my_custom_detector", std::move(extension_builder));
SdkBuilder builder(std::move(registry));
Expand Down Expand Up @@ -1472,3 +1479,81 @@ TEST(SdkBuilder, SetResourceFilterAppliesToDetectedAttributesOnly)
EXPECT_EQ(GetStringAttribute(resource, "process.pid"), "from-list");
EXPECT_EQ(GetStringAttribute(resource, "process.command_args"), "from-attributes");
}

TEST(SdkBuilder, SetResourceNullAttributeValueSkipped)
{
auto registry = RegistryFactory::Create();
SdkBuilder builder(std::move(registry));

auto typed_value = std::make_unique<config_sdk::StringAttributeValueConfiguration>();
typed_value->value = "kept";

auto model = std::make_unique<config_sdk::ResourceConfiguration>();
model->attributes = std::make_unique<config_sdk::AttributesConfiguration>();
model->attributes->kv_map.emplace("present", std::move(typed_value));
model->attributes->kv_map.emplace("null-entry", nullptr); // should be skipped

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

EXPECT_EQ(GetStringAttribute(resource, "present"), "kept");
const auto &attrs = resource.GetAttributes();
EXPECT_EQ(attrs.find("null-entry"), attrs.end());
}

TEST(SdkBuilder, SetResourceAttributesNullValueSkipped)
{
// The default behavior for the resource.attributes field must skip null values.
// This allows optional env values to be set (e.g. OTEL_SERVICE_NAME).
// If the env var is not set then the attribute should be ignored.

// Example config created programmatically below
//
// resource:
// attributes:
// -name: service.name
// value: ${OTEL_SERVICE_NAME} # env not set
// -name: service.namespace
// value: ${MY_SERVICE_NAMESPACE} # env not set
// -name: service.version
// value: version-from-attributes
// attributes_list: "service.name=name-from-list"
// detection:
// detectors:
// - test_service_detector # detects service.{name, version, namespace}

auto registry = RegistryFactory::Create();
auto ext_detector = std::make_unique<TestExtensionResourceDetectorBuilder>(
opentelemetry::sdk::resource::ResourceAttributes{
{"service.name", "name-from-detector"},
{"service.namespace", "namespace-from-detector"},
{"service.version", "version-from-detector"}});
registry->SetExtensionResourceDetectorBuilder("test_service_detector", std::move(ext_detector));
SdkBuilder builder(std::move(registry));

auto model = std::make_unique<config_sdk::ResourceConfiguration>();
auto detector_config = std::make_unique<config_sdk::ExtensionResourceDetectorConfiguration>();
detector_config->name = "test_service_detector";
model->detection = std::make_unique<config_sdk::ResourceDetectionConfiguration>();
model->detection->detectors.push_back(std::move(detector_config));
model->attributes_list = "service.name=name-from-list,service.version=version-from-list";

// Simulate null values from unset env variables.
model->attributes = std::make_unique<config_sdk::AttributesConfiguration>();
model->attributes->kv_map.emplace("service.name", nullptr); // simulate unset OTEL_SERVICE_NAME
model->attributes->kv_map.emplace("service.namespace",
nullptr); // simulate unset MY_SERVICE_NAMESPACE
auto typed_value = std::make_unique<config_sdk::StringAttributeValueConfiguration>();
typed_value->value = "version-from-attributes";
model->attributes->kv_map.emplace("service.version", std::move(typed_value));

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

// attributes_list wins over detection for service.name; nullptr in attributes doesn't override.
EXPECT_EQ(GetStringAttribute(resource, "service.name"), "name-from-list");
// detected value survives for service.namespace (no attributes_list entry, nullptr skipped).
EXPECT_EQ(GetStringAttribute(resource, "service.namespace"), "namespace-from-detector");
// attribute value for service.version overrides both list and detection.
EXPECT_EQ(GetStringAttribute(resource, "service.version"), "version-from-attributes");
}
Loading
Loading