diff --git a/src/jaegertracing/ConfigTest.cpp b/src/jaegertracing/ConfigTest.cpp index 5392fe9c..128c6007 100644 --- a/src/jaegertracing/ConfigTest.cpp +++ b/src/jaegertracing/ConfigTest.cpp @@ -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 #include @@ -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 tags; @@ -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(); @@ -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()); diff --git a/src/jaegertracing/TracerFactory.cpp b/src/jaegertracing/TracerFactory.cpp index d743d7eb..68a27d47 100644 --- a/src/jaegertracing/TracerFactory.cpp +++ b/src/jaegertracing/TracerFactory.cpp @@ -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&) { diff --git a/src/jaegertracing/TracerFactoryTest.cpp b/src/jaegertracing/TracerFactoryTest.cpp index cc6a3e31..b530cb15 100644 --- a/src/jaegertracing/TracerFactoryTest.cpp +++ b/src/jaegertracing/TracerFactoryTest.cpp @@ -16,12 +16,14 @@ #include "jaegertracing/TracerFactory.h" #include "jaegertracing/Constants.h" +#include "jaegertracing/testutils/EnvVariable.h" #include namespace jaegertracing { #ifdef JAEGERTRACING_WITH_YAML_CPP TEST(TracerFactory, testInvalidConfig) { + testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", ""); const char* invalidConfigTestCases[] = { "", "abc: {", R"({ @@ -65,12 +67,43 @@ 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); + + const auto jaegerTracer = std::static_pointer_cast(tracerMaybe); + ASSERT_EQ(std::string("test"), jaegerTracer->serviceName()); +} + #else TEST(TracerFactory, failsWithoutYAML) { diff --git a/src/jaegertracing/testutils/EnvVariable.cpp b/src/jaegertracing/testutils/EnvVariable.cpp new file mode 100644 index 00000000..726bbb23 --- /dev/null +++ b/src/jaegertracing/testutils/EnvVariable.cpp @@ -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" diff --git a/src/jaegertracing/testutils/EnvVariable.h b/src/jaegertracing/testutils/EnvVariable.h new file mode 100644 index 00000000..aa487fd6 --- /dev/null +++ b/src/jaegertracing/testutils/EnvVariable.h @@ -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 + +#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 diff --git a/src/jaegertracing/utils/EnvVariable.h b/src/jaegertracing/utils/EnvVariable.h index 8696248a..dc34f823 100644 --- a/src/jaegertracing/utils/EnvVariable.h +++ b/src/jaegertracing/utils/EnvVariable.h @@ -62,4 +62,4 @@ inline std::pair getBoolVariable(const char* envVar) } // namespace utils } // namespace jaegertracing -#endif // JAEGERTRACING_UTILS_HEXPARSING_H +#endif // JAEGERTRACING_UTILS_ENV_VARIABLE_H