Skip to content

Commit

Permalink
Change to use StringUtil and check for errors (with tests)
Browse files Browse the repository at this point in the history
Signed-off-by: Gary Brown <[email protected]>
  • Loading branch information
objectiser committed Sep 20, 2017
1 parent e6b82ff commit eb237f0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
22 changes: 16 additions & 6 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 @@ -107,20 +108,29 @@ Tracing::SpanPtr Driver::startSpan(const Tracing::Config& config, Http::HeaderMa

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 traceId(0);
uint64_t spanId(0);
uint64_t parentId(0);
if (!StringUtil::atoul(request_headers.XB3TraceId()->value().c_str(), traceId, 16)) {
throw EnvoyException(fmt::format("invalid hex string for XB3TraceId '{}'",
request_headers.XB3TraceId()->value().c_str()));
}
if (!StringUtil::atoul(request_headers.XB3SpanId()->value().c_str(), spanId, 16)) {
throw EnvoyException(fmt::format("invalid hex string for XB3SpanId '{}'",
request_headers.XB3SpanId()->value().c_str()));
}
if (request_headers.XB3ParentSpanId()) {
parentId = std::stoull(request_headers.XB3ParentSpanId()->value().c_str(), nullptr, 16);
if (!StringUtil::atoul(request_headers.XB3ParentSpanId()->value().c_str(), parentId, 16)) {
throw EnvoyException(fmt::format("invalid hex string for XB3ParentSpanId '{}'",
request_headers.XB3ParentSpanId()->value().c_str()));
}
}

SpanContext context(std::stoull(request_headers.XB3TraceId()->value().c_str(), nullptr, 16),
std::stoull(request_headers.XB3SpanId()->value().c_str(), nullptr, 16),
parentId);
SpanContext context(traceId, spanId, parentId);

new_zipkin_span =
tracer.startSpan(config, request_headers.Host()->value().c_str(), start_time, context);

} else {
// Create a root Zipkin span. No context was found in the headers.
new_zipkin_span = tracer.startSpan(config, request_headers.Host()->value().c_str(), start_time);
Expand Down
33 changes: 33 additions & 0 deletions test/common/tracing/zipkin/zipkin_tracer_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -390,5 +390,38 @@ TEST_F(ZipkinDriverTest, ZipkinSpanContextFromB3HeadersTest) {
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()));

EXPECT_THROW(driver_->startSpan(config_, request_headers_, operation_name_, start_time_),
EnvoyException);
}

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()));

EXPECT_THROW(driver_->startSpan(config_, request_headers_, operation_name_, start_time_),
EnvoyException);
}

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"));

EXPECT_THROW(driver_->startSpan(config_, request_headers_, operation_name_, start_time_),
EnvoyException);
}
} // namespace Zipkin
} // namespace Envoy

0 comments on commit eb237f0

Please sign in to comment.