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

Commit

Permalink
Extracted Jager porpagation and W3C propgation in their own classes
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Stadler <[email protected]>
  • Loading branch information
tobiasstadler committed Dec 6, 2020
1 parent d5ac706 commit 1f3ed2a
Show file tree
Hide file tree
Showing 23 changed files with 701 additions and 490 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
12 changes: 11 additions & 1 deletion 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 Expand Up @@ -62,7 +73,6 @@ void Config::fromEnv()
}
_reporter.fromEnv();
_sampler.fromEnv();
_headers.fromEnv();
}

} // namespace jaegertracing
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: 4 additions & 13 deletions src/jaegertracing/ConfigTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,14 @@ TEST(Config, testZeroSamplingParam)
}
}

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

Expand Down Expand Up @@ -197,12 +193,7 @@ TEST(Config, testFromEnv)
testutils::EnvVariable::setEnv("JAEGER_PROPAGATION", "w3c");

config.fromEnv();
ASSERT_EQ(kW3CTraceContextHeaderName,
config.headers().traceContextHeaderName());
ASSERT_EQ(kW3CTraceStateHeaderName,
config.headers().traceStateHeaderName());
ASSERT_EQ(propagation::Format::W3C,
config.headers().traceContextHeaderFormat());
ASSERT_EQ(propagation::Format::W3C, config.propagationFormat());

testutils::EnvVariable::setEnv("JAEGER_AGENT_HOST", "");
testutils::EnvVariable::setEnv("JAEGER_AGENT_PORT", "");
Expand Down
2 changes: 1 addition & 1 deletion src/jaegertracing/Constants.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ 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 kW3CTraceContextHeaderName = "traceparent";
static constexpr auto kW3CTraceParentHeaderName = "traceparent";
static constexpr auto kW3CTraceStateHeaderName = "tracestate";
static constexpr auto kSamplerTypeConst = "const";
static constexpr auto kSamplerTypeRemote = "remote";
Expand Down
66 changes: 1 addition & 65 deletions src/jaegertracing/SpanContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace jaegertracing {

SpanContext SpanContext::parseJaegerFormat(std::istream& in)
SpanContext SpanContext::fromStream(std::istream& in)
{
SpanContext spanContext;
spanContext._traceID = TraceID::fromStream(in);
Expand Down Expand Up @@ -65,68 +65,4 @@ SpanContext SpanContext::parseJaegerFormat(std::istream& in)
return spanContext;
}

SpanContext SpanContext::parseW3CFormat(std::istream& in)
{
char ch = '\0';
if (!in.get(ch) || ch != '0') {
return SpanContext();
}

if (!in.get(ch) || ch != '0') {
return SpanContext();
}

if (!in.get(ch) || ch != '-') {
return SpanContext();
}

SpanContext spanContext;
spanContext._traceID = TraceID::fromStream(in, PropagationFormat::W3C);
if (!spanContext._traceID.isValid()) {
return SpanContext();
}

if (!in.get(ch) || ch != '-') {
return SpanContext();
}

constexpr auto kMaxUInt64Chars = static_cast<size_t>(16);
auto buffer = utils::HexParsing::readSegment(in, kMaxUInt64Chars, '-');
if (buffer.empty()) {
return SpanContext();
}
spanContext._spanID = utils::HexParsing::decodeHex<uint64_t>(buffer);

if (!in.get(ch) || ch != '-') {
return SpanContext();
}

constexpr auto kMaxByteChars = static_cast<size_t>(2);
buffer = utils::HexParsing::readSegment(in, kMaxByteChars, '-');
if (buffer.empty() || buffer.size() != 2) {
return SpanContext();
}
spanContext._flags = utils::HexParsing::decodeHex<unsigned char>(buffer);

in.clear();
return spanContext;
}

SpanContext SpanContext::fromStream(std::istream& in, PropagationFormat format)
{
switch (format) {
case PropagationFormat::JAEGER:
return parseJaegerFormat(in);
case PropagationFormat::W3C:
return parseW3CFormat(in);
}

return SpanContext();
}

SpanContext SpanContext::fromStream(std::istream& in)
{
return fromStream(in, PropagationFormat::JAEGER);
}

} // namespace jaegertracing
37 changes: 6 additions & 31 deletions src/jaegertracing/SpanContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,17 @@
#include <opentracing/span.h>

#include "jaegertracing/Compilers.h"

#include "jaegertracing/TraceID.h"
#include "jaegertracing/propagation/Format.h"

namespace jaegertracing {

class SpanContext : public opentracing::SpanContext {
public:
using StrMap = std::unordered_map<std::string, std::string>;
using PropagationFormat = propagation::Format;

enum class Flag : unsigned char { kSampled = 1, kDebug = 2 };

static SpanContext fromStream(std::istream& in, PropagationFormat format);
static SpanContext fromStream(std::istream& in);

SpanContext()
Expand Down Expand Up @@ -146,13 +144,13 @@ class SpanContext : public opentracing::SpanContext {

const std::string& debugID() const { return _debugID; }

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

bool isSampled() const
{
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 All @@ -165,32 +163,12 @@ class SpanContext : public opentracing::SpanContext {

bool isValid() const { return _traceID.isValid() && _spanID != 0; }

template <typename Stream>
void print(Stream& out, PropagationFormat format) const
{
switch (format) {
case PropagationFormat::JAEGER:
_traceID.print(out, PropagationFormat::JAEGER);
out << ':' << std::hex << _spanID << ':' << std::hex << _parentID
<< ':' << std::hex << static_cast<size_t>(_flags);
break;
case PropagationFormat::W3C:
out << "00";
out << '-';
_traceID.print(out, PropagationFormat::W3C);
out << '-';
out << std::setw(16) << std::setfill('0') << std::hex << _spanID;
out << '-';
out << std::setw(2) << std::setfill('0') << std::hex
<< static_cast<size_t>(_flags);
break;
}
}

template <typename Stream>
void print(Stream& out) const
{
print(out, PropagationFormat::JAEGER);
_traceID.print(out);
out << ':' << std::hex << _spanID << ':' << std::hex << _parentID << ':'
<< std::hex << static_cast<size_t>(_flags);
}

void ForeachBaggageItem(
Expand Down Expand Up @@ -229,9 +207,6 @@ class SpanContext : public opentracing::SpanContext {
}

private:
static SpanContext parseJaegerFormat(std::istream& in);
static SpanContext parseW3CFormat(std::istream& in);

TraceID _traceID;
uint64_t _spanID;
uint64_t _parentID;
Expand Down
Loading

0 comments on commit 1f3ed2a

Please sign in to comment.