Skip to content

Commit

Permalink
tracers-zipkin(envoyproxy#4839): adds span serializer interface and s…
Browse files Browse the repository at this point in the history
…ets v1 as the default serializer.

Signed-off-by: José Carlos Chávez <jcchavezs@gmail.com>
  • Loading branch information
jcchavezs committed May 6, 2019
1 parent 74563b2 commit 5cfbed5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
6 changes: 3 additions & 3 deletions source/extensions/tracers/zipkin/span_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ bool SpanBuffer::addSpan(const Span& span) {
return true;
}

std::string SpanBuffer::toStringifiedJsonArray() {
std::string SpanBuffer::toStringifiedJsonArray(SpanSerializer& span_serialier) {
std::string stringified_json_array = "[";

if (pendingSpans()) {
stringified_json_array += span_buffer_[0].toJson();
stringified_json_array += span_serialier.serialize(span_buffer_[0]);
const uint64_t size = span_buffer_.size();
for (uint64_t i = 1; i < size; i++) {
stringified_json_array += ",";
stringified_json_array += span_buffer_[i].toJson();
stringified_json_array += span_serialier.serialize(span_buffer_[i]);
}
}
stringified_json_array += "]";
Expand Down
3 changes: 2 additions & 1 deletion source/extensions/tracers/zipkin/span_buffer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "extensions/tracers/zipkin/span_serializer.h"
#include "extensions/tracers/zipkin/zipkin_core_types.h"

namespace Envoy {
Expand Down Expand Up @@ -57,7 +58,7 @@ class SpanBuffer {
* @return the contents of the buffer as a stringified array of JSONs, where
* each JSON in the array corresponds to one Zipkin span.
*/
std::string toStringifiedJsonArray();
std::string toStringifiedJsonArray(SpanSerializer& span_serializer);

private:
// We use a pre-allocated vector to improve performance
Expand Down
26 changes: 26 additions & 0 deletions source/extensions/tracers/zipkin/span_serializer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include "extensions/tracers/zipkin/zipkin_core_types.h"

namespace Envoy {
namespace Extensions {
namespace Tracers {
namespace Zipkin {
class SpanSerializer {
public:
/**
* Destructor.
*/
virtual ~SpanSerializer() {}

/**
* Method that a concrete SpanSerializer class must implement to serialize spans.
*
* @param span The span that needs action.
*/
virtual std::string serialize(const Span& span);
};
} // namespace Zipkin
} // namespace Tracers
} // namespace Extensions
} // namespace Envoy
19 changes: 16 additions & 3 deletions source/extensions/tracers/zipkin/zipkin_tracer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,21 @@ Tracing::SpanPtr Driver::startSpan(const Tracing::Config& config, Http::HeaderMa
return std::move(active_span);
}

V1SpanSerializer::V1SpanSerializer() {}

const std::string V1SpanSerializer::serialize(const Span& span) { return span.toJson(); }

ReporterImpl::ReporterImpl(Driver& driver, Event::Dispatcher& dispatcher,
const std::string& collector_endpoint)
const std::string& collector_endpoint, SpanSerializer& span_serializer)
: driver_(driver), collector_endpoint_(collector_endpoint) {
flush_timer_ = dispatcher.createTimer([this]() -> void {
driver_.tracerStats().timer_flushed_.inc();
flushSpans();
enableTimer();
});

span_serializer_ = span_serializer;

const uint64_t min_flush_spans =
driver_.runtime().snapshot().getInteger("tracing.zipkin.min_flush_spans", 5U);
span_buffer_.allocateBuffer(min_flush_spans);
Expand All @@ -140,7 +146,14 @@ ReporterImpl::ReporterImpl(Driver& driver, Event::Dispatcher& dispatcher,

ReporterPtr ReporterImpl::NewInstance(Driver& driver, Event::Dispatcher& dispatcher,
const std::string& collector_endpoint) {
return ReporterPtr(new ReporterImpl(driver, dispatcher, collector_endpoint));
return ReporterPtr(
new ReporterImpl(driver, dispatcher, collector_endpoint, new V1SpanSerializer()));
}

ReporterPtr ReporterImpl::NewInstanceWithSerializer(Driver& driver, Event::Dispatcher& dispatcher,
const std::string& collector_endpoint,
SpanSerializer span_serializer) {
return ReporterPtr(new ReporterImpl(driver, dispatcher, collector_endpoint, span_serializer));
}

// TODO(fabolive): Need to avoid the copy to improve performance.
Expand All @@ -165,7 +178,7 @@ void ReporterImpl::flushSpans() {
if (span_buffer_.pendingSpans()) {
driver_.tracerStats().spans_sent_.add(span_buffer_.pendingSpans());

const std::string request_body = span_buffer_.toStringifiedJsonArray();
const std::string request_body = span_buffer_.toStringifiedJsonArray(span_serializer_);
Http::MessagePtr message(new Http::RequestMessageImpl());
message->headers().insertMethod().value().setReference(Http::Headers::get().MethodValues.Post);
message->headers().insertPath().value(collector_endpoint_);
Expand Down
1 change: 1 addition & 0 deletions source/extensions/tracers/zipkin/zipkin_tracer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ class ReporterImpl : public Reporter, Http::AsyncClient::Callbacks {
Event::TimerPtr flush_timer_;
SpanBuffer span_buffer_;
const std::string collector_endpoint_;
SpanSerializer span_serializer_;
};
} // namespace Zipkin
} // namespace Tracers
Expand Down

0 comments on commit 5cfbed5

Please sign in to comment.