Skip to content

Opentracing 1.6 API support #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion ci/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "io_opentracing_cpp",
remote = "https://github.com/opentracing/opentracing-cpp",
commit = "ac50154a7713877f877981c33c3375003b6ebfe1",
commit = "597b0fa9507c854877b3efa9f4f20293a9e1ed30",
)

new_local_repository(
Expand Down
2 changes: 1 addition & 1 deletion ci/build_plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ apt-get install --no-install-recommends --no-install-suggests -y \
git \
ca-certificates

export OPENTRACING_VERSION=1.5.0
export OPENTRACING_VERSION=1.6.0

# Compile for a portable cpu architecture
export CFLAGS="-march=x86-64"
Expand Down
2 changes: 1 addition & 1 deletion ci/install_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ apt-get install --no-install-recommends --no-install-suggests -y \

# Build OpenTracing
cd /
export OPENTRACING_VERSION=1.5.0
export OPENTRACING_VERSION=1.6.0
git clone -b v$OPENTRACING_VERSION https://github.com/opentracing/opentracing-cpp.git
cd opentracing-cpp
mkdir .build && cd .build
Expand Down
10 changes: 10 additions & 0 deletions zipkin/include/zipkin/span_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace zipkin {
*/
struct AnnotationSet {
AnnotationSet() : cs_(false), cr_(false), ss_(false), sr_(false) {}
AnnotationSet(const AnnotationSet &set) : cs_{set.cs_}, cr_{set.cr_}, ss_{set.ss_}, sr_{set.sr_} {}
bool cs_ : 1;
bool cr_ : 1;
bool ss_ : 1;
Expand Down Expand Up @@ -58,6 +59,15 @@ class SpanContext {
: trace_id_{trace_id}, id_{id}, parent_id_{parent_id}, flags_{flags},
is_initialized_{true} {}

/**
* Copy constructor that creates a context object from the given SpanContext
* instance.
*/
SpanContext(const SpanContext &span)
: trace_id_{span.trace_id_}, id_{span.id_}, parent_id_{span.parent_id_},
annotation_values_{span.annotation_values_}, flags_{span.flags_},
is_initialized_{span.is_initialized_} {}

bool isSampled() const { return flags_ & zipkin::sampled_flag; }

/**
Expand Down
25 changes: 25 additions & 0 deletions zipkin_opentracing/src/opentracing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class OtSpanContext : public ot::SpanContext {
public:
OtSpanContext() = default;

OtSpanContext(const OtSpanContext &span)
: span_context_{span.span_context_}, baggage_{span.baggage_} {}

explicit OtSpanContext(zipkin::SpanContext &&span_context)
: span_context_{std::move(span_context)} {}

Expand All @@ -69,6 +72,20 @@ class OtSpanContext : public ot::SpanContext {
return *this;
}

std::unique_ptr<SpanContext> Clone() const noexcept override try {
return std::unique_ptr<SpanContext> (new OtSpanContext(*this));
} catch (...) {
return nullptr;
}

std::string ToTraceID() const noexcept override {
return span_context_.traceIdAsHexString();
}

std::string ToSpanID() const noexcept override {
return span_context_.idAsHexString();
}

void ForeachBaggageItem(
std::function<bool(const std::string &, const std::string &)> f)
const override {
Expand Down Expand Up @@ -258,6 +275,14 @@ class OtSpan : public ot::Span {
void Log(std::initializer_list<std::pair<string_view, Value>>
fields) noexcept override {}

void Log(
SystemTime timestamp,
std::initializer_list<std::pair<string_view, Value>> fields) noexcept override {}

void Log(
SystemTime timestamp,
const std::vector<std::pair<string_view, Value>>& fields) noexcept override {}

const ot::SpanContext &context() const noexcept override {
return span_context_;
}
Expand Down
68 changes: 68 additions & 0 deletions zipkin_opentracing/test/ot_tracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ static bool IsChildOf(const zipkin::Span &a, const zipkin::Span &b) {
a.traceId() == b.traceId();
}

class TextMapCarrier : public ot::TextMapWriter {
public:
TextMapCarrier(std::unordered_map<std::string, std::string> &text_map)
: text_map_(text_map) {}

ot::expected<void> Set(ot::string_view key, ot::string_view value) const override {
text_map_[key] = value;
return {};
}

private:
std::unordered_map<std::string, std::string> &text_map_;
};

TEST_CASE("ot_tracer") {
auto reporter = new InMemoryReporter();
ZipkinOtTracerOptions options;
Expand Down Expand Up @@ -167,4 +181,58 @@ TEST_CASE("ot_tracer") {
span->Finish();
CHECK(hasTag(reporter->top(), "abc", 123));
}

SECTION("Get SpanContext and check that it is valid after Finish") {
auto span = tracer->StartSpan("a");
CHECK(span);
span->SetTag("abc", 123);
auto &ctx = span->context();
span->Finish();
auto trace_id = ctx.ToTraceID();
CHECK(trace_id != "");
auto span_id = ctx.ToSpanID();
CHECK(span_id != "");
}

SECTION("Check SpanContext.Clone() preserves attributes") {
auto span = tracer->StartSpan("a");
CHECK(span);
span->SetTag("abc", 123);
span->SetBaggageItem("a", "1");
auto &ctx = span->context();
span->Finish();
auto ctx2 = ctx.Clone();

CHECK(ctx.ToTraceID() == ctx2->ToTraceID());
CHECK(ctx.ToSpanID() == ctx2->ToSpanID());

std::unordered_map<std::string, std::string> items;
ctx.ForeachBaggageItem(
[&items] (const std::string& key, const std::string& value) {
items[key] = value;
return true;
}
);

std::unordered_map<std::string, std::string> items2;
ctx2->ForeachBaggageItem(
[&items2] (const std::string& key, const std::string& value) {
items2[key] = value;
return true;
}
);
CHECK(items == items2);

// Cross-check: serialize span context to a carrier
// and compare low-level representation
items.clear();
items2.clear();

TextMapCarrier carrier{items};
tracer->Inject(ctx, carrier);
TextMapCarrier carrier2{items2};
tracer->Inject(*ctx2, carrier2);

CHECK(items == items2);
}
}