Skip to content

Commit

Permalink
POC - env var substitution
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff committed Feb 25, 2024
1 parent fde1025 commit 3c0465f
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 1 deletion.
7 changes: 7 additions & 0 deletions sdk/include/opentelemetry/sdk/configuration/document_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ class DocumentNode
virtual PropertiesNodeConstIterator end_properties() const = 0;

virtual std::string Dump() const = 0;

protected:
void DoSubstitution(std::string &value);

bool BooleanFromString(const std::string &value);
size_t IntegerFromString(const std::string &value);
double DoubleFromString(const std::string &value);
};

class DocumentNodeConstIteratorImpl
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 @@ -5,7 +5,7 @@

add_library(
opentelemetry_configuration
configuration_factory.cc yaml_configuration_factory.cc ryml_document.cc
configuration_factory.cc document_node.cc yaml_configuration_factory.cc ryml_document.cc
ryml_document_node.cc)

set_target_properties(opentelemetry_configuration PROPERTIES EXPORT_NAME
Expand Down
103 changes: 103 additions & 0 deletions sdk/src/configuration/document_node.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/sdk/common/global_log_handler.h"

#include "opentelemetry/sdk/configuration/document_node.h"
#include "opentelemetry/sdk/configuration/invalid_schema_exception.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace configuration
{

void DocumentNode::DoSubstitution(std::string &value)
{
size_t len = value.length();
char c;

if (len < 4)
{
return;
}

c = value[0];
if (c != '$')
{
return;
}

c = value[1];
if (c != '{')
{
return;
}

c = value[len - 1];
if (c != '}')
{
return;
}

c = value[2];
if (!std::isalpha(c) && c != '_')
{
return;
}

for (int i = 3; i <= len - 2; i++)
{
c = value[i];
if (!std::isalnum(c) && c != '_')
{
return;
}
}

// value is of the form ${ENV_NAME}

std::string name = value.substr(2, len - 3);

const char *sub = std::getenv(name.c_str());
if (sub != nullptr)
{
value = sub;
}
else
{
value = "";
}
}

bool DocumentNode::BooleanFromString(const std::string &value)
{
if (value == "true")
{
return true;
}

if (value == "false")
{
return false;
}

throw InvalidSchemaException("Illegal bool value");
}

size_t DocumentNode::IntegerFromString(const std::string &value)
{
size_t val = atoll(value.c_str());
return val;
}

double DocumentNode::DoubleFromString(const std::string &value)
{
double val = atof(value.c_str());
return val;
}

} // namespace configuration
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
5 changes: 5 additions & 0 deletions sdk/src/configuration/ryml_document_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ std::string RymlDocumentNode::GetRequiredString(const std::string &name)
ryml::csubstr view = ryml_child.val();
std::string value(view.str, view.len);

DoSubstitution(value);

return value;
}

Expand All @@ -272,6 +274,9 @@ std::string RymlDocumentNode::GetString(const std::string &name, const std::stri

ryml::csubstr view = ryml_child.val();
std::string value(view.str, view.len);

DoSubstitution(value);

return value;
}

Expand Down
40 changes: 40 additions & 0 deletions sdk/test/configuration/yaml_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

#include <gtest/gtest.h>
#include <stdlib.h>

#include "opentelemetry/sdk/configuration/yaml_configuration_factory.h"

Expand Down Expand Up @@ -143,3 +144,42 @@ file_format: xx.yy
auto config = DoParse(yaml);
ASSERT_EQ(config, nullptr);
}

TEST(Yaml, no_substitution)
{
unsetenv("ENV_NAME");

std::string yaml = R"(
file_format: ${ENV_NAME}
)";

auto config = DoParse(yaml);
ASSERT_NE(config, nullptr);
ASSERT_EQ(config->file_format, "");
}

TEST(Yaml, empty_substitution)
{
setenv("ENV_NAME", "", 1);

std::string yaml = R"(
file_format: ${ENV_NAME}
)";

auto config = DoParse(yaml);
ASSERT_NE(config, nullptr);
ASSERT_EQ(config->file_format, "");
}

TEST(Yaml, with_substitution)
{
setenv("ENV_NAME", "foo.bar", 1);

std::string yaml = R"(
file_format: ${ENV_NAME}
)";

auto config = DoParse(yaml);
ASSERT_NE(config, nullptr);
ASSERT_EQ(config->file_format, "foo.bar");
}

0 comments on commit 3c0465f

Please sign in to comment.