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

Support Zipkin v2 Span format #12

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
2 changes: 1 addition & 1 deletion examples/grpc_greeter/greeter_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct GreeterServiceImpl final : public Greeter::Service
};

DEFINE_string(grpc_addr, "localhost:50051", "GRPC server address");
DEFINE_string(collector_uri, "", "Collector URI for tracing");
DEFINE_string(collector_uri, "http://localhost:9411/zipkin/api/v1", "Collector URI for tracing");

void RunServer(std::shared_ptr<zipkin::Collector> collector)
{
Expand Down
4 changes: 3 additions & 1 deletion examples/simple_proxy/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ void forward_http_request(struct mg_connection *nc, struct http_message *hm, zip
else if (0 == mg_vcmp(hn, ZIPKIN_X_SPAN_ID))
{
zipkin_span_set_parent_id(span, strtoull(hv->p, NULL, 16));
zipkin_span_set_shared(span, 1);
}
else
{
Expand Down Expand Up @@ -273,6 +274,7 @@ void reply_json_response(struct mg_connection *nc, struct http_message *hm, zipk
else if (0 == mg_vcmp(hn, ZIPKIN_X_SPAN_ID))
{
zipkin_span_set_parent_id(span, strtoull(hv->p, NULL, 16));
zipkin_span_set_shared(span, 1);
}
else if (0 == mg_vcmp(hn, ZIPKIN_X_SAMPLED))
{
Expand Down Expand Up @@ -573,4 +575,4 @@ int main(int argc, char **argv)
}

return 0;
}
}
5 changes: 4 additions & 1 deletion include/zipkin.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ zipkin_span_t zipkin_span_parse_trace_id(zipkin_span_t span, const char *str, si
int zipkin_span_debug(zipkin_span_t span);
zipkin_span_t zipkin_span_set_debug(zipkin_span_t span, int debug);

int zipkin_span_shared(zipkin_span_t span);
zipkin_span_t zipkin_span_set_shared(zipkin_span_t span, int shared);

int zipkin_span_sampled(zipkin_span_t span);
zipkin_span_t zipkin_span_set_sampled(zipkin_span_t span, int sampled);

Expand Down Expand Up @@ -297,4 +300,4 @@ struct curl_slist *zipkin_propagation_inject_curl_headers(struct curl_slist *hea

#ifdef __cplusplus
}
#endif
#endif
13 changes: 12 additions & 1 deletion src/CApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ zipkin_span_t zipkin_span_set_debug(zipkin_span_t span, int debug)

return span;
}
int zipkin_span_shared(zipkin_span_t span)
{
return span ? static_cast<zipkin::Span *>(span)->shared() : false;
}
zipkin_span_t zipkin_span_set_shared(zipkin_span_t span, int shared)
{
if (span)
static_cast<zipkin::Span *>(span)->with_shared(shared);

return span;
}
int zipkin_span_sampled(zipkin_span_t span)
{
return span ? static_cast<zipkin::Span *>(span)->sampled() : false;
Expand Down Expand Up @@ -568,4 +579,4 @@ struct curl_slist *zipkin_propagation_inject_curl_headers(struct curl_slist *hea

#ifdef __cplusplus
}
#endif
#endif
23 changes: 20 additions & 3 deletions src/Collector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace zipkin
std::shared_ptr<BinaryCodec> MessageCodec::binary(new BinaryCodec());
std::shared_ptr<JsonCodec> MessageCodec::json(new JsonCodec());
std::shared_ptr<PrettyJsonCodec> MessageCodec::pretty_json(new PrettyJsonCodec());
std::shared_ptr<JsonCodec> MessageCodec::json_v2(new JsonCodec(2));
std::shared_ptr<PrettyJsonCodec> MessageCodec::pretty_json_v2(new PrettyJsonCodec(2));

CompressionCodec parse_compression_codec(const std::string &codec)
{
Expand Down Expand Up @@ -57,7 +59,10 @@ std::shared_ptr<MessageCodec> MessageCodec::parse(const std::string &codec)
return json;
if (codec == "pretty_json")
return pretty_json;

if (codec == "json_v2")
return json_v2;
if (codec == "pretty_json_v2")
return pretty_json_v2;
return nullptr;
}

Expand Down Expand Up @@ -85,7 +90,13 @@ size_t JsonCodec::encode(boost::shared_ptr<apache::thrift::transport::TMemoryBuf

for (auto &span : spans)
{
span->serialize_json(writer);
if (m_format_version > 1) {
for (auto &span2: __impl::Span2::from_span(span)) {
span2.serialize_json(writer);
}
} else {
span->serialize_json(writer);
}
}

writer.EndArray();
Expand All @@ -105,7 +116,13 @@ size_t PrettyJsonCodec::encode(boost::shared_ptr<apache::thrift::transport::TMem

for (auto &span : spans)
{
span->serialize_json(writer);
if (m_format_version > 1) {
for (auto &span2: __impl::Span2::from_span(span)) {
span2.serialize_json(writer);
}
} else {
span->serialize_json(writer);
}
}

writer.EndArray();
Expand Down
17 changes: 15 additions & 2 deletions src/Collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <atomic>
#include <thread>
#include <mutex>

#include <condition_variable>

#include <boost/lockfree/queue.hpp>
Expand Down Expand Up @@ -54,6 +55,8 @@ class MessageCodec
static std::shared_ptr<BinaryCodec> binary;
static std::shared_ptr<JsonCodec> json;
static std::shared_ptr<PrettyJsonCodec> pretty_json;
static std::shared_ptr<JsonCodec> json_v2;
static std::shared_ptr<PrettyJsonCodec> pretty_json_v2;
};

/**
Expand All @@ -74,8 +77,13 @@ class BinaryCodec : public MessageCodec
*/
class JsonCodec : public MessageCodec
{
int m_format_version;
public:
virtual const std::string name(void) const override { return "json"; }
JsonCodec(int format_version = 1) : m_format_version(format_version) {}

virtual const std::string name(void) const override {
return m_format_version > 1 ? "json_v2" : "json";
}

virtual const std::string mime_type(void) const override { return "application/json"; }

Expand All @@ -87,8 +95,13 @@ class JsonCodec : public MessageCodec
*/
class PrettyJsonCodec : public MessageCodec
{
int m_format_version;
public:
virtual const std::string name(void) const override { return "pretty_json"; }
PrettyJsonCodec(int format_version = 1) : m_format_version(format_version) {}

virtual const std::string name(void) const override {
return m_format_version > 1 ? "pretty_json_v2" : "pretty_json";
}

virtual const std::string mime_type(void) const override { return "application/json"; }

Expand Down
5 changes: 3 additions & 2 deletions src/Propagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ void Propagation::extract(grpc::ServerContext &context, Span &span)
}
else if (item.first == ZIPKIN_X_SPAN_ID_LOWERCASE)
{
span.with_parent_id(folly::to<uint64_t>(value));
span.with_parent_id(folly::to<uint64_t>(value))
.with_shared(true);
}
else if (item.first == ZIPKIN_X_SAMPLED_LOWERCASE)
{
Expand All @@ -127,4 +128,4 @@ void Propagation::extract(grpc::ServerContext &context, Span &span)

#endif // WITH_GRPC

} // namespace zipkin
} // namespace zipkin
Loading