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

tracing: Add support for sending data in Zipkin v2 format #6985

Merged
merged 55 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
d445cc8
Prepare sending proto3 to zipkin
dio May 16, 2019
ac13b27
Generate ids
dio May 16, 2019
7fdbeb9
Add newline
dio May 16, 2019
07dfb1d
Add test for endpoint proto
dio May 16, 2019
07c897b
Add test for zipkin_core_types
dio May 17, 2019
9aafd95
Add span buffer test
dio May 17, 2019
8685f63
Fix
dio May 17, 2019
4c58100
Fix
dio May 17, 2019
c6f86c8
Rename bytesOf to toByteString
dio May 17, 2019
c775be0
Add changelog
dio May 18, 2019
d60b37f
Review comments
dio May 27, 2019
d03f5f4
Fix
dio May 27, 2019
a75668b
Fix format
dio May 27, 2019
544522c
Merge remote-tracking branch 'upstream/master'
dio May 27, 2019
7c925bc
Merge remote-tracking branch 'upstream/master'
dio Aug 15, 2019
53d0ded
Fix tests
dio Aug 15, 2019
12015fe
Fix format
dio Aug 15, 2019
4c7542f
Fix format
dio Aug 16, 2019
f7601d0
apache -> openzipkin
dio Aug 16, 2019
2a2746a
Fix sha256
dio Aug 16, 2019
e4cf785
Fix format
dio Aug 17, 2019
66ea6dd
Change approach to use serializer
dio Aug 20, 2019
240c9a1
Comments and add tests
dio Aug 21, 2019
8c0480d
Fix docs
dio Aug 21, 2019
ee21280
Merge remote-tracking branch 'upstream/master'
dio Aug 21, 2019
acfa29d
Fix
dio Aug 21, 2019
50aec33
absl::StrAppend and absl::StrCat
dio Aug 21, 2019
bd462dc
Remove wrong entry
dio Aug 21, 2019
273c92c
Fix clang-tidy
dio Aug 21, 2019
756261b
Add missing comments
dio Aug 21, 2019
49f6bf5
Remove unused headers
dio Aug 21, 2019
3d3fcae
Fix clang-tidy again
dio Aug 21, 2019
336fe07
Merge remote-tracking branch 'upstream/master'
dio Aug 21, 2019
57625de
Fix alpha ordering later
dio Aug 21, 2019
f303859
Comments
dio Aug 22, 2019
93cbf23
Comments
dio Aug 22, 2019
19f3bb4
Use vector
dio Aug 23, 2019
ba7bf9c
Use 0.2.2 release
dio Aug 23, 2019
199d28e
Fix
dio Aug 23, 2019
10f220d
Cleanup
dio Aug 23, 2019
7971c73
Fix
dio Aug 23, 2019
f21f54e
Merge remote-tracking branch 'upstream/master'
dio Aug 23, 2019
6a39da4
Comments
dio Aug 23, 2019
d91e756
Fix
dio Aug 23, 2019
b00b470
Comments
dio Aug 23, 2019
7c3a278
Merge remote-tracking branch 'upstream/master'
dio Aug 26, 2019
a1c8cc7
Add comment on immediate deprecation
dio Aug 27, 2019
f413994
Fix
dio Aug 27, 2019
17e24d8
Comments
dio Aug 28, 2019
e602b50
Fix
dio Aug 28, 2019
3f4c927
More const-ness
dio Aug 28, 2019
527f62c
make_unique and make_shared
dio Aug 28, 2019
c409f91
Comments
dio Aug 29, 2019
3976724
Remove TODOs
dio Aug 29, 2019
ec4ca84
Merge remote-tracking branch 'upstream/master'
dio Aug 30, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions api/bazel/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ def api_dependencies():
locations = REPOSITORY_LOCATIONS,
build_file_content = KAFKASOURCE_BUILD_CONTENT,
)
envoy_http_archive(
name = "com_github_apache_zipkinapi",
locations = REPOSITORY_LOCATIONS,
build_file_content = ZIPKINAPI_BUILD_CONTENT,
)

GOOGLEAPIS_BUILD_CONTENT = """
load("@com_google_protobuf//:protobuf.bzl", "cc_proto_library", "py_proto_library")
Expand Down Expand Up @@ -303,3 +308,21 @@ filegroup(
)

"""

ZIPKINAPI_BUILD_CONTENT = """
load("@envoy_api//bazel:api_build_system.bzl", "api_proto_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
api_proto_library(
name = "zipkin",
srcs = [
"zipkin.proto",
],
visibility = ["//visibility:public"],
)
go_proto_library(
dio marked this conversation as resolved.
Show resolved Hide resolved
name = "zipkin_go_proto",
importpath = "zipkin",
proto = ":zipkin",
visibility = ["//visibility:public"],
)
"""
8 changes: 8 additions & 0 deletions api/bazel/repository_locations.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ PROMETHEUS_SHA = "783bdaf8ee0464b35ec0c8704871e1e72afa0005c3f3587f65d9d6694bf391

KAFKA_SOURCE_SHA = "ae7a1696c0a0302b43c5b21e515c37e6ecd365941f68a510a7e442eebddf39a1" # 2.2.0-rc2

ZIPKINAPI_RELEASE = "0.2.1"
ZIPKINAPI_SHA256 = "98a62b36493f189cf47eb2c07c541212d09b3b497f66f698ac7bc5b6305d742a"

REPOSITORY_LOCATIONS = dict(
bazel_skylib = dict(
sha256 = BAZEL_SKYLIB_SHA256,
Expand Down Expand Up @@ -54,4 +57,9 @@ REPOSITORY_LOCATIONS = dict(
strip_prefix = "kafka-2.2.0-rc2/clients/src/main/resources/common/message",
urls = ["https://github.com/apache/kafka/archive/2.2.0-rc2.zip"],
),
com_github_apache_zipkinapi = dict(
sha256 = ZIPKINAPI_SHA256,
strip_prefix = "incubator-zipkin-api-" + ZIPKINAPI_RELEASE,
urls = ["https://github.com/apache/incubator-zipkin-api/archive/v" + ZIPKINAPI_RELEASE + ".tar.gz"],
),
)
11 changes: 11 additions & 0 deletions api/envoy/config/trace/v2/trace.proto
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ message ZipkinConfig {
// Determines whether client and server spans will shared the same span id.
dio marked this conversation as resolved.
Show resolved Hide resolved
// The default value is true.
google.protobuf.BoolValue shared_span_context = 4;

// Available zipkin collector endpoint versions.
enum CollectorEndpointVersion {
DEFAULT = 0;
dio marked this conversation as resolved.
Show resolved Hide resolved
HTTP_JSON_V1 = 1;
HTTP_PROTO = 2;
}

// Determines the selected collector endpoint version. By default, the ``HTTP_JSON_V1`` will be
// used.
CollectorEndpointVersion collector_endpoint_version = 6;
}

// DynamicOtConfig is used to dynamically load a tracer from a shared library
Expand Down
1 change: 1 addition & 0 deletions source/common/http/headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class HeaderValues {
const std::string GrpcWebText{"application/grpc-web-text"};
const std::string GrpcWebTextProto{"application/grpc-web-text+proto"};
const std::string Json{"application/json"};
const std::string Protobuf{"application/x-protobuf"};
} ContentTypeValues;

struct {
Expand Down
1 change: 1 addition & 0 deletions source/extensions/tracers/zipkin/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ envoy_cc_library(
"//source/common/singleton:const_singleton",
"//source/common/tracing:http_tracer_lib",
"//source/extensions/tracers:well_known_names",
"@com_github_apache_zipkinapi//:zipkin_cc",
],
)

Expand Down
11 changes: 11 additions & 0 deletions source/extensions/tracers/zipkin/span_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ std::string SpanBuffer::toStringifiedJsonArray() {
return stringified_json_array;
}

const zipkin::proto3::ListOfSpans SpanBuffer::toProtoListOfSpans() const {
zipkin::proto3::ListOfSpans spans;
if (pendingSpans()) {
for (const auto& span : span_buffer_) {
auto* mutable_span = spans.add_spans();
mutable_span->MergeFrom(span.toProtoSpan());
}
}
return spans;
}

} // namespace Zipkin
} // namespace Tracers
} // namespace Extensions
Expand Down
14 changes: 13 additions & 1 deletion source/extensions/tracers/zipkin/span_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

#include "zipkin.pb.h"

namespace Envoy {
namespace Extensions {
namespace Tracers {
Expand Down Expand Up @@ -51,14 +53,24 @@ class SpanBuffer {
/**
* @return the number of spans currently buffered.
*/
uint64_t pendingSpans() { return span_buffer_.size(); }
uint64_t pendingSpans() const { return span_buffer_.size(); }

/**
* @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();

/**
* @return the contents of the buffer as a zipkin::proto3::ListOfSpans.
*/
const zipkin::proto3::ListOfSpans toProtoListOfSpans() const;

/**
* @return the current buffered spans.
*/
std::vector<Span> spans() const { return span_buffer_; }

private:
// We use a pre-allocated vector to improve performance
std::vector<Span> span_buffer_;
Expand Down
14 changes: 14 additions & 0 deletions source/extensions/tracers/zipkin/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "envoy/common/time.h"

#include "common/common/byte_order.h"

namespace Envoy {
namespace Extensions {
namespace Tracers {
Expand Down Expand Up @@ -48,6 +50,18 @@ class Util {
* Returns a randomly-generated 64-bit integer number.
*/
static uint64_t generateRandom64(TimeSource& time_source);

/**
* Returns byte string representation of an number.
dio marked this conversation as resolved.
Show resolved Hide resolved
*
* @param value Number that will be represented in byte string.
* @param flip indicates to flip order or not.
* @return std::string byte string representation of a number.
*/
template <typename Type> static std::string toByteString(Type value, bool flip = false) {
auto bytes = flip ? toEndianness<ByteOrder::BigEndian>(value) : value;
return std::string(reinterpret_cast<const char*>(&bytes), sizeof(Type));
}
};

} // namespace Zipkin
Expand Down
54 changes: 54 additions & 0 deletions source/extensions/tracers/zipkin/zipkin_core_types.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "extensions/tracers/zipkin/zipkin_core_types.h"

#include "common/common/byte_order.h"
#include "common/common/utility.h"

#include "extensions/tracers/zipkin/span_context.h"
Expand Down Expand Up @@ -55,6 +56,24 @@ const std::string Endpoint::toJson() {
return json_string;
}

const zipkin::proto3::Endpoint Endpoint::toProtoEndpoint() const {
zipkin::proto3::Endpoint endpoint;
if (!address_) {
endpoint.set_ipv4(EMPTY_STRING);
dio marked this conversation as resolved.
Show resolved Hide resolved
endpoint.set_port(0);
} else {
if (address_->ip()->version() == Network::Address::IpVersion::v4) {
endpoint.set_ipv4(address_->ip()->addressAsString());
} else {
endpoint.set_ipv6(address_->ip()->addressAsString());
}
endpoint.set_port(address_->ip()->port());
dio marked this conversation as resolved.
Show resolved Hide resolved
}

endpoint.set_service_name(service_name_);
dio marked this conversation as resolved.
Show resolved Hide resolved
return endpoint;
}

Annotation::Annotation(const Annotation& ann) {
timestamp_ = ann.timestamp();
value_ = ann.value();
Expand Down Expand Up @@ -219,6 +238,41 @@ const std::string Span::toJson() {
return json_string;
}

const zipkin::proto3::Span Span::toProtoSpan() const {
zipkin::proto3::Span span;
span.set_trace_id(traceIdAsByteString());
span.set_parent_id(parentIdAsByteString());
dio marked this conversation as resolved.
Show resolved Hide resolved
span.set_id(idAsByteString());
span.set_name(name_);

if (timestamp_) {
dio marked this conversation as resolved.
Show resolved Hide resolved
span.set_timestamp(timestamp_.value());
}

if (duration_) {
span.set_duration(duration_.value());
}

for (const auto& annotation : annotations_) {
if (annotation.isSetEndpoint()) {
if (annotation.value() == ZipkinCoreConstants::get().CLIENT_SEND) {
span.set_kind(zipkin::proto3::Span::CLIENT);
span.mutable_local_endpoint()->MergeFrom(annotation.endpoint().toProtoEndpoint());
} else if (annotation.value() == ZipkinCoreConstants::get().SERVER_RECV) {
span.set_kind(zipkin::proto3::Span::SERVER);
dio marked this conversation as resolved.
Show resolved Hide resolved
span.mutable_remote_endpoint()->MergeFrom(annotation.endpoint().toProtoEndpoint());
dio marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

auto& tags = *span.mutable_tags();
for (const auto& binary_annotation : binary_annotations_) {
tags[binary_annotation.key()] = binary_annotation.value();
dio marked this conversation as resolved.
Show resolved Hide resolved
}

return span;
}

void Span::finish() {
// Assumption: Span will have only one annotation when this method is called
SpanContext context(*this);
Expand Down
39 changes: 39 additions & 0 deletions source/extensions/tracers/zipkin/zipkin_core_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
#include "envoy/common/time.h"
#include "envoy/network/address.h"

#include "common/common/empty_string.h"
dio marked this conversation as resolved.
Show resolved Hide resolved
#include "common/common/hex.h"

#include "extensions/tracers/zipkin/tracer_interface.h"
#include "extensions/tracers/zipkin/util.h"

#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "zipkin.pb.h"

namespace Envoy {
namespace Extensions {
Expand Down Expand Up @@ -94,6 +96,13 @@ class Endpoint : public ZipkinBase {
*/
const std::string toJson() override;

/**
* Builds protobuf message representation of this endpoint.
*
* @return zipkin::proto3::Endpoint the protobuf message representation of this endpoint.
*/
const zipkin::proto3::Endpoint toProtoEndpoint() const;

private:
std::string service_name_;
Network::Address::InstanceConstSharedPtr address_;
Expand Down Expand Up @@ -443,6 +452,11 @@ class Span : public ZipkinBase {
*/
const std::string idAsHexString() const { return Hex::uint64ToHex(id_); }

/**
* @return the span's id as a byte string.
*/
const std::string idAsByteString() const { return Util::toByteString(id_); }

/**
* @return the span's name.
*/
Expand All @@ -460,6 +474,13 @@ class Span : public ZipkinBase {
return parent_id_ ? Hex::uint64ToHex(parent_id_.value()) : EMPTY_HEX_STRING_;
}

/**
* @return the span's parent id as a byte string.
*/
const std::string parentIdAsByteString() const {
return parent_id_ ? Util::toByteString(parent_id_.value()) : EMPTY_STRING;
}

/**
* @return whether or not the debug attribute is set
*/
Expand Down Expand Up @@ -494,6 +515,17 @@ class Span : public ZipkinBase {
: Hex::uint64ToHex(trace_id_);
}

/**
* @return the span's trace id as a byte string.
*/
const std::string traceIdAsByteString() const {
// TODO(dio): Make sure this is the right interpretation of
// https://github.com/apache/incubator-zipkin-api/blob/v0.2.1/zipkin.proto#L60-L61.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we change this reference to openzipkin/zipkin-api?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// https://github.com/apache/incubator-zipkin-api/blob/v0.2.1/zipkin.proto#L60-L61.
// https://github.com/openzipkin/zipkin-api/blob/v0.2.1/zipkin.proto#L60-L61.

return trace_id_high_.has_value() ? Util::toByteString(trace_id_high_.value(), true) +
dio marked this conversation as resolved.
Show resolved Hide resolved
Util::toByteString(trace_id_, true)
: Util::toByteString(trace_id_, true);
}

/**
* @return the span's start time (monotonic, used to calculate duration).
*/
Expand All @@ -518,6 +550,13 @@ class Span : public ZipkinBase {
*/
const std::string toJson() override;

/**
* Builds protobuf message representation of this span.
*
* @return zipkin::proto3::Span the protobuf message representation of this span.
*/
const zipkin::proto3::Span toProtoSpan() const;

/**
* Associates a Tracer object with the span. The tracer's reportSpan() method is invoked
* by the span's finish() method so that the tracer can decide what to do with the span
Expand Down
Loading