Skip to content

Commit

Permalink
add inject to Span interface and test
Browse files Browse the repository at this point in the history
  • Loading branch information
goaway committed May 15, 2017
1 parent a0c8036 commit d483a33
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 10 deletions.
2 changes: 1 addition & 1 deletion include/envoy/tracing/http_tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Span {

virtual void setTag(const std::string& name, const std::string& value) PURE;
virtual void finishSpan() PURE;
// virtual void inject(Http::HeaderMap& request_headers) PURE;
virtual void injectContext(Http::HeaderMap& request_headers) PURE;
virtual SpanPtr spawnChild(const std::string& name, SystemTime start_time) PURE;
};

Expand Down
1 change: 1 addition & 0 deletions source/common/http/conn_manager_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ void ConnectionManagerImpl::ActiveStream::decodeHeaders(HeaderMapPtr&& headers,

if (tracing_decision.is_tracing) {
active_span_ = connection_manager_.tracer_.startSpan(*this, *request_headers_, request_info_);
active_span_->injectContext(*request_headers_);
}
}

Expand Down
1 change: 1 addition & 0 deletions source/common/tracing/http_tracer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class NullSpan : public Tracing::Span {
// Tracing::Span
void setTag(const std::string&, const std::string&) override {}
void finishSpan() override {}
void injectContext(Http::HeaderMap&) override {}
SpanPtr spawnChild(const std::string&, SystemTime) override {
SpanPtr nullSpan;
nullSpan.reset(new NullSpan());
Expand Down
18 changes: 9 additions & 9 deletions source/common/tracing/lightstep_tracer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ void LightStepSpan::setTag(const std::string& name, const std::string& value) {
span_.SetTag(name, value);
}

void LightStepSpan::injectContext(Http::HeaderMap& request_headers) {
lightstep::BinaryCarrier ctx;
tracer_.Inject(context(), lightstep::CarrierFormat::LightStepBinaryCarrier,
lightstep::ProtoWriter(&ctx));
const std::string current_span_context = ctx.SerializeAsString();
request_headers.insertOtSpanContext().value(
Base64::encode(current_span_context.c_str(), current_span_context.length()));
}

SpanPtr LightStepSpan::spawnChild(const std::string& name, SystemTime start_time) {
SpanPtr child_span;
lightstep::Span ls_span = tracer_.StartSpan(
Expand Down Expand Up @@ -150,15 +159,6 @@ SpanPtr LightStepDriver::startSpan(Http::HeaderMap& request_headers,
active_span.reset(new LightStepSpan(ls_span, tracer));
}

// Inject newly created span context into HTTP carrier. TODO: move to inject()
lightstep::BinaryCarrier ctx;
tracer.Inject(active_span->context(), lightstep::CarrierFormat::LightStepBinaryCarrier,
lightstep::ProtoWriter(&ctx));
const std::string current_span_context = ctx.SerializeAsString();
request_headers.insertOtSpanContext().value(
Base64::encode(current_span_context.c_str(), current_span_context.length()));
// end TODO

return std::move(active_span);
}

Expand Down
1 change: 1 addition & 0 deletions source/common/tracing/lightstep_tracer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class LightStepSpan : public Span {
// Tracing::Span
void finishSpan() override;
void setTag(const std::string& name, const std::string& value) override;
void injectContext(Http::HeaderMap& request_headers) override;
SpanPtr spawnChild(const std::string& name, SystemTime start_time) override;

lightstep::SpanContext context() { return span_.context(); }
Expand Down
25 changes: 25 additions & 0 deletions test/common/tracing/lightstep_tracer_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ TEST_F(LightStepDriverTest, SerializeAndDeserializeContext) {
// Supply empty context.
request_headers_.removeOtSpanContext();
SpanPtr span = driver_->startSpan(request_headers_, operation_name_, start_time_);
span->injectContext(request_headers_);

injected_ctx = request_headers_.OtSpanContext()->value().c_str();
EXPECT_FALSE(injected_ctx.empty());
Expand All @@ -285,8 +286,32 @@ TEST_F(LightStepDriverTest, SerializeAndDeserializeContext) {

// Supply parent context, request_headers has properly populated x-ot-span-context.
SpanPtr span_with_parent = driver_->startSpan(request_headers_, operation_name_, start_time_);
request_headers_.removeOtSpanContext();
span_with_parent->injectContext(request_headers_);
injected_ctx = request_headers_.OtSpanContext()->value().c_str();
EXPECT_FALSE(injected_ctx.empty());
}

TEST_F(LightStepDriverTest, SpawnChild) {
setupValidDriver();

SpanPtr parent = driver_->startSpan(request_headers_, operation_name_, start_time_);
parent->injectContext(request_headers_);

SpanPtr childViaHeaders = driver_->startSpan(request_headers_, operation_name_, start_time_);
SpanPtr childViaSpawn = parent->spawnChild(operation_name_, start_time_);

Http::TestHeaderMapImpl base1{{":path", "/"}, {":method", "GET"}, {"x-request-id", "foo"}};
Http::TestHeaderMapImpl base2{{":path", "/"}, {":method", "GET"}, {"x-request-id", "foo"}};

childViaHeaders->injectContext(base1);
childViaSpawn->injectContext(base2);

std::string base1_context = Base64::decode(base1.OtSpanContext()->value().c_str());
std::string base2_context = Base64::decode(base2.OtSpanContext()->value().c_str());

EXPECT_FALSE(base1_context.empty());
EXPECT_FALSE(base2_context.empty());
}

} // Tracing
1 change: 1 addition & 0 deletions test/mocks/tracing/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class MockSpan : public Span {

MOCK_METHOD2(setTag, void(const std::string& name, const std::string& value));
MOCK_METHOD0(finishSpan, void());
MOCK_METHOD1(injectContext, void(Http::HeaderMap& request_headers));

SpanPtr spawnChild(const std::string& name, SystemTime start_time) override {
return SpanPtr{spawnChild_(name, start_time)};
Expand Down

0 comments on commit d483a33

Please sign in to comment.