From 0734c82106a8b1bf2a9c61373bf3c6c3495dece2 Mon Sep 17 00:00:00 2001 From: "FR-MUREX-COM\\mchaikhadouaihy" Date: Sat, 5 Oct 2019 19:26:13 +0200 Subject: [PATCH] Support configuring client from environment variables (fix #178) Signed-off-by: FR-MUREX-COM\mchaikhadouaihy --- CMakeLists.txt | 1 + README.md | 21 ++++++++ src/jaegertracing/Config.cpp | 50 ++++++++++++++++++ src/jaegertracing/Config.h | 25 ++++++++- src/jaegertracing/ConfigTest.cpp | 69 +++++++++++++++++++++++++ src/jaegertracing/Tracer.cpp | 1 + src/jaegertracing/Tracer.h | 35 +++++++++++++ src/jaegertracing/TracerFactory.cpp | 3 +- src/jaegertracing/TracerTest.cpp | 32 ++++++++++++ src/jaegertracing/net/IPAddress.h | 10 +++- src/jaegertracing/reporters/Config.cpp | 63 ++++++++++++++++++++++ src/jaegertracing/reporters/Config.h | 12 +++++ src/jaegertracing/samplers/Config.cpp | 27 ++++++++++ src/jaegertracing/samplers/Config.h | 6 +++ src/jaegertracing/utils/EnvVariable.cpp | 17 ++++++ src/jaegertracing/utils/EnvVariable.h | 65 +++++++++++++++++++++++ 16 files changed, 431 insertions(+), 6 deletions(-) create mode 100644 src/jaegertracing/utils/EnvVariable.cpp create mode 100644 src/jaegertracing/utils/EnvVariable.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fce8b09b..3c1a689f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -210,6 +210,7 @@ set(SRC src/jaegertracing/thrift-gen/zipkincore_types.cpp src/jaegertracing/utils/ErrorUtil.cpp src/jaegertracing/utils/HexParsing.cpp + src/jaegertracing/utils/EnvVariable.cpp src/jaegertracing/utils/RateLimiter.cpp src/jaegertracing/utils/UDPTransporter.cpp src/jaegertracing/utils/HTTPTransporter.cpp diff --git a/README.md b/README.md index 9421949b..83bb97f8 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,27 @@ sampler: samplingServerURL: http://jaeger-agent.local:5778 ``` +### Configuration via Environment + +It's possible to populate the tracer configuration from the environement variables by calling `jaegertracing::Config::fromEnv`. + +The following property names are currently available: + +Property | Description +--- | --- +JAEGER_SERVICE_NAME | The service name +JAEGER_DISABLED _(not recommended)_ | Instructs the Configuration to return a no-op tracer +JAEGER_AGENT_HOST | The hostname for communicating with agent via UDP +JAEGER_AGENT_PORT | The port for communicating with agent via UDP +JAEGER_ENDPOINT | The traces endpoint, in case the client should connect directly to the Collector, like http://jaeger-collector:14268/api/traces +JAEGER_REPORTER_LOG_SPANS | Whether the reporter should also log the spans +JAEGER_REPORTER_MAX_QUEUE_SIZE | The reporter's maximum queue size +JAEGER_REPORTER_FLUSH_INTERVAL | The reporter's flush interval (ms) +JAEGER_SAMPLER_TYPE | The [sampler type](https://www.jaegertracing.io/docs/latest/sampling/#client-sampling-configuration) +JAEGER_SAMPLER_PARAM | The sampler parameter (number) +JAEGER_SAMPLER_MANAGER_HOST_PORT | The host name and port when using the remote controlled sampler +JAEGER_TAGS | A comma separated list of `name = value` tracer level tags, which get added to all reported spans. The value can also refer to an environment variable using the format `${envVarName:default}`, where the `:default` is optional, and identifies a value to be used if the environment variable cannot be found + ## License [Apache 2.0 License](./LICENSE). diff --git a/src/jaegertracing/Config.cpp b/src/jaegertracing/Config.cpp index cc38961f..abf28bcc 100644 --- a/src/jaegertracing/Config.cpp +++ b/src/jaegertracing/Config.cpp @@ -15,3 +15,53 @@ */ #include "jaegertracing/Config.h" +#include "jaegertracing/samplers/Config.h" +#include "jaegertracing/utils/EnvVariable.h" + +namespace jaegertracing { + +constexpr const char* Config::kJAEGER_SERVICE_NAME_ENV_PROP; +constexpr const char* Config::kJAEGER_TAGS_ENV_PROP; +constexpr const char* Config::kJAEGER_JAEGER_DISABLED_ENV_PROP; + +void Config::fromEnv() +{ + const auto disabled = + utils::EnvVariable::getBoolVariable(kJAEGER_JAEGER_DISABLED_ENV_PROP); + if (disabled.first) { + _disabled = disabled.second; + } + + const auto serviceName = + utils::EnvVariable::getStringVariable(kJAEGER_SERVICE_NAME_ENV_PROP); + if (!serviceName.empty()) { + _serviceName = serviceName; + } + + const auto tags = + utils::EnvVariable::getStringVariable(kJAEGER_TAGS_ENV_PROP); + if (!tags.empty()) { + std::string tag; + std::istringstream tagsStream(tags); + while (std::getline(tagsStream, tag, ',')) { + + std::istringstream tagStream(tag); + + std::string tagKey; + std::string tagValue; + if (std::getline(tagStream, tagKey, '=')) { + std::getline(tagStream, tagValue, '='); + if (std::getline(tagStream, tagValue, '=')) { + // error, should be logged somewhere + } + else { + _tags.emplace_back(tagKey, tagValue); + } + } + } + } + _reporter.fromEnv(); + _sampler.fromEnv(); +} + +} // namespace jaegertracing diff --git a/src/jaegertracing/Config.h b/src/jaegertracing/Config.h index 5257840e..ba32fc42 100644 --- a/src/jaegertracing/Config.h +++ b/src/jaegertracing/Config.h @@ -19,6 +19,7 @@ #include "jaegertracing/Compilers.h" #include "jaegertracing/Constants.h" +#include "jaegertracing/Tag.h" #include "jaegertracing/baggage/RestrictionsConfig.h" #include "jaegertracing/propagation/HeadersConfig.h" #include "jaegertracing/reporters/Config.h" @@ -29,6 +30,11 @@ namespace jaegertracing { class Config { public: + + static constexpr auto kJAEGER_SERVICE_NAME_ENV_PROP = "JAEGER_SERVICE_NAME"; + static constexpr auto kJAEGER_TAGS_ENV_PROP = "JAEGER_TAGS"; + static constexpr auto kJAEGER_JAEGER_DISABLED_ENV_PROP = "JAEGER_DISABLED"; + #ifdef JAEGERTRACING_WITH_YAML_CPP static Config parse(const YAML::Node& configYAML) @@ -37,6 +43,9 @@ class Config { return Config(); } + const auto serviceName = + utils::yaml::findOrDefault(configYAML, "service_name", ""); + const auto disabled = utils::yaml::findOrDefault(configYAML, "disabled", false); const auto samplerNode = configYAML["sampler"]; @@ -49,7 +58,7 @@ class Config { const auto baggageRestrictions = baggage::RestrictionsConfig::parse(baggageRestrictionsNode); return Config( - disabled, sampler, reporter, headers, baggageRestrictions); + disabled, sampler, reporter, headers, baggageRestrictions, serviceName); } #endif // JAEGERTRACING_WITH_YAML_CPP @@ -60,8 +69,12 @@ class Config { const propagation::HeadersConfig& headers = propagation::HeadersConfig(), const baggage::RestrictionsConfig& baggageRestrictions = - baggage::RestrictionsConfig()) + baggage::RestrictionsConfig(), + const std::string& serviceName = "", + const std::vector tags = std::vector()) : _disabled(disabled) + , _serviceName(serviceName) + , _tags(tags) , _sampler(sampler) , _reporter(reporter) , _headers(headers) @@ -82,8 +95,16 @@ class Config { return _baggageRestrictions; } + const std::string& serviceName() const { return _serviceName; } + + const std::vector& tags() const { return _tags; } + + void fromEnv(); + private: bool _disabled; + std::string _serviceName; + std::vector< Tag > _tags; samplers::Config _sampler; reporters::Config _reporter; propagation::HeadersConfig _headers; diff --git a/src/jaegertracing/ConfigTest.cpp b/src/jaegertracing/ConfigTest.cpp index 1079cc97..091e60f3 100644 --- a/src/jaegertracing/ConfigTest.cpp +++ b/src/jaegertracing/ConfigTest.cpp @@ -21,6 +21,8 @@ #include "jaegertracing/utils/YAML.h" #include +#include + namespace jaegertracing { #ifdef JAEGERTRACING_WITH_YAML_CPP @@ -93,4 +95,71 @@ 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) +{ + setEnv(reporters::Config::kJAEGER_AGENT_HOST_ENV_PROP, "host33"); + setEnv(reporters::Config::kJAEGER_AGENT_PORT_ENV_PROP, "45"); + setEnv(reporters::Config::kJAEGER_ENDPOINT_ENV_PROP, + "http://host34:56567"); + + setEnv(reporters::Config::kJAEGER_REPORTER_MAX_QUEUE_SIZE_ENV_PROP, + "33"); + setEnv(reporters::Config::kJAEGER_REPORTER_FLUSH_INTERVAL_ENV_PROP, + "45"); + setEnv( + reporters::Config::kJAEGER_REPORTER_LOG_SPANS_ENV_PROP, "true"); + + setEnv(samplers::Config::kJAEGER_SAMPLER_PARAM_ENV_PROP, "33"); + setEnv(samplers::Config::kJAEGER_SAMPLER_TYPE_ENV_PROP, "const"); + setEnv(samplers::Config::kJAEGER_SAMPLER_MANAGER_HOST_PORT_ENV_PROP, + "host34:56"); + + setEnv(Config::kJAEGER_SERVICE_NAME_ENV_PROP, "AService"); + setEnv(Config::kJAEGER_TAGS_ENV_PROP, + "hostname=foobar,my.app.version=1.2.3"); + + Config config; + + config.fromEnv(); + + ASSERT_EQ(std::string("http://host34:56567"), config.reporter().endpoint()); + ASSERT_EQ(std::string("host33:45"), config.reporter().localAgentHostPort()); + + ASSERT_EQ(33, config.reporter().queueSize()); + ASSERT_EQ(std::chrono::milliseconds(45), + config.reporter().bufferFlushInterval()); + ASSERT_EQ(true, config.reporter().logSpans()); + + ASSERT_EQ(33., config.sampler().param()); + ASSERT_EQ(std::string("const"), config.sampler().type()); + ASSERT_EQ(std::string("http://host34:56/sampling"), + config.sampler().samplingServerURL()); + + ASSERT_EQ(std::string("AService"), config.serviceName()); + + std::vector expectedTags; + expectedTags.emplace_back("hostname", std::string("foobar")); + expectedTags.emplace_back("my.app.version", std::string("1.2.3")); + ASSERT_EQ(expectedTags, config.tags()); + + ASSERT_EQ(false, config.disabled()); + + setEnv(Config::kJAEGER_JAEGER_DISABLED_ENV_PROP, "TRue"); + setEnv(reporters::Config::kJAEGER_AGENT_PORT_ENV_PROP, "445"); + + config.fromEnv(); + ASSERT_EQ(true, config.disabled()); + ASSERT_EQ(std::string("host33:445"), + config.reporter().localAgentHostPort()); +} + } // namespace jaegertracing diff --git a/src/jaegertracing/Tracer.cpp b/src/jaegertracing/Tracer.cpp index 6aaf68cc..331d44b0 100644 --- a/src/jaegertracing/Tracer.cpp +++ b/src/jaegertracing/Tracer.cpp @@ -239,6 +239,7 @@ Tracer::make(const std::string& serviceName, logger, metrics, config.headers(), + config.tags(), options)); } diff --git a/src/jaegertracing/Tracer.h b/src/jaegertracing/Tracer.h index 7fb7cdbb..90063be2 100644 --- a/src/jaegertracing/Tracer.h +++ b/src/jaegertracing/Tracer.h @@ -53,6 +53,13 @@ class Tracer : public opentracing::Tracer, static constexpr auto kGen128BitOption = 1; + static std::shared_ptr make(const Config& config) + { + return make(config.serviceName(), + config, + std::shared_ptr(logging::nullLogger())); + } + static std::shared_ptr make(const std::string& serviceName, const Config& config) { @@ -70,6 +77,13 @@ class Tracer : public opentracing::Tracer, return make(serviceName, config, logger, factory); } + static std::shared_ptr + make(const Config& config, const std::shared_ptr& logger) + { + metrics::NullStatsFactory factory; + return make(config.serviceName(), config, logger, factory); + } + static std::shared_ptr make(const std::string& serviceName, const Config& config, @@ -79,6 +93,14 @@ class Tracer : public opentracing::Tracer, return make(serviceName, config, logger, statsFactory, 0); } + static std::shared_ptr + make(const Config& config, + const std::shared_ptr& logger, + metrics::StatsFactory& statsFactory) + { + return make(config.serviceName(), config, logger, statsFactory, 0); + } + static std::shared_ptr make(const std::string& serviceName, const Config& config, @@ -86,6 +108,16 @@ class Tracer : public opentracing::Tracer, metrics::StatsFactory& statsFactory, int options); + static std::shared_ptr + make(const Config& config, + const std::shared_ptr& logger, + metrics::StatsFactory& statsFactory, + int options) + { + return make( + config.serviceName(), config, logger, statsFactory, options); + } + ~Tracer() { Close(); } std::unique_ptr @@ -202,6 +234,7 @@ class Tracer : public opentracing::Tracer, const std::shared_ptr& logger, const std::shared_ptr& metrics, const propagation::HeadersConfig& headersConfig, + const std::vector& tags, int options) : _serviceName(serviceName) , _hostIPv4(net::IPAddress::localIP(AF_INET)) @@ -233,6 +266,8 @@ class Tracer : public opentracing::Tracer, _tags.push_back(Tag(kTracerIPTagKey, _hostIPv4.host())); } + std::copy(tags.cbegin(), tags.cend(), std::back_inserter(_tags)); + std::random_device device; _randomNumberGenerator.seed(device()); } diff --git a/src/jaegertracing/TracerFactory.cpp b/src/jaegertracing/TracerFactory.cpp index e5299501..d743d7eb 100644 --- a/src/jaegertracing/TracerFactory.cpp +++ b/src/jaegertracing/TracerFactory.cpp @@ -49,10 +49,9 @@ TracerFactory::MakeTracer(const char* configuration, return opentracing::make_unexpected( opentracing::invalid_configuration_error); } - std::string serviceName = serviceNameNode.Scalar(); const auto tracerConfig = jaegertracing::Config::parse(yaml); - return jaegertracing::Tracer::make(serviceName, tracerConfig); + return jaegertracing::Tracer::make(tracerConfig); #endif // JAEGERTRACING_WITH_YAML_CPP } catch (const std::bad_alloc&) { return opentracing::make_unexpected( diff --git a/src/jaegertracing/TracerTest.cpp b/src/jaegertracing/TracerTest.cpp index 60dd10c4..1526eefc 100644 --- a/src/jaegertracing/TracerTest.cpp +++ b/src/jaegertracing/TracerTest.cpp @@ -416,4 +416,36 @@ TEST(Tracer, testPropagation) tracer->Close(); } +TEST(Tracer, testTracerTags) +{ + std::vector tags; + tags.emplace_back("hostname", std::string("foobar")); + tags.emplace_back("my.app.version", std::string("1.2.3")); + + Config config( + false, + samplers::Config( + "const", 1, "", 0, samplers::Config::Clock::duration()), + reporters::Config(0, std::chrono::milliseconds(100), false, "", ""), + propagation::HeadersConfig(), + baggage::RestrictionsConfig(), + "test-service", + tags); + + auto tracer = Tracer::make(config, logging::nullLogger()); + const auto jaegerTracer = std::static_pointer_cast(tracer); + + ASSERT_TRUE(std::find(jaegerTracer->tags().begin(), + jaegerTracer->tags().end(), + Tag("hostname", std::string("foobar"))) != + jaegerTracer->tags().end()); + + ASSERT_TRUE(std::find(jaegerTracer->tags().begin(), + jaegerTracer->tags().end(), + Tag("my.app.version", std::string("1.2.3"))) != + jaegerTracer->tags().end()); + + ASSERT_EQ(std::string("test-service"), jaegerTracer->serviceName()); +} + } // namespace jaegertracing diff --git a/src/jaegertracing/net/IPAddress.h b/src/jaegertracing/net/IPAddress.h index 1095c933..46e15e89 100644 --- a/src/jaegertracing/net/IPAddress.h +++ b/src/jaegertracing/net/IPAddress.h @@ -53,7 +53,7 @@ namespace net { class IPAddress { public: - static IPAddress v4(const std::string& hostPort) + static std::pair parse(const std::string& hostPort) { const auto colonPos = hostPort.find(':'); const auto ip = hostPort.substr(0, colonPos); @@ -65,7 +65,13 @@ class IPAddress { port = 0; } } - return v4(ip, port); + return std::make_pair(ip, port); + } + + static IPAddress v4(const std::string& hostPort) + { + auto result = parse(hostPort); + return v4(result.first, result.second); } static IPAddress v4(const std::string& ip, int port) diff --git a/src/jaegertracing/reporters/Config.cpp b/src/jaegertracing/reporters/Config.cpp index e2de49dd..c099574a 100644 --- a/src/jaegertracing/reporters/Config.cpp +++ b/src/jaegertracing/reporters/Config.cpp @@ -14,11 +14,14 @@ * limitations under the License. */ +#include + #include "jaegertracing/reporters/Config.h" #include "jaegertracing/ThriftSender.h" #include "jaegertracing/reporters/CompositeReporter.h" #include "jaegertracing/reporters/LoggingReporter.h" #include "jaegertracing/reporters/RemoteReporter.h" +#include "jaegertracing/utils/EnvVariable.h" namespace jaegertracing { namespace reporters { @@ -26,6 +29,13 @@ namespace reporters { constexpr int Config::kDefaultQueueSize; constexpr const char* Config::kDefaultLocalAgentHostPort; constexpr const char* Config::kDefaultEndpoint; +constexpr const char* Config::kJAEGER_AGENT_HOST_ENV_PROP; +constexpr const char* Config::kJAEGER_AGENT_PORT_ENV_PROP; +constexpr const char* Config::kJAEGER_ENDPOINT_ENV_PROP; + +constexpr const char* Config::kJAEGER_REPORTER_LOG_SPANS_ENV_PROP; +constexpr const char* Config::kJAEGER_REPORTER_FLUSH_INTERVAL_ENV_PROP; +constexpr const char* Config::kJAEGER_REPORTER_MAX_QUEUE_SIZE_ENV_PROP; std::unique_ptr Config::makeReporter(const std::string& serviceName, logging::Logger& logger, @@ -51,5 +61,58 @@ std::unique_ptr Config::makeReporter(const std::string& serviceName, return std::unique_ptr(std::move(remoteReporter)); } +void Config::fromEnv() +{ + const auto agentHost = + utils::EnvVariable::getStringVariable(kJAEGER_AGENT_HOST_ENV_PROP); + if (!agentHost.empty()) { + auto agentHostPort = net::IPAddress::parse(_localAgentHostPort); + std::ostringstream oss; + oss << agentHost << ":" << agentHostPort.second; + _localAgentHostPort = oss.str(); + } + const auto agentPort = + utils::EnvVariable::getStringVariable(kJAEGER_AGENT_PORT_ENV_PROP); + if (!agentPort.empty()) { + std::istringstream iss(agentPort); + int port = 0; + if (iss >> port) { + auto agentHostPort = net::IPAddress::parse(_localAgentHostPort); + std::ostringstream oss; + oss << agentHostPort.first << ":" << port; + _localAgentHostPort = oss.str(); + } + } + + const auto endpoint = + utils::EnvVariable::getStringVariable(kJAEGER_ENDPOINT_ENV_PROP); + if (!endpoint.empty()) { + _endpoint = endpoint; + } + + const auto logSpan = utils::EnvVariable::getBoolVariable( + kJAEGER_REPORTER_LOG_SPANS_ENV_PROP); + if (logSpan.first) { + _logSpans = logSpan.second; + } + + const auto flushInterval = utils::EnvVariable::getIntVariable( + kJAEGER_REPORTER_FLUSH_INTERVAL_ENV_PROP); + if (!flushInterval.first) { + if (flushInterval.second > 0) { + _bufferFlushInterval = + std::chrono::milliseconds(flushInterval.second); + } + } + + const auto maxQueueSize = utils::EnvVariable::getIntVariable( + kJAEGER_REPORTER_MAX_QUEUE_SIZE_ENV_PROP); + if (!maxQueueSize.first) { + if (maxQueueSize.second > 0) { + _queueSize = maxQueueSize.second; + } + } +} + } // namespace reporters } // namespace jaegertracing diff --git a/src/jaegertracing/reporters/Config.h b/src/jaegertracing/reporters/Config.h index 57004195..8f6904d2 100644 --- a/src/jaegertracing/reporters/Config.h +++ b/src/jaegertracing/reporters/Config.h @@ -40,6 +40,16 @@ class Config { static constexpr auto kDefaultLocalAgentHostPort = "127.0.0.1:6831"; static constexpr auto kDefaultEndpoint = ""; + static constexpr auto kJAEGER_AGENT_HOST_ENV_PROP = "JAEGER_AGENT_HOST"; + static constexpr auto kJAEGER_AGENT_PORT_ENV_PROP = "JAEGER_AGENT_PORT"; + static constexpr auto kJAEGER_ENDPOINT_ENV_PROP = "JAEGER_ENDPOINT"; + + static constexpr auto kJAEGER_REPORTER_LOG_SPANS_ENV_PROP = "JAEGER_REPORTER_LOG_SPANS"; + static constexpr auto kJAEGER_REPORTER_FLUSH_INTERVAL_ENV_PROP = "JAEGER_REPORTER_FLUSH_INTERVAL"; + static constexpr auto kJAEGER_REPORTER_MAX_QUEUE_SIZE_ENV_PROP = "JAEGER_REPORTER_MAX_QUEUE_SIZE"; + + + static Clock::duration defaultBufferFlushInterval() { return std::chrono::seconds(10); @@ -111,6 +121,8 @@ class Config { return _endpoint; } + void fromEnv(); + private: int _queueSize; Clock::duration _bufferFlushInterval; diff --git a/src/jaegertracing/samplers/Config.cpp b/src/jaegertracing/samplers/Config.cpp index 3597c025..23531fbb 100644 --- a/src/jaegertracing/samplers/Config.cpp +++ b/src/jaegertracing/samplers/Config.cpp @@ -15,6 +15,7 @@ */ #include "jaegertracing/samplers/Config.h" +#include "jaegertracing/utils/EnvVariable.h" namespace jaegertracing { namespace samplers { @@ -22,5 +23,31 @@ namespace samplers { constexpr double Config::kDefaultSamplingProbability; constexpr const char* Config::kDefaultSamplingServerURL; +constexpr const char* Config::kJAEGER_SAMPLER_TYPE_ENV_PROP; +constexpr const char* Config::kJAEGER_SAMPLER_PARAM_ENV_PROP; +constexpr const char* Config::kJAEGER_SAMPLER_MANAGER_HOST_PORT_ENV_PROP; + +void Config::fromEnv() +{ + const auto samplerType = utils::EnvVariable::getStringVariable(kJAEGER_SAMPLER_TYPE_ENV_PROP); + if (!samplerType.empty()) { + _type = samplerType; + } + + const auto param = utils::EnvVariable::getStringVariable(kJAEGER_SAMPLER_PARAM_ENV_PROP); + if (!param.empty()) { + std::istringstream iss(param); + int paramVal = 0; + if (iss >> paramVal) { + _param = paramVal; + } + } + + const auto samplerUrl = utils::EnvVariable::getStringVariable(kJAEGER_SAMPLER_MANAGER_HOST_PORT_ENV_PROP); + if (!samplerUrl.empty()) { + _samplingServerURL = "http://" + samplerUrl + "/sampling"; + } +} + } // namespace samplers } // namespace jaegertracing diff --git a/src/jaegertracing/samplers/Config.h b/src/jaegertracing/samplers/Config.h index f349cbce..58df7f99 100644 --- a/src/jaegertracing/samplers/Config.h +++ b/src/jaegertracing/samplers/Config.h @@ -45,6 +45,10 @@ class Config { static constexpr auto kDefaultSamplingServerURL = "http://127.0.0.1:5778/sampling"; static constexpr auto kDefaultMaxOperations = 2000; + static constexpr auto kJAEGER_SAMPLER_TYPE_ENV_PROP = "JAEGER_SAMPLER_TYPE"; + static constexpr auto kJAEGER_SAMPLER_PARAM_ENV_PROP = "JAEGER_SAMPLER_PARAM"; + static constexpr auto kJAEGER_SAMPLER_MANAGER_HOST_PORT_ENV_PROP = "JAEGER_SAMPLER_MANAGER_HOST_PORT"; + static Clock::duration defaultSamplingRefreshInterval() { return std::chrono::minutes(1); @@ -170,6 +174,8 @@ class Config { return _samplingRefreshInterval; } + void fromEnv(); + private: std::string _type; double _param; diff --git a/src/jaegertracing/utils/EnvVariable.cpp b/src/jaegertracing/utils/EnvVariable.cpp new file mode 100644 index 00000000..69459bc9 --- /dev/null +++ b/src/jaegertracing/utils/EnvVariable.cpp @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2019 Uber Technologies, Inc. + * + * 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/utils/EnvVariable.h" diff --git a/src/jaegertracing/utils/EnvVariable.h b/src/jaegertracing/utils/EnvVariable.h new file mode 100644 index 00000000..f580bef9 --- /dev/null +++ b/src/jaegertracing/utils/EnvVariable.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2019 Uber Technologies, Inc. + * + * 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_UTILS_ENV_VARIABLE_H +#define JAEGERTRACING_UTILS_ENV_VARIABLE_H + +#include +#include +#include + +namespace jaegertracing { +namespace utils { +namespace EnvVariable { + +inline std::string getStringVariable(const char* envVar) +{ + const auto rawVariable = std::getenv(envVar); + return std::string(rawVariable ? rawVariable : ""); +} + +inline std::pair getIntVariable(const char* envVar) +{ + const auto rawVariable = std::getenv(envVar); + const std::string variable(rawVariable ? rawVariable : ""); + if (!variable.empty()) { + std::istringstream iss(variable); + int intVal = 0; + if (iss >> intVal) { + return std::make_pair(false, intVal); + } + } + return std::make_pair(false, 0); +} + +inline std::pair getBoolVariable(const char* envVar) +{ + const auto rawVariable = std::getenv(envVar); + std::string variable(rawVariable ? rawVariable : ""); + + if (!variable.empty()) { + std::transform( + variable.begin(), variable.end(), variable.begin(), ::tolower); + return std::make_pair(true, (variable == "true")); + } + return std::make_pair(false, false); +} + +} // namespace EnvVariable +} // namespace utils +} // namespace jaegertracing + +#endif // JAEGERTRACING_UTILS_HEXPARSING_H