Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.

Commit 322395b

Browse files
Merge 4c6fd45 into 741b1af
2 parents 741b1af + 4c6fd45 commit 322395b

File tree

6 files changed

+111
-30
lines changed

6 files changed

+111
-30
lines changed

src/jaegertracing/ConfigTest.cpp

+13-21
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "jaegertracing/propagation/HeadersConfig.h"
2020
#include "jaegertracing/samplers/Config.h"
2121
#include "jaegertracing/utils/YAML.h"
22+
#include "jaegertracing/testutils/EnvVariable.h"
2223
#include <gtest/gtest.h>
2324

2425
#include <cstdlib>
@@ -95,15 +96,6 @@ TEST(Config, testZeroSamplingParam)
9596

9697
#endif // JAEGERTRACING_WITH_YAML_CPP
9798

98-
99-
void setEnv(const char *variable, const char *value) {
100-
#ifdef WIN32
101-
_putenv_s(variable, value);
102-
#else
103-
setenv(variable, value, true);
104-
#endif
105-
}
106-
10799
TEST(Config, testFromEnv)
108100
{
109101
std::vector<Tag> tags;
@@ -139,19 +131,19 @@ TEST(Config, testFromEnv)
139131
ASSERT_EQ(.7, config.sampler().param());
140132
ASSERT_EQ(std::string("probabilistic"), config.sampler().type());
141133

142-
setEnv("JAEGER_AGENT_HOST", "host33");
143-
setEnv("JAEGER_AGENT_PORT", "45");
144-
setEnv("JAEGER_ENDPOINT", "http://host34:56567");
134+
testutils::EnvVariable::setEnv("JAEGER_AGENT_HOST", "host33");
135+
testutils::EnvVariable::setEnv("JAEGER_AGENT_PORT", "45");
136+
testutils::EnvVariable::setEnv("JAEGER_ENDPOINT", "http://host34:56567");
145137

146-
setEnv("JAEGER_REPORTER_MAX_QUEUE_SIZE", "33");
147-
setEnv("JAEGER_REPORTER_FLUSH_INTERVAL", "45");
148-
setEnv("JAEGER_REPORTER_LOG_SPANS", "true");
138+
testutils::EnvVariable::setEnv("JAEGER_REPORTER_MAX_QUEUE_SIZE", "33");
139+
testutils::EnvVariable::setEnv("JAEGER_REPORTER_FLUSH_INTERVAL", "45");
140+
testutils::EnvVariable::setEnv("JAEGER_REPORTER_LOG_SPANS", "true");
149141

150-
setEnv("JAEGER_SAMPLER_PARAM", "33");
151-
setEnv("JAEGER_SAMPLER_TYPE", "const");
142+
testutils::EnvVariable::setEnv("JAEGER_SAMPLER_PARAM", "33");
143+
testutils::EnvVariable::setEnv("JAEGER_SAMPLER_TYPE", "const");
152144

153-
setEnv("JAEGER_SERVICE_NAME", "AService");
154-
setEnv("JAEGER_TAGS", "hostname=foobar,my.app.version=4.5.6");
145+
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "AService");
146+
testutils::EnvVariable::setEnv("JAEGER_TAGS", "hostname=foobar,my.app.version=4.5.6");
155147

156148
config.fromEnv();
157149

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

178170
ASSERT_EQ(false, config.disabled());
179171

180-
setEnv("JAEGER_DISABLED", "TRue"); // case-insensitive
181-
setEnv("JAEGER_AGENT_PORT", "445");
172+
testutils::EnvVariable::setEnv("JAEGER_DISABLED", "TRue"); // case-insensitive
173+
testutils::EnvVariable::setEnv("JAEGER_AGENT_PORT", "445");
182174

183175
config.fromEnv();
184176
ASSERT_EQ(true, config.disabled());

src/jaegertracing/TracerFactory.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,16 @@ TracerFactory::MakeTracer(const char* configuration,
3838
opentracing::configuration_parse_error);
3939
}
4040

41-
const auto serviceNameNode = yaml["service_name"];
42-
if (!serviceNameNode) {
41+
auto tracerConfig = jaegertracing::Config::parse(yaml);
42+
43+
tracerConfig.fromEnv();
44+
45+
if (tracerConfig.serviceName().empty()) {
4346
errorMessage = "`service_name` not provided";
4447
return opentracing::make_unexpected(
4548
opentracing::invalid_configuration_error);
4649
}
47-
if (!serviceNameNode.IsScalar()) {
48-
errorMessage = "`service_name` must be a string";
49-
return opentracing::make_unexpected(
50-
opentracing::invalid_configuration_error);
51-
}
5250

53-
const auto tracerConfig = jaegertracing::Config::parse(yaml);
5451
return jaegertracing::Tracer::make(tracerConfig);
5552
#endif // JAEGERTRACING_WITH_YAML_CPP
5653
} catch (const std::bad_alloc&) {

src/jaegertracing/TracerFactoryTest.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
#include "jaegertracing/TracerFactory.h"
1818
#include "jaegertracing/Constants.h"
19+
#include "jaegertracing/testutils/EnvVariable.h"
1920
#include <gtest/gtest.h>
2021

2122
namespace jaegertracing {
2223
#ifdef JAEGERTRACING_WITH_YAML_CPP
2324
TEST(TracerFactory, testInvalidConfig)
2425
{
26+
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "");
2527
const char* invalidConfigTestCases[] = { "",
2628
"abc: {",
2729
R"({
@@ -65,12 +67,44 @@ TEST(TracerFactory, testValidConfig)
6567
"refreshInterval": 60
6668
}
6769
})";
70+
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "");
6871
TracerFactory tracerFactory;
6972
std::string errorMessage;
7073
auto tracerMaybe = tracerFactory.MakeTracer(config, errorMessage);
7174
ASSERT_EQ(errorMessage, "");
7275
ASSERT_TRUE(tracerMaybe);
7376
}
77+
78+
TEST(TracerFactory, testEnvironmentConfig)
79+
{
80+
const char* config = "";
81+
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "AService");
82+
TracerFactory tracerFactory;
83+
std::string errorMessage;
84+
auto tracerMaybe = tracerFactory.MakeTracer(config, errorMessage);
85+
ASSERT_EQ(errorMessage, "");
86+
ASSERT_TRUE(tracerMaybe);
87+
}
88+
89+
TEST(TracerFactory, testConfigAndEnvironment)
90+
{
91+
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "");
92+
const char* config = R"(
93+
{
94+
"service_name": "test"
95+
})";
96+
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "AService");
97+
TracerFactory tracerFactory;
98+
std::string errorMessage;
99+
auto tracerMaybe = tracerFactory.MakeTracer(config, errorMessage);
100+
ASSERT_EQ(errorMessage, "");
101+
ASSERT_TRUE(tracerMaybe);
102+
103+
const auto tracer = tracerMaybe.value();
104+
const auto jaegerTracer = dynamic_cast<Tracer>(tracer);
105+
ASSERT_EQ(std::string("test"), jaegerTracer->serviceName());
106+
}
107+
74108
#else
75109
TEST(TracerFactory, failsWithoutYAML)
76110
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (c) 2019 The Jaeger Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "jaegertracing/testutils/EnvVariable.h"
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2019 The Jaeger Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef JAEGERTRACING_TESTUTILS_ENVVARIABLE_H
18+
#define JAEGERTRACING_TESTUTILS_ENVVARIABLE_H
19+
20+
#include <string>
21+
22+
#include "jaegertracing/Tracer.h"
23+
#include "jaegertracing/testutils/MockAgent.h"
24+
25+
namespace jaegertracing {
26+
namespace testutils {
27+
namespace EnvVariable {
28+
29+
inline void setEnv(const char *variable, const char *value) {
30+
#ifdef WIN32
31+
_putenv_s(variable, value);
32+
#else
33+
setenv(variable, value, true);
34+
#endif
35+
}
36+
37+
} // namespace EnvVariable
38+
} // namespace testutils
39+
} // namespace jaegertracing
40+
41+
#endif // JAEGERTRACING_TESTUTILS_ENVVARIABLE_H

src/jaegertracing/utils/EnvVariable.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,4 @@ inline std::pair<bool, bool> getBoolVariable(const char* envVar)
6262
} // namespace utils
6363
} // namespace jaegertracing
6464

65-
#endif // JAEGERTRACING_UTILS_HEXPARSING_H
65+
#endif // JAEGERTRACING_UTILS_ENV_VARIABLE_H

0 commit comments

Comments
 (0)