Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2801483

Browse files
authoredMar 2, 2020
Merge dc583ad into 2bce8db
2 parents 2bce8db + dc583ad commit 2801483

8 files changed

+163
-39
lines changed
 

‎src/jaegertracing/ConfigTest.cpp

+26-22
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,20 +131,20 @@ 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_TYPE", "remote");
151-
setEnv("JAEGER_SAMPLER_PARAM", "0.33");
152-
setEnv("JAEGER_SAMPLING_ENDPOINT", "http://myagent:1234");
142+
testutils::EnvVariable::setEnv("JAEGER_SAMPLER_TYPE", "remote");
143+
testutils::EnvVariable::setEnv("JAEGER_SAMPLER_PARAM", "0.33");
144+
testutils::EnvVariable::setEnv("JAEGER_SAMPLING_ENDPOINT", "http://myagent:1234");
153145

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

157149
config.fromEnv();
158150

@@ -179,13 +171,25 @@ TEST(Config, testFromEnv)
179171

180172
ASSERT_EQ(false, config.disabled());
181173

182-
setEnv("JAEGER_DISABLED", "TRue"); // case-insensitive
183-
setEnv("JAEGER_AGENT_PORT", "445");
174+
testutils::EnvVariable::setEnv("JAEGER_DISABLED", "TRue"); // case-insensitive
175+
testutils::EnvVariable::setEnv("JAEGER_AGENT_PORT", "445");
184176

185177
config.fromEnv();
186178
ASSERT_EQ(true, config.disabled());
187179
ASSERT_EQ(std::string("host33:445"),
188180
config.reporter().localAgentHostPort());
181+
182+
testutils::EnvVariable::setEnv("JAEGER_AGENT_HOST", "");
183+
testutils::EnvVariable::setEnv("JAEGER_AGENT_PORT", "");
184+
testutils::EnvVariable::setEnv("JAEGER_ENDPOINT", "");
185+
testutils::EnvVariable::setEnv("JAEGER_REPORTER_MAX_QUEUE_SIZE", "");
186+
testutils::EnvVariable::setEnv("JAEGER_REPORTER_FLUSH_INTERVAL", "");
187+
testutils::EnvVariable::setEnv("JAEGER_REPORTER_LOG_SPANS", "");
188+
testutils::EnvVariable::setEnv("JAEGER_SAMPLER_PARAM", "");
189+
testutils::EnvVariable::setEnv("JAEGER_SAMPLER_TYPE", "");
190+
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "");
191+
testutils::EnvVariable::setEnv("JAEGER_TAGS", "");
192+
testutils::EnvVariable::setEnv("JAEGER_DISABLED", "");
189193
}
190194

191195
} // namespace jaegertracing

‎src/jaegertracing/DynamicLoad.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ static int makeTracerFactory(const char* opentracingVersion,
4141
return opentracing::incompatible_library_versions_error.value();
4242
}
4343

44-
*tracerFactory = new (std::nothrow) jaegertracing::TracerFactory{};
44+
const auto jaegerTracerFactory = new (std::nothrow) jaegertracing::TracerFactory(true);
45+
*tracerFactory = jaegerTracerFactory;
4546
if (*tracerFactory == nullptr) {
4647
*errorCategory = static_cast<const void*>(&std::generic_category());
4748
return static_cast<int>(std::errc::not_enough_memory);
@@ -50,4 +51,4 @@ static int makeTracerFactory(const char* opentracingVersion,
5051
return 0;
5152
}
5253

53-
OPENTRACING_DECLARE_IMPL_FACTORY(makeTracerFactory)
54+
OPENTRACING_DECLARE_IMPL_FACTORY(makeTracerFactory)

‎src/jaegertracing/TracerFactory.cpp

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

41-
const auto serviceNameNode = yaml["service_name"];
42-
if (!serviceNameNode) {
43-
errorMessage = "`service_name` not provided";
44-
return opentracing::make_unexpected(
45-
opentracing::invalid_configuration_error);
41+
auto tracerConfig = jaegertracing::Config::parse(yaml);
42+
43+
if (_readFromEnv) {
44+
tracerConfig.fromEnv();
4645
}
47-
if (!serviceNameNode.IsScalar()) {
48-
errorMessage = "`service_name` must be a string";
46+
47+
if (tracerConfig.serviceName().empty()) {
48+
errorMessage = "`service_name` not provided";
4949
return opentracing::make_unexpected(
5050
opentracing::invalid_configuration_error);
5151
}
5252

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

‎src/jaegertracing/TracerFactory.h

+12
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ class TracerFactory : public opentracing::TracerFactory {
2626
opentracing::expected<std::shared_ptr<opentracing::Tracer>>
2727
MakeTracer(const char* configuration, std::string& errorMessage) const
2828
noexcept override;
29+
30+
TracerFactory()
31+
: TracerFactory(false)
32+
{
33+
}
34+
35+
TracerFactory(bool readFromEnv)
36+
: _readFromEnv(readFromEnv)
37+
{
38+
}
39+
private:
40+
bool _readFromEnv;
2941
};
3042

3143
} // namespace jaegertracing

‎src/jaegertracing/TracerFactoryTest.cpp

+59-6
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17+
#include "jaegertracing/Tracer.h"
1718
#include "jaegertracing/TracerFactory.h"
1819
#include "jaegertracing/Constants.h"
20+
#include "jaegertracing/testutils/EnvVariable.h"
1921
#include <gtest/gtest.h>
2022

2123
namespace jaegertracing {
@@ -27,11 +29,11 @@ TEST(TracerFactory, testInvalidConfig)
2729
R"({
2830
"service_name": {}
2931
})" };
30-
TracerFactory tracerFactory;
32+
const auto tracerFactory = new (std::nothrow) TracerFactory(true);
3133
for (auto&& invalidConfig : invalidConfigTestCases) {
3234
std::string errorMessage;
3335
auto tracerMaybe =
34-
tracerFactory.MakeTracer(invalidConfig, errorMessage);
36+
tracerFactory->MakeTracer(invalidConfig, errorMessage);
3537
ASSERT_FALSE(tracerMaybe);
3638
ASSERT_NE(errorMessage, "");
3739
}
@@ -65,19 +67,70 @@ TEST(TracerFactory, testValidConfig)
6567
"refreshInterval": 60
6668
}
6769
})";
68-
TracerFactory tracerFactory;
70+
const auto tracerFactory = new (std::nothrow) TracerFactory(true);
6971
std::string errorMessage;
70-
auto tracerMaybe = tracerFactory.MakeTracer(config, errorMessage);
72+
auto tracerMaybe = tracerFactory->MakeTracer(config, errorMessage);
7173
ASSERT_EQ(errorMessage, "");
7274
ASSERT_TRUE(tracerMaybe);
7375
}
76+
77+
TEST(TracerFactory, testWithoutReadFromEnv)
78+
{
79+
const char* config = "";
80+
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "AService");
81+
const auto tracerFactory = new (std::nothrow) TracerFactory();
82+
std::string errorMessage;
83+
auto tracerMaybe = tracerFactory->MakeTracer(config, errorMessage);
84+
ASSERT_FALSE(tracerMaybe);
85+
ASSERT_NE(errorMessage, "");
86+
87+
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "");
88+
}
89+
90+
TEST(TracerFactory, testWithReadFromEnv)
91+
{
92+
const char* config = "";
93+
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "AService");
94+
const auto tracerFactory = new (std::nothrow) TracerFactory(true);
95+
std::string errorMessage;
96+
auto tracerMaybe = tracerFactory->MakeTracer(config, errorMessage);
97+
ASSERT_EQ(errorMessage, "");
98+
ASSERT_TRUE(tracerMaybe);
99+
100+
auto tracer = tracerMaybe.value();
101+
const auto jaegerTracer = std::dynamic_pointer_cast<jaegertracing::Tracer>(tracer);
102+
ASSERT_EQ(std::string("AService"), jaegerTracer->serviceName());
103+
104+
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "");
105+
}
106+
107+
TEST(TracerFactory, testEnvTakesPrecedence)
108+
{
109+
110+
const char* config = R"(
111+
{
112+
"service_name": "Ignored"
113+
})";
114+
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "test");
115+
const auto tracerFactory = new (std::nothrow) TracerFactory(true);
116+
std::string errorMessage;
117+
auto tracerMaybe = tracerFactory->MakeTracer(config, errorMessage);
118+
ASSERT_EQ(errorMessage, "");
119+
ASSERT_TRUE(tracerMaybe);
120+
121+
auto tracer = tracerMaybe.value();
122+
const auto jaegerTracer = std::dynamic_pointer_cast<jaegertracing::Tracer>(tracer);
123+
ASSERT_EQ(std::string("test"), jaegerTracer->serviceName());
124+
125+
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "");
126+
}
74127
#else
75128
TEST(TracerFactory, failsWithoutYAML)
76129
{
77130
const char* config = "";
78-
TracerFactory tracerFactory;
131+
const auto tracerFactory = new (std::nothrow) TracerFactory(true);
79132
std::string errorMessage;
80-
auto tracerMaybe = tracerFactory.MakeTracer(config, errorMessage);
133+
auto tracerMaybe = tracerFactory->MakeTracer(config, errorMessage);
81134
ASSERT_NE(errorMessage, "");
82135
ASSERT_FALSE(tracerMaybe);
83136
}
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"
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
namespace jaegertracing {
23+
namespace testutils {
24+
namespace EnvVariable {
25+
26+
inline void setEnv(const char *variable, const char *value) {
27+
#ifdef WIN32
28+
_putenv_s(variable, value);
29+
#else
30+
setenv(variable, value, true);
31+
#endif
32+
}
33+
34+
} // namespace EnvVariable
35+
} // namespace testutils
36+
} // namespace jaegertracing
37+
38+
#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)
This repository has been archived.