Skip to content
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

Recover the OT context header from the B3 headers if not propagated #1702

Merged
merged 5 commits into from
Sep 21, 2017
Merged
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
11 changes: 11 additions & 0 deletions source/common/tracing/zipkin/span_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ class SpanContext {
*/
SpanContext() : trace_id_(0), id_(0), parent_id_(0), is_initialized_(false) {}

/**
* Constructor that creates a context object from the supplied trace, span
* and parent ids.
*
* @param trace_id The trace id.
* @param id The span id.
* @param parent_id The parent id.
*/
SpanContext(const uint64_t trace_id, const uint64_t id, const uint64_t parent_id)
: trace_id_(trace_id), id_(id), parent_id_(parent_id), is_initialized_(true) {}

/**
* Constructor that creates a context object from the given Zipkin span object.
*
Expand Down
16 changes: 16 additions & 0 deletions source/common/tracing/zipkin/zipkin_tracer_impl.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "common/tracing/zipkin/zipkin_tracer_impl.h"

#include "common/common/enum_to_int.h"
#include "common/common/utility.h"
#include "common/http/headers.h"
#include "common/http/message_impl.h"
#include "common/http/utility.h"
Expand Down Expand Up @@ -105,6 +106,21 @@ Tracing::SpanPtr Driver::startSpan(const Tracing::Config& config, Http::HeaderMa
// annotation. In this case, we are dealing with an ingress operation. This envoy instance,
// being at the receiving end, will add the SR annotation to the shared span context.

new_zipkin_span =
tracer.startSpan(config, request_headers.Host()->value().c_str(), start_time, context);
} else if (request_headers.XB3TraceId() && request_headers.XB3SpanId()) {
uint64_t trace_id(0);
uint64_t span_id(0);
uint64_t parent_id(0);
if (!StringUtil::atoul(request_headers.XB3TraceId()->value().c_str(), trace_id, 16) ||
!StringUtil::atoul(request_headers.XB3SpanId()->value().c_str(), span_id, 16) ||
(request_headers.XB3ParentSpanId() &&
!StringUtil::atoul(request_headers.XB3ParentSpanId()->value().c_str(), parent_id, 16))) {
return Tracing::SpanPtr(new Tracing::NullSpan());
}

SpanContext context(trace_id, span_id, parent_id);

new_zipkin_span =
tracer.startSpan(config, request_headers.Host()->value().c_str(), start_time, context);
} else {
Expand Down
82 changes: 82 additions & 0 deletions test/common/tracing/zipkin/zipkin_tracer_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -344,5 +344,87 @@ TEST_F(ZipkinDriverTest, ZipkinSpanTest) {
EXPECT_EQ("key3", zipkin_zipkin_span3.binaryAnnotations()[0].key());
EXPECT_EQ("value3", zipkin_zipkin_span3.binaryAnnotations()[0].value());
}

TEST_F(ZipkinDriverTest, ZipkinSpanContextFromOtSpanContextHeaderTest) {
setupValidDriver();

const std::string trace_id = Hex::uint64ToHex(Util::generateRandom64());
const std::string span_id = Hex::uint64ToHex(Util::generateRandom64());
const std::string parent_id = Hex::uint64ToHex(Util::generateRandom64());
const std::string context =
trace_id + ";" + span_id + ";" + parent_id + ";" + ZipkinCoreConstants::get().CLIENT_SEND;

request_headers_.insertOtSpanContext().value(context);

// New span will have an SR annotation - so its span and parent ids will be
// the same as the supplied span context (i.e. shared context)
Tracing::SpanPtr span =
driver_->startSpan(config_, request_headers_, operation_name_, start_time_);

ZipkinSpanPtr zipkin_span(dynamic_cast<ZipkinSpan*>(span.release()));

EXPECT_EQ(trace_id, zipkin_span->span().traceIdAsHexString());
EXPECT_EQ(span_id, zipkin_span->span().idAsHexString());
EXPECT_EQ(parent_id, zipkin_span->span().parentIdAsHexString());
}

TEST_F(ZipkinDriverTest, ZipkinSpanContextFromB3HeadersTest) {
setupValidDriver();

const std::string trace_id = Hex::uint64ToHex(Util::generateRandom64());
const std::string span_id = Hex::uint64ToHex(Util::generateRandom64());
const std::string parent_id = Hex::uint64ToHex(Util::generateRandom64());

request_headers_.insertXB3TraceId().value(trace_id);
request_headers_.insertXB3SpanId().value(span_id);
request_headers_.insertXB3ParentSpanId().value(parent_id);

// New span will have an SR annotation - so its span and parent ids will be
// the same as the supplied span context (i.e. shared context)
Tracing::SpanPtr span =
driver_->startSpan(config_, request_headers_, operation_name_, start_time_);

ZipkinSpanPtr zipkin_span(dynamic_cast<ZipkinSpan*>(span.release()));

EXPECT_EQ(trace_id, zipkin_span->span().traceIdAsHexString());
EXPECT_EQ(span_id, zipkin_span->span().idAsHexString());
EXPECT_EQ(parent_id, zipkin_span->span().parentIdAsHexString());
}

TEST_F(ZipkinDriverTest, ZipkinSpanContextFromInvalidTraceIdB3HeadersTest) {
setupValidDriver();

request_headers_.insertXB3TraceId().value(std::string("xyz"));
request_headers_.insertXB3SpanId().value(Hex::uint64ToHex(Util::generateRandom64()));
request_headers_.insertXB3ParentSpanId().value(Hex::uint64ToHex(Util::generateRandom64()));

Tracing::SpanPtr span =
driver_->startSpan(config_, request_headers_, operation_name_, start_time_);
EXPECT_NE(nullptr, dynamic_cast<Tracing::NullSpan*>(span.get()));
}

TEST_F(ZipkinDriverTest, ZipkinSpanContextFromInvalidSpanIdB3HeadersTest) {
setupValidDriver();

request_headers_.insertXB3TraceId().value(Hex::uint64ToHex(Util::generateRandom64()));
request_headers_.insertXB3SpanId().value(std::string("xyz"));
request_headers_.insertXB3ParentSpanId().value(Hex::uint64ToHex(Util::generateRandom64()));

Tracing::SpanPtr span =
driver_->startSpan(config_, request_headers_, operation_name_, start_time_);
EXPECT_NE(nullptr, dynamic_cast<Tracing::NullSpan*>(span.get()));
}

TEST_F(ZipkinDriverTest, ZipkinSpanContextFromInvalidParentIdB3HeadersTest) {
setupValidDriver();

request_headers_.insertXB3TraceId().value(Hex::uint64ToHex(Util::generateRandom64()));
request_headers_.insertXB3SpanId().value(Hex::uint64ToHex(Util::generateRandom64()));
request_headers_.insertXB3ParentSpanId().value(std::string("xyz"));

Tracing::SpanPtr span =
driver_->startSpan(config_, request_headers_, operation_name_, start_time_);
EXPECT_NE(nullptr, dynamic_cast<Tracing::NullSpan*>(span.get()));
}
} // namespace Zipkin
} // namespace Envoy