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

Commit

Permalink
Merge 96625d4 into 5748442
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasstadler authored Dec 6, 2020
2 parents 5748442 + 96625d4 commit 6cd4725
Show file tree
Hide file tree
Showing 20 changed files with 893 additions and 206 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ set(SRC
src/jaegertracing/propagation/HeadersConfig.cpp
src/jaegertracing/propagation/Injector.cpp
src/jaegertracing/propagation/Propagator.cpp
src/jaegertracing/propagation/JaegerPropagator.cpp
src/jaegertracing/propagation/W3CPropagator.cpp
src/jaegertracing/reporters/CompositeReporter.cpp
src/jaegertracing/reporters/Config.cpp
src/jaegertracing/reporters/InMemoryReporter.cpp
Expand Down Expand Up @@ -332,6 +334,7 @@ if(BUILD_TESTING)
src/jaegertracing/net/http/MethodTest.cpp
src/jaegertracing/net/http/ResponseTest.cpp
src/jaegertracing/propagation/PropagatorTest.cpp
src/jaegertracing/propagation/W3CPropagatorTest.cpp
src/jaegertracing/reporters/ConfigTest.cpp
src/jaegertracing/reporters/ReporterTest.cpp
src/jaegertracing/samplers/SamplerTest.cpp
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ JAEGER_DISABLED _(not recommended)_ | Instructs the Configuration to return a no
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_PROPAGATION | The propagation format used by the tracer. Supported values are jaeger and w3c
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)
Expand Down
11 changes: 11 additions & 0 deletions src/jaegertracing/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ void Config::fromEnv()
_disabled = disabled.second;
}

const auto propagationFormat =
utils::EnvVariable::getStringVariable(kJAEGER_PROPAGATION_ENV_PROP);
if(!propagationFormat.empty()) {
if (propagationFormat == "w3c") {
_propagationFormat = propagation::Format::W3C;
}
else {
_propagationFormat = propagation::Format::JAEGER;
}
}

const auto serviceName =
utils::EnvVariable::getStringVariable(kJAEGER_SERVICE_NAME_ENV_PROP);
if (!serviceName.empty()) {
Expand Down
23 changes: 20 additions & 3 deletions src/jaegertracing/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "jaegertracing/Tag.h"
#include "jaegertracing/baggage/RestrictionsConfig.h"
#include "jaegertracing/propagation/HeadersConfig.h"
#include "jaegertracing/propagation/Format.h"
#include "jaegertracing/reporters/Config.h"
#include "jaegertracing/samplers/Config.h"
#include "jaegertracing/utils/YAML.h"
Expand All @@ -34,6 +35,7 @@ class Config {
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";
static constexpr auto kJAEGER_PROPAGATION_ENV_PROP = "JAEGER_PROPAGATION";

#ifdef JAEGERTRACING_WITH_YAML_CPP

Expand All @@ -48,6 +50,8 @@ class Config {

const auto disabled =
utils::yaml::findOrDefault<bool>(configYAML, "disabled", false);
const auto propagationFormat = utils::yaml::findOrDefault<std::string>(
configYAML, "propagation_format", "jaeger");
const auto samplerNode = configYAML["sampler"];
const auto sampler = samplers::Config::parse(samplerNode);
const auto reporterNode = configYAML["reporter"];
Expand All @@ -57,8 +61,15 @@ class Config {
const auto baggageRestrictionsNode = configYAML["baggage_restrictions"];
const auto baggageRestrictions =
baggage::RestrictionsConfig::parse(baggageRestrictionsNode);
return Config(
disabled, sampler, reporter, headers, baggageRestrictions, serviceName);
return Config(disabled,
sampler,
reporter,
headers,
baggageRestrictions,
serviceName,
std::vector<Tag>(),
propagationFormat == "w3c" ? propagation::Format::W3C
: propagation::Format::JAEGER);
}

#endif // JAEGERTRACING_WITH_YAML_CPP
Expand All @@ -71,8 +82,11 @@ class Config {
const baggage::RestrictionsConfig& baggageRestrictions =
baggage::RestrictionsConfig(),
const std::string& serviceName = "",
const std::vector<Tag>& tags = std::vector<Tag>())
const std::vector<Tag>& tags = std::vector<Tag>(),
const propagation::Format propagationFormat =
propagation::Format::JAEGER)
: _disabled(disabled)
, _propagationFormat(propagationFormat)
, _serviceName(serviceName)
, _tags(tags)
, _sampler(sampler)
Expand All @@ -84,6 +98,8 @@ class Config {

bool disabled() const { return _disabled; }

propagation::Format propagationFormat() const { return _propagationFormat; }

const samplers::Config& sampler() const { return _sampler; }

const reporters::Config& reporter() const { return _reporter; }
Expand All @@ -103,6 +119,7 @@ class Config {

private:
bool _disabled;
propagation::Format _propagationFormat;
std::string _serviceName;
std::vector< Tag > _tags;
samplers::Config _sampler;
Expand Down
17 changes: 17 additions & 0 deletions src/jaegertracing/ConfigTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ TEST(Config, testZeroSamplingParam)
}
}

TEST(Config, testPropagationFormat)
{
{
constexpr auto kConfigYAML = R"cfg(
propagation_format: w3c
)cfg";
const auto config = Config::parse(YAML::Load(kConfigYAML));
ASSERT_EQ(propagation::Format::W3C, config.propagationFormat());
}
}

#endif // JAEGERTRACING_WITH_YAML_CPP

TEST(Config, testFromEnv)
Expand Down Expand Up @@ -179,6 +190,11 @@ TEST(Config, testFromEnv)
ASSERT_EQ(std::string("host33:445"),
config.reporter().localAgentHostPort());

testutils::EnvVariable::setEnv("JAEGER_PROPAGATION", "w3c");

config.fromEnv();
ASSERT_EQ(propagation::Format::W3C, config.propagationFormat());

testutils::EnvVariable::setEnv("JAEGER_AGENT_HOST", "");
testutils::EnvVariable::setEnv("JAEGER_AGENT_PORT", "");
testutils::EnvVariable::setEnv("JAEGER_ENDPOINT", "");
Expand All @@ -190,6 +206,7 @@ TEST(Config, testFromEnv)
testutils::EnvVariable::setEnv("JAEGER_SERVICE_NAME", "");
testutils::EnvVariable::setEnv("JAEGER_TAGS", "");
testutils::EnvVariable::setEnv("JAEGER_DISABLED", "");
testutils::EnvVariable::setEnv("JAEGER_PROPAGATION", "");
}

} // namespace jaegertracing
2 changes: 2 additions & 0 deletions src/jaegertracing/Constants.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ static constexpr auto kSamplerParamTagKey = "sampler.param";
static constexpr auto kTraceContextHeaderName = "uber-trace-id";
static constexpr auto kTracerStateHeaderName = kTraceContextHeaderName;
static constexpr auto kTraceBaggageHeaderPrefix = "uberctx-";
static constexpr auto kW3CTraceParentHeaderName = "traceparent";
static constexpr auto kW3CTraceStateHeaderName = "tracestate";
static constexpr auto kSamplerTypeConst = "const";
static constexpr auto kSamplerTypeRemote = "remote";
static constexpr auto kSamplerTypeProbabilistic = "probabilistic";
Expand Down
21 changes: 17 additions & 4 deletions src/jaegertracing/SpanContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ class SpanContext : public opentracing::SpanContext {
uint64_t parentID,
unsigned char flags,
const StrMap& baggage,
const std::string& debugID = "")
const std::string& debugID = "",
const std::string& traceState = "")
: _traceID(traceID)
, _spanID(spanID)
, _parentID(parentID)
, _flags(flags)
, _baggage(baggage)
, _debugID(debugID)
, _traceState(traceState)
, _mutex()
{
}
Expand All @@ -72,6 +74,7 @@ class SpanContext : public opentracing::SpanContext {
, _flags(ctx._flags)
, _baggage(ctx._baggage)
, _debugID(ctx._debugID)
, _traceState(ctx._traceState)
{
}

Expand All @@ -90,6 +93,7 @@ class SpanContext : public opentracing::SpanContext {
swap(_flags, ctx._flags);
swap(_baggage, ctx._baggage);
swap(_debugID, ctx._debugID);
swap(_traceState, ctx._traceState);
}

friend void swap(SpanContext& lhs, SpanContext& rhs) { lhs.swap(rhs); }
Expand All @@ -105,8 +109,13 @@ class SpanContext : public opentracing::SpanContext {
SpanContext withBaggage(const StrMap& baggage) const
{
std::lock_guard<std::mutex> lock(_mutex);
return SpanContext(
_traceID, _spanID, _parentID, _flags, baggage, _debugID);
return SpanContext(_traceID,
_spanID,
_parentID,
_flags,
baggage,
_debugID,
_traceState);
}

template <typename Function>
Expand Down Expand Up @@ -140,6 +149,8 @@ class SpanContext : public opentracing::SpanContext {
return _flags & static_cast<unsigned char>(Flag::kSampled);
}

const std::string& traceState() const { return _traceState; }

bool isDebug() const
{
return _flags & static_cast<unsigned char>(Flag::kDebug);
Expand Down Expand Up @@ -186,7 +197,8 @@ class SpanContext : public opentracing::SpanContext {
}
return lhs._traceID == rhs._traceID && lhs._spanID == rhs._spanID &&
lhs._parentID == rhs._parentID && lhs._flags == rhs._flags &&
lhs._debugID == rhs._debugID;
lhs._debugID == rhs._debugID &&
lhs._traceState == rhs._traceState;
}

friend bool operator!=(const SpanContext& lhs, const SpanContext& rhs)
Expand All @@ -201,6 +213,7 @@ class SpanContext : public opentracing::SpanContext {
unsigned char _flags;
StrMap _baggage;
std::string _debugID;
std::string _traceState;
mutable std::mutex _mutex; // Protects _baggage.
};

Expand Down
21 changes: 20 additions & 1 deletion src/jaegertracing/Tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "jaegertracing/Tracer.h"
#include "jaegertracing/Reference.h"
#include "jaegertracing/TraceID.h"
#include "jaegertracing/propagation/JaegerPropagator.h"
#include "jaegertracing/propagation/W3CPropagator.h"
#include "jaegertracing/samplers/SamplingStatus.h"
#include <algorithm>
#include <cassert>
Expand Down Expand Up @@ -258,6 +260,22 @@ Tracer::make(const std::string& serviceName,
}

auto metrics = std::make_shared<metrics::Metrics>(statsFactory);

TextMapPropagator* textPropagator;
HTTPHeaderPropagator* httpHeaderPropagator;
if (config.propagationFormat() == propagation::Format::W3C) {
textPropagator =
new propagation::W3CTextMapPropagator(config.headers(), metrics);
httpHeaderPropagator =
new propagation::W3CHTTPHeaderPropagator(config.headers(), metrics);
}
else {
textPropagator =
new propagation::JaegerTextMapPropagator(config.headers(), metrics);
httpHeaderPropagator = new propagation::JaegerHTTPHeaderPropagator(
config.headers(), metrics);
}

std::shared_ptr<samplers::Sampler> sampler(
config.sampler().makeSampler(serviceName, *logger, *metrics));
std::shared_ptr<reporters::Reporter> reporter(
Expand All @@ -267,7 +285,8 @@ Tracer::make(const std::string& serviceName,
reporter,
logger,
metrics,
config.headers(),
textPropagator,
httpHeaderPropagator,
config.tags(),
options));
}
Expand Down
26 changes: 17 additions & 9 deletions src/jaegertracing/Tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Tracer : public opentracing::Tracer,
return opentracing::make_expected_from_error<void>(
opentracing::invalid_span_context_error);
}
_textPropagator.inject(*jaegerCtx, writer);
_textPropagator->inject(*jaegerCtx, writer);
return opentracing::make_expected();
}

Expand All @@ -132,7 +132,7 @@ class Tracer : public opentracing::Tracer,
return opentracing::make_expected_from_error<void>(
opentracing::invalid_span_context_error);
}
_httpHeaderPropagator.inject(*jaegerCtx, writer);
_httpHeaderPropagator->inject(*jaegerCtx, writer);
return opentracing::make_expected();
}

Expand All @@ -150,7 +150,7 @@ class Tracer : public opentracing::Tracer,
opentracing::expected<std::unique_ptr<opentracing::SpanContext>>
Extract(const opentracing::TextMapReader& reader) const override
{
const auto spanContext = _textPropagator.extract(reader);
const auto spanContext = _textPropagator->extract(reader);
if (spanContext == SpanContext()) {
return std::unique_ptr<opentracing::SpanContext>();
}
Expand All @@ -161,7 +161,7 @@ class Tracer : public opentracing::Tracer,
opentracing::expected<std::unique_ptr<opentracing::SpanContext>>
Extract(const opentracing::HTTPHeadersReader& reader) const override
{
const auto spanContext = _httpHeaderPropagator.extract(reader);
const auto spanContext = _httpHeaderPropagator->extract(reader);
if (spanContext == SpanContext()) {
return std::unique_ptr<opentracing::SpanContext>();
}
Expand Down Expand Up @@ -201,12 +201,20 @@ class Tracer : public opentracing::Tracer,
}

private:
using TextMapPropagator =
propagation::Propagator<const opentracing::TextMapReader&,
const opentracing::TextMapWriter&>;
using HTTPHeaderPropagator =
propagation::Propagator<const opentracing::HTTPHeadersReader&,
const opentracing::HTTPHeadersWriter&>;

Tracer(const std::string& serviceName,
const std::shared_ptr<samplers::Sampler>& sampler,
const std::shared_ptr<reporters::Reporter>& reporter,
const std::shared_ptr<logging::Logger>& logger,
const std::shared_ptr<metrics::Metrics>& metrics,
const propagation::HeadersConfig& headersConfig,
const TextMapPropagator* textPropagator,
const HTTPHeaderPropagator* httpHeaderPropagator,
const std::vector<Tag>& tags,
int options)
: _serviceName(serviceName)
Expand All @@ -216,8 +224,8 @@ class Tracer : public opentracing::Tracer,
, _metrics(metrics)
, _logger(logger)
, _randomNumberGenerator()
, _textPropagator(headersConfig, _metrics)
, _httpHeaderPropagator(headersConfig, _metrics)
, _textPropagator(textPropagator)
, _httpHeaderPropagator(httpHeaderPropagator)
, _binaryPropagator(_metrics)
, _tags()
, _restrictionManager(new baggage::DefaultRestrictionManager(0))
Expand Down Expand Up @@ -294,8 +302,8 @@ class Tracer : public opentracing::Tracer,
std::shared_ptr<logging::Logger> _logger;
mutable std::mt19937_64 _randomNumberGenerator;
mutable std::mutex _randomMutex;
propagation::TextMapPropagator _textPropagator;
propagation::HTTPHeaderPropagator _httpHeaderPropagator;
std::unique_ptr<const TextMapPropagator> _textPropagator;
std::unique_ptr<const HTTPHeaderPropagator> _httpHeaderPropagator;
propagation::BinaryPropagator _binaryPropagator;
std::vector<Tag> _tags;
std::unique_ptr<baggage::RestrictionManager> _restrictionManager;
Expand Down
Loading

0 comments on commit 6cd4725

Please sign in to comment.