Skip to content

Commit

Permalink
tracing: add direct support for a span to spawn a child
Browse files Browse the repository at this point in the history
  • Loading branch information
goaway committed May 12, 2017
1 parent 0259407 commit a0c8036
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
8 changes: 6 additions & 2 deletions include/envoy/tracing/http_tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,20 @@ class Config {
/*
* Basic abstraction for span.
*/
class Span;

typedef std::unique_ptr<Span> SpanPtr;

class Span {
public:
virtual ~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 SpanPtr spawnChild(const std::string& name, SystemTime start_time) PURE;
};

typedef std::unique_ptr<Span> SpanPtr;

/**
* Tracing driver is responsible for span creation.
*/
Expand Down
5 changes: 5 additions & 0 deletions source/common/tracing/http_tracer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ class NullSpan : public Tracing::Span {
// Tracing::Span
void setTag(const std::string&, const std::string&) override {}
void finishSpan() override {}
SpanPtr spawnChild(const std::string&, SystemTime) override {
SpanPtr nullSpan;
nullSpan.reset(new NullSpan());
return std::move(nullSpan);
}
};

} // Tracing
19 changes: 15 additions & 4 deletions source/common/tracing/lightstep_tracer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,24 @@

namespace Tracing {

LightStepSpan::LightStepSpan(lightstep::Span& span) : span_(span) {}
LightStepSpan::LightStepSpan(lightstep::Span& span, lightstep::Tracer& tracer)
: span_(span), tracer_(tracer) {}

void LightStepSpan::finishSpan() { span_.Finish(); }

void LightStepSpan::setTag(const std::string& name, const std::string& value) {
span_.SetTag(name, value);
}

SpanPtr LightStepSpan::spawnChild(const std::string& name, SystemTime start_time) {
SpanPtr child_span;
lightstep::Span ls_span = tracer_.StartSpan(
name, {lightstep::ChildOf(span_.context()), lightstep::StartTimestamp(start_time)});
child_span.reset(new LightStepSpan(ls_span, tracer_));

return std::move(child_span);
}

LightStepRecorder::LightStepRecorder(const lightstep::TracerImpl& tracer, LightStepDriver& driver,
Event::Dispatcher& dispatcher)
: builder_(tracer), driver_(driver) {
Expand Down Expand Up @@ -133,20 +143,21 @@ SpanPtr LightStepDriver::startSpan(Http::HeaderMap& request_headers,
lightstep::Span ls_span =
tracer.StartSpan(operation_name, {lightstep::ChildOf(parent_span_ctx),
lightstep::StartTimestamp(start_time)});
active_span.reset(new LightStepSpan(ls_span));
active_span.reset(new LightStepSpan(ls_span, tracer));
} else {
lightstep::Span ls_span =
tracer.StartSpan(operation_name, {lightstep::StartTimestamp(start_time)});
active_span.reset(new LightStepSpan(ls_span));
active_span.reset(new LightStepSpan(ls_span, tracer));
}

// Inject newly created span context into HTTP carrier.
// 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
4 changes: 3 additions & 1 deletion source/common/tracing/lightstep_tracer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ struct LightstepTracerStats {

class LightStepSpan : public Span {
public:
LightStepSpan(lightstep::Span& span);
LightStepSpan(lightstep::Span& span, lightstep::Tracer& tracer);

// Tracing::Span
void finishSpan() override;
void setTag(const std::string& name, const std::string& value) override;
SpanPtr spawnChild(const std::string& name, SystemTime start_time) override;

lightstep::SpanContext context() { return span_.context(); }

private:
lightstep::Span span_;
lightstep::Tracer& tracer_;
};

typedef std::unique_ptr<LightStepSpan> LightStepSpanPtr;
Expand Down
8 changes: 7 additions & 1 deletion test/mocks/tracing/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class MockSpan : public Span {

MOCK_METHOD2(setTag, void(const std::string& name, const std::string& value));
MOCK_METHOD0(finishSpan, void());

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

MOCK_METHOD2(spawnChild_, Span*(const std::string& name, SystemTime start_time));
};

class MockHttpTracer : public HttpTracer {
Expand Down Expand Up @@ -63,4 +69,4 @@ class MockDriver : public Driver {
const std::string& operation_name, SystemTime start_time));
};

} // Tracing
} // Tracing

0 comments on commit a0c8036

Please sign in to comment.