From 19bec569e5192163acc1a225a283be97b40f254f Mon Sep 17 00:00:00 2001 From: Douglas Barker Date: Sun, 23 Aug 2026 18:07:00 -0400 Subject: [PATCH 1/4] Support setting values from env vars --- .../sdk/configuration/document_node.h | 1 + .../sdk/configuration/ryml_document_node.h | 1 + sdk/src/configuration/configuration_parser.cc | 32 +- sdk/src/configuration/ryml_document_node.cc | 20 + sdk/src/configuration/sdk_builder.cc | 7 +- sdk/test/configuration/sdk_builder_test.cc | 92 +++- sdk/test/configuration/yaml_resource_test.cc | 405 ++++++++++++++++++ sdk/test/configuration/yaml_test.cc | 64 +++ 8 files changed, 613 insertions(+), 9 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/configuration/document_node.h b/sdk/include/opentelemetry/sdk/configuration/document_node.h index 2dc715eec9..281515574b 100644 --- a/sdk/include/opentelemetry/sdk/configuration/document_node.h +++ b/sdk/include/opentelemetry/sdk/configuration/document_node.h @@ -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 GetRequiredChildNode(const std::string &name) const = 0; virtual std::unique_ptr GetChildNode(const std::string &name) const = 0; diff --git a/sdk/include/opentelemetry/sdk/configuration/ryml_document_node.h b/sdk/include/opentelemetry/sdk/configuration/ryml_document_node.h index f22c75f796..37248df4cf 100644 --- a/sdk/include/opentelemetry/sdk/configuration/ryml_document_node.h +++ b/sdk/include/opentelemetry/sdk/configuration/ryml_document_node.h @@ -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 GetRequiredChildNode(const std::string &name) const override; std::unique_ptr GetChildNode(const std::string &name) const override; diff --git a/sdk/src/configuration/configuration_parser.cc b/sdk/src/configuration/configuration_parser.cc index 58f7fed866..3d2dab661c 100644 --- a/sdk/src/configuration/configuration_parser.cc +++ b/sdk/src/configuration/configuration_parser.cc @@ -2477,10 +2477,13 @@ std::unique_ptr ConfigurationParser::ParseStringAttributeValueConfiguration( const std::unique_ptr &node) const { - auto model = std::make_unique(); - + auto model = std::make_unique(); 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; } @@ -2613,6 +2616,21 @@ std::unique_ptr ConfigurationParser::ParseAttributesCon std::unique_ptr 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(); @@ -2669,6 +2687,14 @@ std::unique_ptr 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> entry( name, std::move(value_model)); model->kv_map.insert(std::move(entry)); diff --git a/sdk/src/configuration/ryml_document_node.cc b/sdk/src/configuration/ryml_document_node.cc index b24a312d8b..65d5ec43c9 100644 --- a/sdk/src/configuration/ryml_document_node.cc +++ b/sdk/src/configuration/ryml_document_node.cc @@ -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); } @@ -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); } @@ -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); } @@ -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()) @@ -383,6 +398,11 @@ std::string RymlDocumentNode::GetString(const std::string &name, value = DoSubstitution(value); + if (value.empty()) + { + return default_value; + } + return value; } diff --git a/sdk/src/configuration/sdk_builder.cc b/sdk/src/configuration/sdk_builder.cc index fe91e685f7..39d959a23a 100644 --- a/sdk/src/configuration/sdk_builder.cc +++ b/sdk/src/configuration/sdk_builder.cc @@ -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()); + } } } diff --git a/sdk/test/configuration/sdk_builder_test.cc b/sdk/test/configuration/sdk_builder_test.cc index c4e4ca86d5..824b5d7df7 100644 --- a/sdk/test/configuration/sdk_builder_test.cc +++ b/sdk/test/configuration/sdk_builder_test.cc @@ -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 Build( const config_sdk::ExtensionResourceDetectorConfiguration *model) const override { called = true; name = model->name; - return std::make_unique( - opentelemetry::sdk::resource::ResourceAttributes{{"custom.key", "custom-value"}}, - std::string{}); + return std::make_unique(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, @@ -1342,8 +1348,9 @@ TEST(SdkBuilder, SetResourceDetectorDispatch) TEST(SdkBuilder, SetResourceExtensionDetector) { - auto registry = RegistryFactory::Create(); - auto extension_builder = std::make_unique(); + auto registry = RegistryFactory::Create(); + auto extension_builder = std::make_unique( + 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)); @@ -1472,3 +1479,78 @@ 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(); + typed_value->value = "kept"; + + auto model = std::make_unique(); + model->attributes = std::make_unique(); + 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: + // service.name: ${OTEL_SERVICE_NAME} # env not set + // service.namespace: ${MY_SERVICE_NAMESPACE} # env not set + // service.version: 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( + 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(); + auto detector_config = std::make_unique(); + detector_config->name = "test_service_detector"; + model->detection = std::make_unique(); + 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(); + 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(); + 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"); +} diff --git a/sdk/test/configuration/yaml_resource_test.cc b/sdk/test/configuration/yaml_resource_test.cc index fed0fc7a6f..2826cf6948 100644 --- a/sdk/test/configuration/yaml_resource_test.cc +++ b/sdk/test/configuration/yaml_resource_test.cc @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -11,17 +12,32 @@ #include "opentelemetry/sdk/configuration/attribute_value_configuration.h" #include "opentelemetry/sdk/configuration/attributes_configuration.h" +#include "opentelemetry/sdk/configuration/boolean_array_attribute_value_configuration.h" +#include "opentelemetry/sdk/configuration/boolean_attribute_value_configuration.h" #include "opentelemetry/sdk/configuration/configuration.h" +#include "opentelemetry/sdk/configuration/double_array_attribute_value_configuration.h" +#include "opentelemetry/sdk/configuration/double_attribute_value_configuration.h" #include "opentelemetry/sdk/configuration/extension_resource_detector_configuration.h" #include "opentelemetry/sdk/configuration/include_exclude_configuration.h" +#include "opentelemetry/sdk/configuration/integer_array_attribute_value_configuration.h" +#include "opentelemetry/sdk/configuration/integer_attribute_value_configuration.h" #include "opentelemetry/sdk/configuration/resource_configuration.h" #include "opentelemetry/sdk/configuration/resource_detection_configuration.h" #include "opentelemetry/sdk/configuration/resource_detector_configuration.h" #include "opentelemetry/sdk/configuration/resource_detector_configuration_visitor.h" +#include "opentelemetry/sdk/configuration/string_array_attribute_value_configuration.h" #include "opentelemetry/sdk/configuration/string_array_configuration.h" #include "opentelemetry/sdk/configuration/string_attribute_value_configuration.h" #include "opentelemetry/sdk/configuration/yaml_configuration_parser.h" +#if defined(_MSC_VER) +# include "opentelemetry/sdk/common/env_variables.h" +using opentelemetry::sdk::common::setenv; +using opentelemetry::sdk::common::unsetenv; +#endif + +namespace config_sdk = opentelemetry::sdk::configuration; + static std::unique_ptr DoParse( const std::string &yaml) { @@ -191,6 +207,40 @@ file_format: "1.0-resource" ASSERT_EQ(config->resource->attributes_list, "foo=1234,bar=5678"); } +TEST(YamlResource, attributes_list_substitution) +{ + setenv("TEST_ENV_NAME", "foo=1234,bar=5678", 1); + + std::string yaml = R"( +file_format: "1.0-resource" +resource: + attributes_list: ${TEST_ENV_NAME} +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_NE(config->resource, nullptr); + ASSERT_EQ(config->resource->attributes_list, "foo=1234,bar=5678"); + + unsetenv("TEST_ENV_NAME"); +} + +TEST(YamlResource, attributes_list_substitution_fallback) +{ + unsetenv("TEST_ENV_NAME"); + + std::string yaml = R"( +file_format: "1.0-resource" +resource: + attributes_list: ${TEST_ENV_NAME:-service.name=fallback} +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_NE(config->resource, nullptr); + ASSERT_EQ(config->resource->attributes_list, "service.name=fallback"); +} + TEST(YamlResource, both_0_10) { // This is the old 0.10 format, must fail @@ -448,3 +498,358 @@ file_format: "1.0-resource" ASSERT_EQ(config->resource->detection->attributes->included->string_array[0], "container.*"); ASSERT_EQ(config->resource->detection->detectors.size(), 2); } + +TEST(YamlResource, string_attribute_substitution) +{ + setenv("TEST_ENV_NAME", "my-service", 1); + + std::string yaml = R"( +file_format: "1.0-resource" +resource: + attributes: + - name: service.name + value: ${TEST_ENV_NAME} +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_NE(config->resource, nullptr); + ASSERT_NE(config->resource->attributes, nullptr); + auto &kv = config->resource->attributes->kv_map; + ASSERT_EQ(kv.count("service.name"), 1u); + auto *val = + static_cast(kv.at("service.name").get()); + ASSERT_EQ(val->value, "my-service"); + + unsetenv("TEST_ENV_NAME"); +} + +TEST(YamlResource, string_attribute_substitution_fallback_empty) +{ + unsetenv("TEST_ENV_NAME"); + + std::string yaml = R"( +file_format: "1.0-resource" +resource: + attributes: + - name: service.name + value: ${TEST_ENV_NAME:-fallback-name} +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_NE(config->resource, nullptr); + ASSERT_NE(config->resource->attributes, nullptr); + auto &kv = config->resource->attributes->kv_map; + ASSERT_EQ(kv.count("service.name"), 1u); + auto *val = + static_cast(kv.at("service.name").get()); + ASSERT_EQ(val->value, "fallback-name"); +} + +TEST(YamlResource, boolean_attribute_substitution) +{ + setenv("TEST_ENV_NAME", "true", 1); + + std::string yaml = R"( +file_format: "1.0-resource" +resource: + attributes: + - name: flag + value: ${TEST_ENV_NAME} + type: bool +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_NE(config->resource, nullptr); + ASSERT_NE(config->resource->attributes, nullptr); + auto &kv = config->resource->attributes->kv_map; + ASSERT_EQ(kv.count("flag"), 1u); + auto *val = static_cast(kv.at("flag").get()); + ASSERT_EQ(val->value, true); + + unsetenv("TEST_ENV_NAME"); +} + +TEST(YamlResource, boolean_attribute_substitution_fallback) +{ + unsetenv("TEST_ENV_NAME"); + + std::string yaml = R"( +file_format: "1.0-resource" +resource: + attributes: + - name: flag + value: ${TEST_ENV_NAME:-false} + type: bool +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_NE(config->resource, nullptr); + ASSERT_NE(config->resource->attributes, nullptr); + auto &kv = config->resource->attributes->kv_map; + ASSERT_EQ(kv.count("flag"), 1u); + auto *val = static_cast(kv.at("flag").get()); + ASSERT_EQ(val->value, false); +} + +TEST(YamlResource, integer_attribute_substitution) +{ + setenv("TEST_ENV_NAME", "42", 1); + + std::string yaml = R"( +file_format: "1.0-resource" +resource: + attributes: + - name: count + value: ${TEST_ENV_NAME} + type: int +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_NE(config->resource, nullptr); + ASSERT_NE(config->resource->attributes, nullptr); + auto &kv = config->resource->attributes->kv_map; + ASSERT_EQ(kv.count("count"), 1u); + auto *val = static_cast(kv.at("count").get()); + ASSERT_EQ(val->value, 42); + + unsetenv("TEST_ENV_NAME"); +} + +TEST(YamlResource, integer_attribute_substitution_fallback) +{ + unsetenv("TEST_ENV_NAME"); + + std::string yaml = R"( +file_format: "1.0-resource" +resource: + attributes: + - name: count + value: ${TEST_ENV_NAME:-99} + type: int +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_NE(config->resource, nullptr); + ASSERT_NE(config->resource->attributes, nullptr); + auto &kv = config->resource->attributes->kv_map; + ASSERT_EQ(kv.count("count"), 1u); + auto *val = static_cast(kv.at("count").get()); + ASSERT_EQ(val->value, 99); +} + +TEST(YamlResource, double_attribute_substitution) +{ + setenv("TEST_ENV_NAME", "3.14", 1); + + std::string yaml = R"( +file_format: "1.0-resource" +resource: + attributes: + - name: ratio + value: ${TEST_ENV_NAME} + type: double +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_NE(config->resource, nullptr); + ASSERT_NE(config->resource->attributes, nullptr); + auto &kv = config->resource->attributes->kv_map; + ASSERT_EQ(kv.count("ratio"), 1u); + auto *val = static_cast(kv.at("ratio").get()); + ASSERT_DOUBLE_EQ(val->value, 3.14); + + unsetenv("TEST_ENV_NAME"); +} + +TEST(YamlResource, double_attribute_substitution_fallback) +{ + unsetenv("TEST_ENV_NAME"); + + std::string yaml = R"( +file_format: "1.0-resource" +resource: + attributes: + - name: ratio + value: ${TEST_ENV_NAME:-2.71} + type: double +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_NE(config->resource, nullptr); + ASSERT_NE(config->resource->attributes, nullptr); + auto &kv = config->resource->attributes->kv_map; + ASSERT_EQ(kv.count("ratio"), 1u); + auto *val = static_cast(kv.at("ratio").get()); + ASSERT_DOUBLE_EQ(val->value, 2.71); +} + +TEST(YamlResource, string_array_attribute_substitution) +{ + setenv("TEST_ENV_NAME", "hello", 1); + + std::string yaml = R"( +file_format: "1.0-resource" +resource: + attributes: + - name: tags + value: + - ${TEST_ENV_NAME} + - world + type: string_array +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_NE(config->resource, nullptr); + ASSERT_NE(config->resource->attributes, nullptr); + auto &kv = config->resource->attributes->kv_map; + ASSERT_EQ(kv.count("tags"), 1u); + auto *val = + static_cast(kv.at("tags").get()); + ASSERT_EQ(val->value.size(), 2u); + ASSERT_EQ(val->value[0], "hello"); + ASSERT_EQ(val->value[1], "world"); + + unsetenv("TEST_ENV_NAME"); +} + +TEST(YamlResource, integer_array_attribute_substitution) +{ + setenv("TEST_ENV_NAME", "7", 1); + + std::string yaml = R"( +file_format: "1.0-resource" +resource: + attributes: + - name: ids + value: + - ${TEST_ENV_NAME} + - 8 + type: int_array +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_NE(config->resource, nullptr); + ASSERT_NE(config->resource->attributes, nullptr); + auto &kv = config->resource->attributes->kv_map; + ASSERT_EQ(kv.count("ids"), 1u); + auto *val = + static_cast(kv.at("ids").get()); + ASSERT_EQ(val->value.size(), 2u); + ASSERT_EQ(val->value[0], 7u); + ASSERT_EQ(val->value[1], 8u); + + unsetenv("TEST_ENV_NAME"); +} + +TEST(YamlResource, double_array_attribute_substitution) +{ + setenv("TEST_ENV_NAME", "1.5", 1); + + std::string yaml = R"( +file_format: "1.0-resource" +resource: + attributes: + - name: scores + value: + - ${TEST_ENV_NAME} + - 2.5 + type: double_array +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_NE(config->resource, nullptr); + ASSERT_NE(config->resource->attributes, nullptr); + auto &kv = config->resource->attributes->kv_map; + ASSERT_EQ(kv.count("scores"), 1u); + auto *val = + static_cast(kv.at("scores").get()); + ASSERT_EQ(val->value.size(), 2u); + ASSERT_DOUBLE_EQ(val->value[0], 1.5); + ASSERT_DOUBLE_EQ(val->value[1], 2.5); + + unsetenv("TEST_ENV_NAME"); +} + +TEST(YamlResource, boolean_array_attribute_substitution) +{ + setenv("TEST_ENV_NAME", "true", 1); + + std::string yaml = R"( +file_format: "1.0-resource" +resource: + attributes: + - name: flags + value: + - ${TEST_ENV_NAME} + - false + type: bool_array +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_NE(config->resource, nullptr); + ASSERT_NE(config->resource->attributes, nullptr); + auto &kv = config->resource->attributes->kv_map; + ASSERT_EQ(kv.count("flags"), 1u); + auto *val = + static_cast(kv.at("flags").get()); + ASSERT_EQ(val->value.size(), 2u); + ASSERT_EQ(val->value[0], true); + ASSERT_EQ(val->value[1], false); + + unsetenv("TEST_ENV_NAME"); +} + +TEST(YamlResource, null_attribute_value_skipped) +{ + // YAML null value (value:) must be skipped per schema nullBehavior. + std::string yaml = R"( +file_format: "1.0-resource" +resource: + attributes: + - name: service.name + value: + - name: host.name + value: "my-host" +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_NE(config->resource, nullptr); + ASSERT_NE(config->resource->attributes, nullptr); + const auto &kv = config->resource->attributes->kv_map; + // null value entry must be absent + ASSERT_EQ(kv.count("service.name"), 0u); + // non-null entry must be present + ASSERT_EQ(kv.count("host.name"), 1u); +} + +TEST(YamlResource, empty_name_attribute_parse_error) +{ + // An empty attribute name (e.g. from an unset env var) is an invalid config. + unsetenv("TEST_ENV_NAME"); + + std::string yaml = R"( +file_format: "1.0-resource" +resource: + attributes: + - name: ${TEST_ENV_NAME} + value: "some-value" + - name: host.name + value: "my-host" +)"; + + auto config = DoParse(yaml); + ASSERT_EQ(config, nullptr); +} diff --git a/sdk/test/configuration/yaml_test.cc b/sdk/test/configuration/yaml_test.cc index 8f95478429..db0f2d2ae9 100644 --- a/sdk/test/configuration/yaml_test.cc +++ b/sdk/test/configuration/yaml_test.cc @@ -8,6 +8,7 @@ #include "opentelemetry/sdk/configuration/attribute_limits_configuration.h" #include "opentelemetry/sdk/configuration/configuration.h" +#include "opentelemetry/sdk/configuration/severity_number.h" #include "opentelemetry/sdk/configuration/trace_id_ratio_based_sampler_configuration.h" #include "opentelemetry/sdk/configuration/tracer_provider_configuration.h" #include "opentelemetry/sdk/configuration/yaml_configuration_parser.h" @@ -757,3 +758,66 @@ file_format: "1.0" auto config = DoParse(yaml); ASSERT_EQ(config, nullptr); } + +// --- empty env var with :- fallback tests --- + +TEST(Yaml, empty_string_substitution_with_fallback) +{ + setenv("ENV_NAME", "", 1); + + std::string yaml = R"( +file_format: ${ENV_NAME:-1.0} +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_EQ(config->file_format, "1.0"); +} + +TEST(Yaml, empty_boolean_substitution_with_fallback) +{ + setenv("ENV_NAME", "", 1); + + std::string yaml = R"( +file_format: "1.0" +disabled: ${ENV_NAME:-true} +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_EQ(config->disabled, true); +} + +// --- GetString default-on-empty tests --- + +TEST(Yaml, optional_string_unset_env_var_uses_default) +{ + // An unset env var in an optional string field must produce the declared default, + // not an empty string that fails downstream parsing. + unsetenv("ENV_NAME"); + + std::string yaml = R"( +file_format: "1.0" +log_level: ${ENV_NAME} +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + // default log_level is "info" -> SeverityNumber::info + ASSERT_EQ(config->log_level, opentelemetry::sdk::configuration::SeverityNumber::info); +} + +TEST(Yaml, optional_string_set_env_var_uses_value) +{ + setenv("ENV_NAME", "debug", 1); + + std::string yaml = R"( +file_format: "1.0" +log_level: ${ENV_NAME} +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_EQ(config->log_level, opentelemetry::sdk::configuration::SeverityNumber::debug); +} + From f6347b2301ab529b5fafa3aa0fa82d36909234c9 Mon Sep 17 00:00:00 2001 From: Douglas Barker Date: Sun, 23 Aug 2026 18:14:31 -0400 Subject: [PATCH 2/4] fix iwyu warning --- sdk/test/configuration/yaml_resource_test.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/test/configuration/yaml_resource_test.cc b/sdk/test/configuration/yaml_resource_test.cc index 2826cf6948..95b2723333 100644 --- a/sdk/test/configuration/yaml_resource_test.cc +++ b/sdk/test/configuration/yaml_resource_test.cc @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 #include -#include #include #include #include From e2862f53369e0e0ce69e460e9f69255a0a0fa074 Mon Sep 17 00:00:00 2001 From: Douglas Barker Date: Sun, 23 Aug 2026 18:30:22 -0400 Subject: [PATCH 3/4] cleanup and add missing null value tests --- sdk/test/configuration/sdk_builder_test.cc | 11 +++-- sdk/test/configuration/yaml_resource_test.cc | 44 ++++++++++++++++++++ sdk/test/configuration/yaml_test.cc | 1 - 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/sdk/test/configuration/sdk_builder_test.cc b/sdk/test/configuration/sdk_builder_test.cc index 824b5d7df7..8558a920b0 100644 --- a/sdk/test/configuration/sdk_builder_test.cc +++ b/sdk/test/configuration/sdk_builder_test.cc @@ -1511,10 +1511,13 @@ TEST(SdkBuilder, SetResourceAttributesNullValueSkipped) // // resource: // attributes: - // service.name: ${OTEL_SERVICE_NAME} # env not set - // service.namespace: ${MY_SERVICE_NAMESPACE} # env not set - // service.version: version-from-attributes - // attributes_list: service.name=name-from-list + // -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} diff --git a/sdk/test/configuration/yaml_resource_test.cc b/sdk/test/configuration/yaml_resource_test.cc index 95b2723333..d62cdbae59 100644 --- a/sdk/test/configuration/yaml_resource_test.cc +++ b/sdk/test/configuration/yaml_resource_test.cc @@ -834,6 +834,50 @@ file_format: "1.0-resource" ASSERT_EQ(kv.count("host.name"), 1u); } +TEST(YamlResource, null_tilde_attribute_value_skipped) +{ + // value: ~ is the YAML tilde form of null — also skipped. + std::string yaml = R"( +file_format: "1.0-resource" +resource: + attributes: + - name: service.name + value: ~ + - name: host.name + value: "my-host" +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_NE(config->resource, nullptr); + ASSERT_NE(config->resource->attributes, nullptr); + const auto &kv = config->resource->attributes->kv_map; + ASSERT_EQ(kv.count("service.name"), 0u); + ASSERT_EQ(kv.count("host.name"), 1u); +} + +TEST(YamlResource, null_keyword_attribute_value_skipped) +{ + // value: null is the YAML keyword form of null — also skipped. + std::string yaml = R"( +file_format: "1.0-resource" +resource: + attributes: + - name: service.name + value: null + - name: host.name + value: "my-host" +)"; + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_NE(config->resource, nullptr); + ASSERT_NE(config->resource->attributes, nullptr); + const auto &kv = config->resource->attributes->kv_map; + ASSERT_EQ(kv.count("service.name"), 0u); + ASSERT_EQ(kv.count("host.name"), 1u); +} + TEST(YamlResource, empty_name_attribute_parse_error) { // An empty attribute name (e.g. from an unset env var) is an invalid config. diff --git a/sdk/test/configuration/yaml_test.cc b/sdk/test/configuration/yaml_test.cc index db0f2d2ae9..8a04886c94 100644 --- a/sdk/test/configuration/yaml_test.cc +++ b/sdk/test/configuration/yaml_test.cc @@ -820,4 +820,3 @@ log_level: ${ENV_NAME} ASSERT_NE(config, nullptr); ASSERT_EQ(config->log_level, opentelemetry::sdk::configuration::SeverityNumber::debug); } - From 828de82699093516a2fc0fe753a9513d05a70c1d Mon Sep 17 00:00:00 2001 From: Douglas Barker Date: Sun, 23 Aug 2026 21:20:16 -0400 Subject: [PATCH 4/4] changelog entry --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 805c832e50..9602408868 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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