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

Support Tracer tags and configuration via environment variables #181

Merged
merged 3 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
50 changes: 50 additions & 0 deletions src/jaegertracing/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 23 additions & 2 deletions src/jaegertracing/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -37,6 +43,9 @@ class Config {
return Config();
}

const auto serviceName =
utils::yaml::findOrDefault<std::string>(configYAML, "service_name", "");

const auto disabled =
utils::yaml::findOrDefault<bool>(configYAML, "disabled", false);
const auto samplerNode = configYAML["sampler"];
Expand All @@ -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
Expand All @@ -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<Tag> tags = std::vector<Tag>())
: _disabled(disabled)
, _serviceName(serviceName)
, _tags(tags)
, _sampler(sampler)
, _reporter(reporter)
, _headers(headers)
Expand All @@ -82,8 +95,16 @@ class Config {
return _baggageRestrictions;
}

const std::string& serviceName() const { return _serviceName; }

const std::vector<Tag>& 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;
Expand Down
69 changes: 69 additions & 0 deletions src/jaegertracing/ConfigTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "jaegertracing/utils/YAML.h"
#include <gtest/gtest.h>

#include <cstdlib>

namespace jaegertracing {

#ifdef JAEGERTRACING_WITH_YAML_CPP
Expand Down Expand Up @@ -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");
mdouaihy marked this conversation as resolved.
Show resolved Hide resolved
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();
mdouaihy marked this conversation as resolved.
Show resolved Hide resolved

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<Tag> 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
1 change: 1 addition & 0 deletions src/jaegertracing/Tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ Tracer::make(const std::string& serviceName,
logger,
metrics,
config.headers(),
config.tags(),
options));
}

Expand Down
35 changes: 35 additions & 0 deletions src/jaegertracing/Tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ class Tracer : public opentracing::Tracer,

static constexpr auto kGen128BitOption = 1;

static std::shared_ptr<opentracing::Tracer> make(const Config& config)
{
return make(config.serviceName(),
config,
std::shared_ptr<logging::Logger>(logging::nullLogger()));
}

static std::shared_ptr<opentracing::Tracer>
make(const std::string& serviceName, const Config& config)
{
Expand All @@ -70,6 +77,13 @@ class Tracer : public opentracing::Tracer,
return make(serviceName, config, logger, factory);
}

static std::shared_ptr<opentracing::Tracer>
mdouaihy marked this conversation as resolved.
Show resolved Hide resolved
make(const Config& config, const std::shared_ptr<logging::Logger>& logger)
{
metrics::NullStatsFactory factory;
return make(config.serviceName(), config, logger, factory);
}

static std::shared_ptr<opentracing::Tracer>
make(const std::string& serviceName,
const Config& config,
Expand All @@ -79,13 +93,31 @@ class Tracer : public opentracing::Tracer,
return make(serviceName, config, logger, statsFactory, 0);
}

static std::shared_ptr<opentracing::Tracer>
make(const Config& config,
const std::shared_ptr<logging::Logger>& logger,
metrics::StatsFactory& statsFactory)
{
return make(config.serviceName(), config, logger, statsFactory, 0);
}

static std::shared_ptr<opentracing::Tracer>
make(const std::string& serviceName,
const Config& config,
const std::shared_ptr<logging::Logger>& logger,
metrics::StatsFactory& statsFactory,
int options);

static std::shared_ptr<opentracing::Tracer>
make(const Config& config,
const std::shared_ptr<logging::Logger>& logger,
metrics::StatsFactory& statsFactory,
int options)
{
return make(
config.serviceName(), config, logger, statsFactory, options);
}

~Tracer() { Close(); }

std::unique_ptr<opentracing::Span>
Expand Down Expand Up @@ -202,6 +234,7 @@ class Tracer : public opentracing::Tracer,
const std::shared_ptr<logging::Logger>& logger,
const std::shared_ptr<metrics::Metrics>& metrics,
const propagation::HeadersConfig& headersConfig,
const std::vector<Tag>& tags,
int options)
: _serviceName(serviceName)
, _hostIPv4(net::IPAddress::localIP(AF_INET))
Expand Down Expand Up @@ -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());
}
Expand Down
3 changes: 1 addition & 2 deletions src/jaegertracing/TracerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
32 changes: 32 additions & 0 deletions src/jaegertracing/TracerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,36 @@ TEST(Tracer, testPropagation)
tracer->Close();
}

TEST(Tracer, testTracerTags)
{
std::vector<Tag> 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>(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
10 changes: 8 additions & 2 deletions src/jaegertracing/net/IPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace net {

class IPAddress {
public:
static IPAddress v4(const std::string& hostPort)
static std::pair<std::string, int> parse(const std::string& hostPort)
{
const auto colonPos = hostPort.find(':');
const auto ip = hostPort.substr(0, colonPos);
Expand All @@ -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)
Expand Down
Loading