Skip to content

Commit

Permalink
Support plugin configuration via environment variables
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Würbach <johannes.wuerbach@googlemail.com>
  • Loading branch information
johanneswuerbach committed Dec 5, 2019
1 parent 741b1af commit 788f49c
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 30 deletions.
34 changes: 13 additions & 21 deletions src/jaegertracing/ConfigTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "jaegertracing/propagation/HeadersConfig.h"
#include "jaegertracing/samplers/Config.h"
#include "jaegertracing/utils/YAML.h"
#include "jaegertracing/testutils/EnvVariable.h"
#include <gtest/gtest.h>

#include <cstdlib>
Expand Down Expand Up @@ -95,15 +96,6 @@ TEST(Config, testZeroSamplingParam)

#endif // JAEGERTRACING_WITH_YAML_CPP


void setEnv(const char *variable, const char *value) {
#ifdef WIN32
_putenv_s(variable, value);
#else
setenv(variable, value, true);
#endif
}

TEST(Config, testFromEnv)
{
std::vector<Tag> tags;
Expand Down Expand Up @@ -139,19 +131,19 @@ TEST(Config, testFromEnv)
ASSERT_EQ(.7, config.sampler().param());
ASSERT_EQ(std::string("probabilistic"), config.sampler().type());

setEnv("JAEGER_AGENT_HOST", "host33");
setEnv("JAEGER_AGENT_PORT", "45");
setEnv("JAEGER_ENDPOINT", "http://host34:56567");
testutils::EnvVariable::setEnv("JAEGER_AGENT_HOST", "host33");
testutils::EnvVariable::setEnv("JAEGER_AGENT_PORT", "45");
testutils::EnvVariable::setEnv("JAEGER_ENDPOINT", "http://host34:56567");

setEnv("JAEGER_REPORTER_MAX_QUEUE_SIZE", "33");
setEnv("JAEGER_REPORTER_FLUSH_INTERVAL", "45");
setEnv("JAEGER_REPORTER_LOG_SPANS", "true");
testutils::EnvVariable::setEnv("JAEGER_REPORTER_MAX_QUEUE_SIZE", "33");
testutils::EnvVariable::setEnv("JAEGER_REPORTER_FLUSH_INTERVAL", "45");
testutils::EnvVariable::setEnv("JAEGER_REPORTER_LOG_SPANS", "true");

setEnv("JAEGER_SAMPLER_PARAM", "33");
setEnv("JAEGER_SAMPLER_TYPE", "const");
testutils::EnvVariable::setEnv("JAEGER_SAMPLER_PARAM", "33");
testutils::EnvVariable::setEnv("JAEGER_SAMPLER_TYPE", "const");

setEnv("JAEGER_SERVICE_NAME", "AService");
setEnv("JAEGER_TAGS", "hostname=foobar,my.app.version=4.5.6");
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "AService");
testutils::EnvVariable::setEnv("JAEGER_TAGS", "hostname=foobar,my.app.version=4.5.6");

config.fromEnv();

Expand All @@ -177,8 +169,8 @@ TEST(Config, testFromEnv)

ASSERT_EQ(false, config.disabled());

setEnv("JAEGER_DISABLED", "TRue"); // case-insensitive
setEnv("JAEGER_AGENT_PORT", "445");
testutils::EnvVariable::setEnv("JAEGER_DISABLED", "TRue"); // case-insensitive
testutils::EnvVariable::setEnv("JAEGER_AGENT_PORT", "445");

config.fromEnv();
ASSERT_EQ(true, config.disabled());
Expand Down
13 changes: 5 additions & 8 deletions src/jaegertracing/TracerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,16 @@ TracerFactory::MakeTracer(const char* configuration,
opentracing::configuration_parse_error);
}

const auto serviceNameNode = yaml["service_name"];
if (!serviceNameNode) {
auto tracerConfig = jaegertracing::Config::parse(yaml);

tracerConfig.fromEnv();

if (tracerConfig.serviceName().empty()) {
errorMessage = "`service_name` not provided";
return opentracing::make_unexpected(
opentracing::invalid_configuration_error);
}
if (!serviceNameNode.IsScalar()) {
errorMessage = "`service_name` must be a string";
return opentracing::make_unexpected(
opentracing::invalid_configuration_error);
}

const auto tracerConfig = jaegertracing::Config::parse(yaml);
return jaegertracing::Tracer::make(tracerConfig);
#endif // JAEGERTRACING_WITH_YAML_CPP
} catch (const std::bad_alloc&) {
Expand Down
31 changes: 31 additions & 0 deletions src/jaegertracing/TracerFactoryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

#include "jaegertracing/TracerFactory.h"
#include "jaegertracing/Constants.h"
#include "jaegertracing/testutils/EnvVariable.h"
#include <gtest/gtest.h>

namespace jaegertracing {
#ifdef JAEGERTRACING_WITH_YAML_CPP
TEST(TracerFactory, testInvalidConfig)
{
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "");
const char* invalidConfigTestCases[] = { "",
"abc: {",
R"({
Expand Down Expand Up @@ -65,12 +67,41 @@ TEST(TracerFactory, testValidConfig)
"refreshInterval": 60
}
})";
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "");
TracerFactory tracerFactory;
std::string errorMessage;
auto tracerMaybe = tracerFactory.MakeTracer(config, errorMessage);
ASSERT_EQ(errorMessage, "");
ASSERT_TRUE(tracerMaybe);
}

TEST(TracerFactory, testEnvironmentConfig)
{
const char* config = "";
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "AService");
TracerFactory tracerFactory;
std::string errorMessage;
auto tracerMaybe = tracerFactory.MakeTracer(config, errorMessage);
ASSERT_EQ(errorMessage, "");
ASSERT_TRUE(tracerMaybe);
}

TEST(TracerFactory, testConfigAndEnvironment)
{
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "");
const char* config = R"(
{
"service_name": "test"
})";
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "AService");
TracerFactory tracerFactory;
std::string errorMessage;
auto tracerMaybe = tracerFactory.MakeTracer(config, errorMessage);
ASSERT_EQ(errorMessage, "");
ASSERT_TRUE(tracerMaybe);
ASSERT_EQ(tracerMaybe.serviceName(), "test")
}

#else
TEST(TracerFactory, failsWithoutYAML)
{
Expand Down
17 changes: 17 additions & 0 deletions src/jaegertracing/testutils/EnvVariable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) 2019 The Jaeger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "jaegertracing/testutils/EnvVariable.h"
41 changes: 41 additions & 0 deletions src/jaegertracing/testutils/EnvVariable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2019 The Jaeger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef JAEGERTRACING_TESTUTILS_ENVVARIABLE_H
#define JAEGERTRACING_TESTUTILS_ENVVARIABLE_H

#include <string>

#include "jaegertracing/Tracer.h"
#include "jaegertracing/testutils/MockAgent.h"

namespace jaegertracing {
namespace testutils {
namespace EnvVariable {

inline void setEnv(const char *variable, const char *value) {
#ifdef WIN32
_putenv_s(variable, value);
#else
setenv(variable, value, true);
#endif
}

} // namespace EnvVariable
} // namespace testutils
} // namespace jaegertracing

#endif // JAEGERTRACING_TESTUTILS_ENVVARIABLE_H
2 changes: 1 addition & 1 deletion src/jaegertracing/utils/EnvVariable.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ inline std::pair<bool, bool> getBoolVariable(const char* envVar)
} // namespace utils
} // namespace jaegertracing

#endif // JAEGERTRACING_UTILS_HEXPARSING_H
#endif // JAEGERTRACING_UTILS_ENV_VARIABLE_H

0 comments on commit 788f49c

Please sign in to comment.