Skip to content

Commit 1b2ecca

Browse files
authored
[CONFIGURATION] Support environment variable substitution for attributes (#4474)
1 parent 1c2b007 commit 1b2ecca

9 files changed

Lines changed: 663 additions & 10 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Increment the:
3030

3131
* [RESOURCE DETECTOR] Add the host resource detector
3232
[#4413](https://github.com/open-telemetry/opentelemetry-cpp/issues/4413)
33+
34+
* [CONFIGURATION] Support environment variable substitution for attributes
35+
[#4474](https://github.com/open-telemetry/opentelemetry-cpp/pull/4474)
36+
3337
* [RESOURCE DETECTOR] Add the service resource detector and builder
3438
[#4414](https://github.com/open-telemetry/opentelemetry-cpp/issues/4414)
3539
* [CONFIGURATION] deprecate config builder cmake components

‎sdk/include/opentelemetry/sdk/configuration/document_node.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class DocumentNode
4040
virtual std::size_t AsInteger() const = 0;
4141
virtual double AsDouble() const = 0;
4242
virtual std::string AsString() const = 0;
43+
virtual bool IsNull() const = 0;
4344

4445
virtual std::unique_ptr<DocumentNode> GetRequiredChildNode(const std::string &name) const = 0;
4546
virtual std::unique_ptr<DocumentNode> GetChildNode(const std::string &name) const = 0;

‎sdk/include/opentelemetry/sdk/configuration/ryml_document_node.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class RymlDocumentNode : public DocumentNode
4040
std::size_t AsInteger() const override;
4141
double AsDouble() const override;
4242
std::string AsString() const override;
43+
bool IsNull() const override;
4344

4445
std::unique_ptr<DocumentNode> GetRequiredChildNode(const std::string &name) const override;
4546
std::unique_ptr<DocumentNode> GetChildNode(const std::string &name) const override;

‎sdk/src/configuration/configuration_parser.cc‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2477,10 +2477,13 @@ std::unique_ptr<StringAttributeValueConfiguration>
24772477
ConfigurationParser::ParseStringAttributeValueConfiguration(
24782478
const std::unique_ptr<DocumentNode> &node) const
24792479
{
2480-
auto model = std::make_unique<StringAttributeValueConfiguration>();
2481-
2480+
auto model = std::make_unique<StringAttributeValueConfiguration>();
24822481
model->value = node->AsString();
2483-
2482+
// Empty string (YAML null or unset env var) is treated as null per schema nullBehavior.
2483+
if (model->value.empty())
2484+
{
2485+
return nullptr;
2486+
}
24842487
return model;
24852488
}
24862489

@@ -2613,6 +2616,21 @@ std::unique_ptr<AttributesConfiguration> ConfigurationParser::ParseAttributesCon
26132616
std::unique_ptr<AttributeValueConfiguration> value_model;
26142617

26152618
name = name_child->AsString();
2619+
2620+
// An empty name is an invalid attribute key per the OTel spec.
2621+
if (name.empty())
2622+
{
2623+
std::string message("Attribute name must not be empty (check for unset env var)");
2624+
throw InvalidSchemaException(name_child->Location(), message);
2625+
}
2626+
2627+
// Per schema nullBehavior: skip entries with a null value.
2628+
if (value_child->IsNull())
2629+
{
2630+
OTEL_INTERNAL_LOG_DEBUG("Skipping attribute '" << name << "' with null value");
2631+
continue;
2632+
}
2633+
26162634
if (type_child)
26172635
{
26182636
type = type_child->AsString();
@@ -2669,6 +2687,14 @@ std::unique_ptr<AttributesConfiguration> ConfigurationParser::ParseAttributesCon
26692687
throw InvalidSchemaException(node->Location(), message);
26702688
}
26712689

2690+
// Per schema nullBehavior: skip entries with a null/empty value (e.g. string from unset env
2691+
// var).
2692+
if (value_model == nullptr)
2693+
{
2694+
OTEL_INTERNAL_LOG_DEBUG("Skipping attribute '" << name << "' with null/empty value");
2695+
continue;
2696+
}
2697+
26722698
std::pair<std::string, std::unique_ptr<AttributeValueConfiguration>> entry(
26732699
name, std::move(value_model));
26742700
model->kv_map.insert(std::move(entry));

‎sdk/src/configuration/ryml_document_node.cc‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ bool RymlDocumentNode::AsBoolean() const
8080
}
8181
ryml::csubstr view = node_.val();
8282
std::string value(view.str, view.len);
83+
value = DoSubstitution(value);
8384
return BooleanFromString(value);
8485
}
8586

@@ -93,6 +94,7 @@ size_t RymlDocumentNode::AsInteger() const
9394
}
9495
ryml::csubstr view = node_.val();
9596
std::string value(view.str, view.len);
97+
value = DoSubstitution(value);
9698
return IntegerFromString(value);
9799
}
98100

@@ -106,6 +108,7 @@ double RymlDocumentNode::AsDouble() const
106108
}
107109
ryml::csubstr view = node_.val();
108110
std::string value(view.str, view.len);
111+
value = DoSubstitution(value);
109112
return DoubleFromString(value);
110113
}
111114

@@ -119,9 +122,21 @@ std::string RymlDocumentNode::AsString() const
119122
}
120123
ryml::csubstr view = node_.val();
121124
std::string value(view.str, view.len);
125+
value = DoSubstitution(value);
122126
return value;
123127
}
124128

129+
bool RymlDocumentNode::IsNull() const
130+
{
131+
OTEL_INTERNAL_LOG_DEBUG("RymlDocumentNode::IsNull()");
132+
133+
if (!node_.is_val() && !node_.is_keyval())
134+
{
135+
return false;
136+
}
137+
return node_.val_is_null();
138+
}
139+
125140
ryml::ConstNodeRef RymlDocumentNode::GetRequiredRymlChildNode(const std::string &name) const
126141
{
127142
if (!node_.is_map())
@@ -383,6 +398,11 @@ std::string RymlDocumentNode::GetString(const std::string &name,
383398

384399
value = DoSubstitution(value);
385400

401+
if (value.empty())
402+
{
403+
return default_value;
404+
}
405+
386406
return value;
387407
}
388408

‎sdk/src/configuration/sdk_builder.cc‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2576,7 +2576,12 @@ void SdkBuilder::SetResource(
25762576
{
25772577
for (const auto &kv : opt_model->attributes->kv_map)
25782578
{
2579-
SetResourceAttribute(sdk_attributes, kv.first, kv.second.get());
2579+
// kv_map values may be nullptr for programmatically-set null entries.
2580+
// The default behavior for resource.attributes is to ignore null values.
2581+
if (kv.second != nullptr)
2582+
{
2583+
SetResourceAttribute(sdk_attributes, kv.first, kv.second.get());
2584+
}
25802585
}
25812586
}
25822587

‎sdk/test/configuration/sdk_builder_test.cc‎

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,18 +1260,24 @@ class TestProcessResourceDetectorBuilder : public config_sdk::ProcessResourceDet
12601260
class TestExtensionResourceDetectorBuilder : public config_sdk::ExtensionResourceDetectorBuilder
12611261
{
12621262
public:
1263+
explicit TestExtensionResourceDetectorBuilder(
1264+
opentelemetry::sdk::resource::ResourceAttributes attributes)
1265+
: attributes_(std::move(attributes))
1266+
{}
1267+
12631268
std::unique_ptr<opentelemetry::sdk::resource::ResourceDetector> Build(
12641269
const config_sdk::ExtensionResourceDetectorConfiguration *model) const override
12651270
{
12661271
called = true;
12671272
name = model->name;
1268-
return std::make_unique<TestResourceDetector>(
1269-
opentelemetry::sdk::resource::ResourceAttributes{{"custom.key", "custom-value"}},
1270-
std::string{});
1273+
return std::make_unique<TestResourceDetector>(attributes_, std::string{});
12711274
}
12721275

12731276
mutable bool called{false};
12741277
mutable std::string name;
1278+
1279+
private:
1280+
opentelemetry::sdk::resource::ResourceAttributes attributes_;
12751281
};
12761282

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

13431349
TEST(SdkBuilder, SetResourceExtensionDetector)
13441350
{
1345-
auto registry = RegistryFactory::Create();
1346-
auto extension_builder = std::make_unique<TestExtensionResourceDetectorBuilder>();
1351+
auto registry = RegistryFactory::Create();
1352+
auto extension_builder = std::make_unique<TestExtensionResourceDetectorBuilder>(
1353+
opentelemetry::sdk::resource::ResourceAttributes{{"custom.key", "custom-value"}});
13471354
auto *extension_builder_p = extension_builder.get();
13481355
registry->SetExtensionResourceDetectorBuilder("my_custom_detector", std::move(extension_builder));
13491356
SdkBuilder builder(std::move(registry));
@@ -1472,3 +1479,81 @@ TEST(SdkBuilder, SetResourceFilterAppliesToDetectedAttributesOnly)
14721479
EXPECT_EQ(GetStringAttribute(resource, "process.pid"), "from-list");
14731480
EXPECT_EQ(GetStringAttribute(resource, "process.command_args"), "from-attributes");
14741481
}
1482+
1483+
TEST(SdkBuilder, SetResourceNullAttributeValueSkipped)
1484+
{
1485+
auto registry = RegistryFactory::Create();
1486+
SdkBuilder builder(std::move(registry));
1487+
1488+
auto typed_value = std::make_unique<config_sdk::StringAttributeValueConfiguration>();
1489+
typed_value->value = "kept";
1490+
1491+
auto model = std::make_unique<config_sdk::ResourceConfiguration>();
1492+
model->attributes = std::make_unique<config_sdk::AttributesConfiguration>();
1493+
model->attributes->kv_map.emplace("present", std::move(typed_value));
1494+
model->attributes->kv_map.emplace("null-entry", nullptr); // should be skipped
1495+
1496+
auto resource = opentelemetry::sdk::resource::Resource::GetEmpty();
1497+
builder.SetResource(resource, model);
1498+
1499+
EXPECT_EQ(GetStringAttribute(resource, "present"), "kept");
1500+
const auto &attrs = resource.GetAttributes();
1501+
EXPECT_EQ(attrs.find("null-entry"), attrs.end());
1502+
}
1503+
1504+
TEST(SdkBuilder, SetResourceAttributesNullValueSkipped)
1505+
{
1506+
// The default behavior for the resource.attributes field must skip null values.
1507+
// This allows optional env values to be set (e.g. OTEL_SERVICE_NAME).
1508+
// If the env var is not set then the attribute should be ignored.
1509+
1510+
// Example config created programmatically below
1511+
//
1512+
// resource:
1513+
// attributes:
1514+
// -name: service.name
1515+
// value: ${OTEL_SERVICE_NAME} # env not set
1516+
// -name: service.namespace
1517+
// value: ${MY_SERVICE_NAMESPACE} # env not set
1518+
// -name: service.version
1519+
// value: version-from-attributes
1520+
// attributes_list: "service.name=name-from-list"
1521+
// detection:
1522+
// detectors:
1523+
// - test_service_detector # detects service.{name, version, namespace}
1524+
1525+
auto registry = RegistryFactory::Create();
1526+
auto ext_detector = std::make_unique<TestExtensionResourceDetectorBuilder>(
1527+
opentelemetry::sdk::resource::ResourceAttributes{
1528+
{"service.name", "name-from-detector"},
1529+
{"service.namespace", "namespace-from-detector"},
1530+
{"service.version", "version-from-detector"}});
1531+
registry->SetExtensionResourceDetectorBuilder("test_service_detector", std::move(ext_detector));
1532+
SdkBuilder builder(std::move(registry));
1533+
1534+
auto model = std::make_unique<config_sdk::ResourceConfiguration>();
1535+
auto detector_config = std::make_unique<config_sdk::ExtensionResourceDetectorConfiguration>();
1536+
detector_config->name = "test_service_detector";
1537+
model->detection = std::make_unique<config_sdk::ResourceDetectionConfiguration>();
1538+
model->detection->detectors.push_back(std::move(detector_config));
1539+
model->attributes_list = "service.name=name-from-list,service.version=version-from-list";
1540+
1541+
// Simulate null values from unset env variables.
1542+
model->attributes = std::make_unique<config_sdk::AttributesConfiguration>();
1543+
model->attributes->kv_map.emplace("service.name", nullptr); // simulate unset OTEL_SERVICE_NAME
1544+
model->attributes->kv_map.emplace("service.namespace",
1545+
nullptr); // simulate unset MY_SERVICE_NAMESPACE
1546+
auto typed_value = std::make_unique<config_sdk::StringAttributeValueConfiguration>();
1547+
typed_value->value = "version-from-attributes";
1548+
model->attributes->kv_map.emplace("service.version", std::move(typed_value));
1549+
1550+
auto resource = opentelemetry::sdk::resource::Resource::GetEmpty();
1551+
builder.SetResource(resource, model);
1552+
1553+
// attributes_list wins over detection for service.name; nullptr in attributes doesn't override.
1554+
EXPECT_EQ(GetStringAttribute(resource, "service.name"), "name-from-list");
1555+
// detected value survives for service.namespace (no attributes_list entry, nullptr skipped).
1556+
EXPECT_EQ(GetStringAttribute(resource, "service.namespace"), "namespace-from-detector");
1557+
// attribute value for service.version overrides both list and detection.
1558+
EXPECT_EQ(GetStringAttribute(resource, "service.version"), "version-from-attributes");
1559+
}

0 commit comments

Comments
 (0)