From e7ad40a0957289f0ba569acae12433bbdcf4c39d Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Wed, 29 Mar 2017 15:59:39 -0400 Subject: [PATCH 01/55] Initial check-in of Zipkin tracing code (#430). --- include/envoy/http/header_map.h | 7 +- source/CMakeLists.txt | 4 + source/common/http/conn_manager_impl.cc | 1 + source/common/http/headers.h | 5 + source/common/json/config_schemas.cc | 2 +- source/exe/CMakeLists.txt | 31 +++ source/zipkin/CMakeLists.txt | 14 ++ source/zipkin/span_buffer.cc | 19 ++ source/zipkin/span_buffer.h | 34 +++ source/zipkin/span_context.cc | 105 +++++++++ source/zipkin/span_context.h | 53 +++++ source/zipkin/tracer.cc | 142 ++++++++++++ source/zipkin/tracer.h | 57 +++++ source/zipkin/tracer_interface.h | 16 ++ source/zipkin/util.cc | 66 ++++++ source/zipkin/util.h | 27 +++ source/zipkin/zipkin_core_constants.cc | 54 +++++ source/zipkin/zipkin_core_constants.h | 38 ++++ source/zipkin/zipkin_core_types.cc | 250 +++++++++++++++++++++ source/zipkin/zipkin_core_types.h | 273 +++++++++++++++++++++++ source/zipkin/zipkin_json_field_names.cc | 25 +++ source/zipkin/zipkin_json_field_names.h | 33 +++ test/CMakeLists.txt | 207 +++++++++++++++++ 23 files changed, 1461 insertions(+), 2 deletions(-) create mode 100755 source/CMakeLists.txt create mode 100755 source/exe/CMakeLists.txt create mode 100755 source/zipkin/CMakeLists.txt create mode 100755 source/zipkin/span_buffer.cc create mode 100755 source/zipkin/span_buffer.h create mode 100755 source/zipkin/span_context.cc create mode 100755 source/zipkin/span_context.h create mode 100755 source/zipkin/tracer.cc create mode 100755 source/zipkin/tracer.h create mode 100755 source/zipkin/tracer_interface.h create mode 100755 source/zipkin/util.cc create mode 100755 source/zipkin/util.h create mode 100755 source/zipkin/zipkin_core_constants.cc create mode 100755 source/zipkin/zipkin_core_constants.h create mode 100755 source/zipkin/zipkin_core_types.cc create mode 100755 source/zipkin/zipkin_core_types.h create mode 100755 source/zipkin/zipkin_json_field_names.cc create mode 100755 source/zipkin/zipkin_json_field_names.h create mode 100755 test/CMakeLists.txt diff --git a/include/envoy/http/header_map.h b/include/envoy/http/header_map.h index 447853f8a1248..5ddf36a130139 100644 --- a/include/envoy/http/header_map.h +++ b/include/envoy/http/header_map.h @@ -227,7 +227,12 @@ class HeaderEntry { HEADER_FUNC(Status) \ HEADER_FUNC(TransferEncoding) \ HEADER_FUNC(Upgrade) \ - HEADER_FUNC(UserAgent) + HEADER_FUNC(UserAgent) \ + HEADER_FUNC(XB3TraceId) \ + HEADER_FUNC(XB3SpanId) \ + HEADER_FUNC(XB3ParentSpanId) \ + HEADER_FUNC(XB3Sampled) \ + HEADER_FUNC(XB3Flags) /** * The following functions are defined for each inline header above. E.g., for ContentLength we diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt new file mode 100755 index 0000000000000..88c9fae4edb88 --- /dev/null +++ b/source/CMakeLists.txt @@ -0,0 +1,4 @@ +add_subdirectory(common) +add_subdirectory(exe) +add_subdirectory(server) +add_subdirectory(zipkin) diff --git a/source/common/http/conn_manager_impl.cc b/source/common/http/conn_manager_impl.cc index f4d08c8e8d94d..22226e2c4e881 100644 --- a/source/common/http/conn_manager_impl.cc +++ b/source/common/http/conn_manager_impl.cc @@ -462,6 +462,7 @@ void ConnectionManagerImpl::ActiveStream::decodeHeaders(HeaderMapPtr&& headers, connection_manager_.config_.tracingStats()); if (tracing_decision.is_tracing) { + stream_log_debug("XXX Fabio: About to call startSpan", *this); active_span_ = connection_manager_.tracer_.startSpan(*this, *request_headers_, request_info_); } } diff --git a/source/common/http/headers.h b/source/common/http/headers.h index 6c1e438abf637..731217ed106fc 100644 --- a/source/common/http/headers.h +++ b/source/common/http/headers.h @@ -56,6 +56,11 @@ class Headers { const LowerCaseString TransferEncoding{"transfer-encoding"}; const LowerCaseString Upgrade{"upgrade"}; const LowerCaseString UserAgent{"user-agent"}; + const LowerCaseString XB3TraceId{"x-b3-traceid"}; + const LowerCaseString XB3SpanId{"x-b3-spanid"}; + const LowerCaseString XB3ParentSpanId{"x-b3-parentspanid"}; + const LowerCaseString XB3Sampled{"x-b3-sampled"}; + const LowerCaseString XB3Flags{"x-b3-flags"}; struct { const std::string Close{"close"}; diff --git a/source/common/json/config_schemas.cc b/source/common/json/config_schemas.cc index bf757a7fed409..846e4bb59f0e0 100644 --- a/source/common/json/config_schemas.cc +++ b/source/common/json/config_schemas.cc @@ -939,7 +939,7 @@ const std::string Json::Schema::TOP_LEVEL_CONFIG_SCHEMA(R"EOF( "properties" : { "type" : { "type" : "string", - "enum" : ["lightstep"] + "enum" : ["lightstep", "zipkin"] }, "config" : { "type" : "object", diff --git a/source/exe/CMakeLists.txt b/source/exe/CMakeLists.txt new file mode 100755 index 0000000000000..3c84add4cc039 --- /dev/null +++ b/source/exe/CMakeLists.txt @@ -0,0 +1,31 @@ +add_executable(envoy hot_restart.cc main.cc $ $ $ ${ENVOY_EXE_EXTRA_OBJECTS}) + +if (ENVOY_TCMALLOC) + target_link_libraries(envoy tcmalloc_and_profiler) +endif() + +target_link_libraries(envoy event) +target_link_libraries(envoy event_pthreads) +target_link_libraries(envoy http_parser) +target_link_libraries(envoy ssl) +target_link_libraries(envoy crypto) +target_link_libraries(envoy nghttp2) +target_link_libraries(envoy lightstep_core_cxx11) +target_link_libraries(envoy cares) +target_link_libraries(envoy protobuf) +target_link_libraries(envoy pthread) +target_link_libraries(envoy anl) +target_link_libraries(envoy rt) +target_link_libraries(envoy dl) + +include_directories(SYSTEM ${ENVOY_OPENSSL_INCLUDE_DIR}) +include_directories(SYSTEM ${ENVOY_LIGHTSTEP_TRACER_INCLUDE_DIR}) +include_directories(SYSTEM ${ENVOY_CARES_INCLUDE_DIR}) + +if (ENVOY_STRIP) + add_custom_command(TARGET envoy POST_BUILD + COMMAND objcopy --only-keep-debug $ $.dbg + COMMAND strip $ + COMMAND objcopy --add-gnu-debuglink=$.dbg $ + ) +endif() diff --git a/source/zipkin/CMakeLists.txt b/source/zipkin/CMakeLists.txt new file mode 100755 index 0000000000000..e381e7c37aab1 --- /dev/null +++ b/source/zipkin/CMakeLists.txt @@ -0,0 +1,14 @@ +add_library( + zipkin OBJECT + tracer.cc + util.cc + zipkin_core_constants.cc + zipkin_core_types.cc + zipkin_json_field_names.cc + span_context.cc + span_buffer.cc +) + +include_directories(${ENVOY_RAPIDJSON_INCLUDE_DIR}) + +cotire(zipkin) diff --git a/source/zipkin/span_buffer.cc b/source/zipkin/span_buffer.cc new file mode 100755 index 0000000000000..e22b0b11fa1b6 --- /dev/null +++ b/source/zipkin/span_buffer.cc @@ -0,0 +1,19 @@ +#include "span_buffer.h" + +namespace Zipkin { + +bool SpanBuffer::addSpan(Span&& span) { + if (next_position_ == size_) { + // Buffer full + return false; + } + span_buffer_[next_position_++] = &span; + + return true; +} + +void SpanBuffer::flush() { + next_position_ = 0; + std::fill(span_buffer_.begin(), span_buffer_.end(), nullptr); +} +} // Zipkin diff --git a/source/zipkin/span_buffer.h b/source/zipkin/span_buffer.h new file mode 100755 index 0000000000000..d2008552c3cb7 --- /dev/null +++ b/source/zipkin/span_buffer.h @@ -0,0 +1,34 @@ +#pragma once + +#include +#include + +#include "zipkin_core_types.h" + +namespace Zipkin { + +typedef Span* SpanPtr; + +class SpanBuffer { +public: + SpanBuffer() : size_(0), next_position_(0) {} + + SpanBuffer(uint64_t size) : span_buffer_(size, nullptr), size_(size), next_position_(0) {} + + virtual ~SpanBuffer() {} + + void allocateBuffer(uint64_t size) { span_buffer_.resize(size); } + + bool addSpan(Span&& span); + + void flush(); + + uint64_t pendingSpans() { return next_position_; } + +private: + // We use a pre-allocated vector to improve performance + std::vector span_buffer_; + uint64_t size_; + uint64_t next_position_; +}; +} // Zipkin diff --git a/source/zipkin/span_context.cc b/source/zipkin/span_context.cc new file mode 100755 index 0000000000000..40777cffbf6de --- /dev/null +++ b/source/zipkin/span_context.cc @@ -0,0 +1,105 @@ +#include +#include +#include + +#include "zipkin_core_constants.h" +#include "span_context.h" + +namespace Zipkin { + +const std::string SpanContext::FIELD_SEPARATOR_ = ";"; + +SpanContext::SpanContext(const Span& span) { + trace_id_ = span.traceId(); + id_ = span.id(); + parent_id_ = span.parentId(); + + for (Annotation ann : span.annotations()) { + if (ann.value() == ZipkinCoreConstants::CLIENT_RECV) { + annotation_values_.cr = true; + } + if (ann.value() == ZipkinCoreConstants::CLIENT_SEND) { + annotation_values_.cs = true; + } + if (ann.value() == ZipkinCoreConstants::SERVER_RECV) { + annotation_values_.sr = true; + } + if (ann.value() == ZipkinCoreConstants::SERVER_SEND) { + annotation_values_.ss = true; + } + } + + is_populated_ = true; +} + +const std::string SpanContext::serializeToString() { + if (!is_populated_) { + return ""; + } + + std::string s; + s = traceIdAsHexString() + FIELD_SEPARATOR_ + idAsHexString() + FIELD_SEPARATOR_ + + parentIdAsHexString(); + + if (annotation_values_.cr) { + s += FIELD_SEPARATOR_ + ZipkinCoreConstants::CLIENT_RECV; + } + if (annotation_values_.cs) { + s += FIELD_SEPARATOR_ + ZipkinCoreConstants::CLIENT_SEND; + } + if (annotation_values_.sr) { + s += FIELD_SEPARATOR_ + ZipkinCoreConstants::SERVER_RECV; + } + if (annotation_values_.ss) { + s += FIELD_SEPARATOR_ + ZipkinCoreConstants::SERVER_SEND; + } + + return s; +} + +void SpanContext::populateFromString(std::string s) { + // ^([0-9,a-z]{16});([0-9,a-z]{16});([0-9,a-z]{16})((;(cs|sr|cr|ss))*)$ + std::string hex_digit_group = "([0-9,a-z]{16})"; + std::string reg_ex_str = + "^" + hex_digit_group + FIELD_SEPARATOR_ + hex_digit_group + FIELD_SEPARATOR_ + + hex_digit_group + "((" + FIELD_SEPARATOR_ + "(" + ZipkinCoreConstants::CLIENT_SEND + "|" + + ZipkinCoreConstants::SERVER_RECV + "|" + ZipkinCoreConstants::CLIENT_RECV + "|" + + ZipkinCoreConstants::SERVER_SEND + "))*)$"; + + std::regex re(reg_ex_str); + std::smatch match; + + trace_id_ = parent_id_ = id_ = 0; + annotation_values_.cs = annotation_values_.cr = annotation_values_.ss = annotation_values_.sr = + false; + + if (std::regex_search(s, match, re)) { + // This is a valid string encoding of the context + trace_id_ = stoull(match.str(1), nullptr, 16); + id_ = stoull(match.str(2), nullptr, 16); + parent_id_ = stoull(match.str(3), nullptr, 16); + + if (match.str(4).size() > 0) { + char* annotation_value_strings = const_cast(match.str(4).c_str()); + char* annotation_value = strtok(annotation_value_strings, FIELD_SEPARATOR_.c_str()); + while (annotation_value) { + if (!strcmp(annotation_value, ZipkinCoreConstants::CLIENT_RECV.c_str())) { + annotation_values_.cr = true; + } else if (!strcmp(annotation_value, ZipkinCoreConstants::CLIENT_SEND.c_str())) { + annotation_values_.cs = true; + } else if (!strcmp(annotation_value, ZipkinCoreConstants::SERVER_RECV.c_str())) { + annotation_values_.sr = true; + } else if (!strcmp(annotation_value, ZipkinCoreConstants::SERVER_SEND.c_str())) { + annotation_values_.ss = true; + } + + annotation_value = strtok(NULL, FIELD_SEPARATOR_.c_str()); + } + } + + is_populated_ = true; + } else { + is_populated_ = false; + } +} +} // Zipkin diff --git a/source/zipkin/span_context.h b/source/zipkin/span_context.h new file mode 100755 index 0000000000000..c651cdd28781f --- /dev/null +++ b/source/zipkin/span_context.h @@ -0,0 +1,53 @@ +#pragma once + +#include +#include + +#include "zipkin_core_types.h" +#include "util.h" + +namespace Zipkin { + +typedef struct Annotation_isset_ { + Annotation_isset_() : cs(false), cr(false), ss(false), sr(false) {} + bool cs : 1; + bool cr : 1; + bool ss : 1; + bool sr : 1; +} Annotation_isset_; + +class SpanContext { +public: + SpanContext() : trace_id_(0), id_(0), parent_id_(0), is_populated_(false) {} + + SpanContext(const Span& span); + + virtual ~SpanContext() {} + + const std::string serializeToString(); + + void populateFromString(std::string s); + + uint64_t id() const { return id_; } + + std::string idAsHexString() const { return Util::uint64ToBase16(id_); } + + uint64_t parent_id() const { return parent_id_; } + + std::string parentIdAsHexString() const { return Util::uint64ToBase16(parent_id_); } + + uint64_t trace_id() const { return trace_id_; } + + std::string traceIdAsHexString() const { return Util::uint64ToBase16(trace_id_); } + + Annotation_isset_ isSetAnnotation() const { return annotation_values_; } + +private: + const static std::string FIELD_SEPARATOR_; + uint64_t trace_id_; + uint64_t id_; + uint64_t parent_id_; + Annotation_isset_ annotation_values_; + bool is_populated_; +}; +} // Zipkin diff --git a/source/zipkin/tracer.cc b/source/zipkin/tracer.cc new file mode 100755 index 0000000000000..c7687ea722023 --- /dev/null +++ b/source/zipkin/tracer.cc @@ -0,0 +1,142 @@ +#include + +#include "util.h" +#include "tracer.h" +#include "zipkin_core_constants.h" + +namespace Zipkin { + +Span Tracer::startSpan(const std::string& operation_name, uint64_t start_time) { + Span span; + Annotation cs; + std::string ip; + uint16_t port; + Endpoint ep; + uint64_t timestampMicro; + + // Build the endpoint + getIPAndPort(address_, ip, port); + ep.setIpv4(ip); + ep.setPort(port); + ep.setServiceName(service_name_); + + // Build the CS annotation + cs.setHost(ep); + cs.setValue(ZipkinCoreConstants::CLIENT_SEND); + + // Create an all-new span, with no parent id + span.setName(operation_name); + uint64_t randonNumber = Util::generateRandom64(); + span.setId(randonNumber); + span.setTraceId(randonNumber); + span.setStartTime(start_time); + + // Set the timestamp globally for the span and also for the CS annotation + timestampMicro = Util::timeSinceEpochMicro(); + cs.setTimestamp(timestampMicro); + span.setTimestamp(timestampMicro); + + // Add CS annotation to the span + span.addAnnotation(cs); + + std::cerr << "Span's tracer pointer before: " << span.tracer() << std::endl; + span.setTracer(this); + std::cerr << "Span's tracer pointer after: " << span.tracer() << std::endl; + + return span; +} + +Span Tracer::startSpan(const std::string& operation_name, uint64_t start_time, + SpanContext& previous_context) { + Span span; + Annotation annotation; + std::string ip; + uint16_t port; + Endpoint ep; + uint64_t timestampMicro; + + // TODO We currently ignore the start_time to set the span/annotation timestamps + // Is start_time really needed? + timestampMicro = Util::timeSinceEpochMicro(); + + if ((previous_context.isSetAnnotation().sr) && (!previous_context.isSetAnnotation().cs)) { + // We need to create a new span that is a child of the previous span; no shared context + + // Create a new span id + uint64_t randonNumber = Util::generateRandom64(); + span.setId(randonNumber); + + // Set the parent id to the id of the previous span + span.setParentId(previous_context.id()); + + // Set the CS annotation value + annotation.setValue(ZipkinCoreConstants::CLIENT_SEND); + + // Set the timestamp globally for the span + span.setTimestamp(timestampMicro); + } else if ((previous_context.isSetAnnotation().cs) && (!previous_context.isSetAnnotation().sr)) { + // We need to create a new span that will share context with the previous span + + // Initialize the shared context for the new span + span.setId(previous_context.id()); + if (previous_context.parent_id()) { + span.setParentId(previous_context.parent_id()); + } + + // Set the SR annotation value + annotation.setValue(ZipkinCoreConstants::SERVER_RECV); + } else { + // Unexpected condition + + // TODO Log an error + return span; // return an empty span + } + + // Build the endpoint + getIPAndPort(address_, ip, port); + ep.setIpv4(ip); + ep.setPort(port); + ep.setServiceName(service_name_); + + // Add the newly-created annotation to the span + annotation.setHost(ep); + annotation.setTimestamp(timestampMicro); + span.addAnnotation(annotation); + + // Keep the same trace id + span.setTraceId(previous_context.trace_id()); + + span.setName(operation_name); + span.setStartTime(start_time); + + std::cerr << "Span's tracer pointer before: " << span.tracer() << std::endl; + span.setTracer(this); + std::cerr << "Span's tracer pointer after: " << span.tracer() << std::endl; + + return span; +} + +void Tracer::reportSpan(Span&& span) { + std::cerr << "Tracer::reportSpan() called." << std::endl; + auto r = reporter(); + if (r) { + std::cerr << "Will call Reporter::reportSpan()" << std::endl; + r->reportSpan(std::move(span)); + } +} + +void Tracer::setReporter(std::unique_ptr reporter) { + reporter_ = std::shared_ptr(std::move(reporter)); +} + +void Tracer::getIPAndPort(const std::string& address, std::string& ip, uint16_t& port) { + std::regex re("^(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})(:(\\d+))?$"); + std::smatch match; + if (std::regex_search(address, match, re)) { + ip = match.str(1); + if (match.str(3).size() > 0) { + port = std::stoi(match.str(3)); + } + } +} +} // Zipkin diff --git a/source/zipkin/tracer.h b/source/zipkin/tracer.h new file mode 100755 index 0000000000000..3547d726ff0c7 --- /dev/null +++ b/source/zipkin/tracer.h @@ -0,0 +1,57 @@ +#pragma once + +#include "zipkin_core_types.h" +#include "span_context.h" +#include "tracer_interface.h" + +namespace Zipkin { + +/** + * Abstract class for Tracer users to implement their own span-buffer management policies + */ +class Reporter { +public: + virtual ~Reporter() {} + + /** + * Span-buffer management policy to be implemented by users of the Trace class + */ + virtual void reportSpan(Span&& span) = 0; +}; + +class Tracer : public TracerInterface { +public: + Tracer(const std::string& service_name, const std::string& address) + : service_name_(service_name), address_(address) {} + + virtual ~Tracer() {} + + /** + * Creates a "root" span + */ + Span startSpan(const std::string& operation_name, uint64_t start_time); + + /** + * Based on the given context, creates either a "child" or a "shared-context" span + */ + Span startSpan(const std::string& operation_name, uint64_t start_time, + SpanContext& previous_context); + + /** + * Called when the Span is finished + */ + void reportSpan(Span&& span) override; + + std::shared_ptr reporter() { return reporter_; } + + void setReporter(std::unique_ptr reporter); + +private: + void getIPAndPort(const std::string& address, std::string& ip, uint16_t& port); + + std::string service_name_; + std::string address_; + + std::shared_ptr reporter_; +}; +} // Zipkin diff --git a/source/zipkin/tracer_interface.h b/source/zipkin/tracer_interface.h new file mode 100755 index 0000000000000..afb344125013b --- /dev/null +++ b/source/zipkin/tracer_interface.h @@ -0,0 +1,16 @@ +#pragma once + +namespace Zipkin { + +class Span; + +class TracerInterface { +public: + virtual ~TracerInterface() {} + + virtual void reportSpan(Span&& span) = 0; +}; + +typedef TracerInterface* TracerPtr; + +} // Zipkin diff --git a/source/zipkin/util.cc b/source/zipkin/util.cc new file mode 100755 index 0000000000000..f0e5bf4f24910 --- /dev/null +++ b/source/zipkin/util.cc @@ -0,0 +1,66 @@ +#include +#include +#include +#include + +#include "rapidjson/writer.h" +#include "rapidjson/stringbuffer.h" +#include "rapidjson/document.h" + +#include "util.h" + +namespace Zipkin { + +void Util::mergeJsons(std::string& target, const std::string& source, + const std::string& field_name) { + rapidjson::Document targetDoc, sourceDoc; + targetDoc.Parse(target.c_str()); + sourceDoc.Parse(source.c_str()); + + targetDoc.AddMember(rapidjson::StringRef(field_name.c_str()), sourceDoc, + targetDoc.GetAllocator()); + + rapidjson::StringBuffer sb; + rapidjson::Writer w(sb); + targetDoc.Accept(w); + target = sb.GetString(); +} + +void Util::addArrayToJson(std::string& target, const std::vector& json_array, + const std::string& field_name) { + std::string stringifiedJsonArray = "["; + + if (json_array.size() > 0) { + stringifiedJsonArray += *(json_array[0]); + for (auto it = json_array.begin() + 1; it != json_array.end(); it++) { + stringifiedJsonArray += ","; + stringifiedJsonArray += **it; + } + } + stringifiedJsonArray += "]"; + + mergeJsons(target, stringifiedJsonArray, field_name); +} + +uint64_t Util::timeSinceEpochMicro() { + return std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()).count(); +} + +uint64_t Util::timeSinceEpochNano() { + return std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()).count(); +} + +uint64_t Util::generateRandom64() { + uint64_t seed = timeSinceEpochNano(); + std::mt19937_64 rand_64(seed); + return rand_64(); +} + +std::string Util::uint64ToBase16(uint64_t value) { + std::stringstream stream; + stream << std::setfill('0') << std::setw(16) << std::hex << value; + return stream.str(); +} +} // Zipkin diff --git a/source/zipkin/util.h b/source/zipkin/util.h new file mode 100755 index 0000000000000..4abbcd9ae0d09 --- /dev/null +++ b/source/zipkin/util.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include + +namespace Zipkin { + +typedef std::chrono::system_clock Duration; + +class Util { +public: + static void mergeJsons(std::string& target, const std::string& source, + const std::string& field_name); + + static void addArrayToJson(std::string& target, const std::vector& json_array, + const std::string& field_name); + + static uint64_t timeSinceEpochMicro(); + + static uint64_t timeSinceEpochNano(); + + static uint64_t generateRandom64(); + + static std::string uint64ToBase16(uint64_t value); +}; +} // Zipkin diff --git a/source/zipkin/zipkin_core_constants.cc b/source/zipkin/zipkin_core_constants.cc new file mode 100755 index 0000000000000..8d33f21cc6c7e --- /dev/null +++ b/source/zipkin/zipkin_core_constants.cc @@ -0,0 +1,54 @@ +#include "zipkin_core_constants.h" + +const std::string Zipkin::ZipkinCoreConstants::CLIENT_SEND = "cs"; + +const std::string Zipkin::ZipkinCoreConstants::CLIENT_RECV = "cr"; + +const std::string Zipkin::ZipkinCoreConstants::SERVER_SEND = "ss"; + +const std::string Zipkin::ZipkinCoreConstants::SERVER_RECV = "sr"; + +const std::string Zipkin::ZipkinCoreConstants::WIRE_SEND = "ws"; + +const std::string Zipkin::ZipkinCoreConstants::WIRE_RECV = "wr"; + +const std::string Zipkin::ZipkinCoreConstants::CLIENT_SEND_FRAGMENT = "csf"; + +const std::string Zipkin::ZipkinCoreConstants::CLIENT_RECV_FRAGMENT = "crf"; + +const std::string Zipkin::ZipkinCoreConstants::SERVER_SEND_FRAGMENT = "ssf"; + +const std::string Zipkin::ZipkinCoreConstants::SERVER_RECV_FRAGMENT = "srf"; + +const std::string Zipkin::ZipkinCoreConstants::HTTP_HOST = "http.host"; + +const std::string Zipkin::ZipkinCoreConstants::HTTP_METHOD = "http.method"; + +const std::string Zipkin::ZipkinCoreConstants::HTTP_PATH = "http.path"; + +const std::string Zipkin::ZipkinCoreConstants::HTTP_URL = "http.url"; + +const std::string Zipkin::ZipkinCoreConstants::HTTP_STATUS_CODE = "http.status_code"; + +const std::string Zipkin::ZipkinCoreConstants::HTTP_REQUEST_SIZE = "http.request.size"; + +const std::string Zipkin::ZipkinCoreConstants::HTTP_RESPONSE_SIZE = "http.response.size"; + +const std::string Zipkin::ZipkinCoreConstants::LOCAL_COMPONENT = "lc"; + +const std::string Zipkin::ZipkinCoreConstants::ERROR = "error"; + +const std::string Zipkin::ZipkinCoreConstants::CLIENT_ADDR = "ca"; + +const std::string Zipkin::ZipkinCoreConstants::SERVER_ADDR = "sa"; + +// Zipkin B3 headers +const std::string Zipkin::ZipkinCoreConstants::X_B3_TRACE_ID = "X-B3-TraceId"; + +const std::string Zipkin::ZipkinCoreConstants::X_B3_SPAN_ID = "X-B3-SpanId"; + +const std::string Zipkin::ZipkinCoreConstants::X_B3_PARENT_SPAN_ID = "X-B3-ParentSpanId"; + +const std::string Zipkin::ZipkinCoreConstants::X_B3_SAMPLED = "X-B3-Sampled"; + +const std::string Zipkin::ZipkinCoreConstants::X_B3_FLAGS = "X-B3-Flags"; diff --git a/source/zipkin/zipkin_core_constants.h b/source/zipkin/zipkin_core_constants.h new file mode 100755 index 0000000000000..9021aa286c0f4 --- /dev/null +++ b/source/zipkin/zipkin_core_constants.h @@ -0,0 +1,38 @@ +#pragma once + +#include + +namespace Zipkin { + +class ZipkinCoreConstants { +public: + static const std::string CLIENT_SEND; + static const std::string CLIENT_RECV; + static const std::string SERVER_SEND; + static const std::string SERVER_RECV; + static const std::string WIRE_SEND; + static const std::string WIRE_RECV; + static const std::string CLIENT_SEND_FRAGMENT; + static const std::string CLIENT_RECV_FRAGMENT; + static const std::string SERVER_SEND_FRAGMENT; + static const std::string SERVER_RECV_FRAGMENT; + static const std::string HTTP_HOST; + static const std::string HTTP_METHOD; + static const std::string HTTP_PATH; + static const std::string HTTP_URL; + static const std::string HTTP_STATUS_CODE; + static const std::string HTTP_REQUEST_SIZE; + static const std::string HTTP_RESPONSE_SIZE; + static const std::string LOCAL_COMPONENT; + static const std::string ERROR; + static const std::string CLIENT_ADDR; + static const std::string SERVER_ADDR; + + // Zipkin B3 Headers + static const std::string X_B3_TRACE_ID; + static const std::string X_B3_SPAN_ID; + static const std::string X_B3_PARENT_SPAN_ID; + static const std::string X_B3_SAMPLED; + static const std::string X_B3_FLAGS; +}; +} // Zipkin diff --git a/source/zipkin/zipkin_core_types.cc b/source/zipkin/zipkin_core_types.cc new file mode 100755 index 0000000000000..7a5d181fb1648 --- /dev/null +++ b/source/zipkin/zipkin_core_types.cc @@ -0,0 +1,250 @@ +#include "rapidjson/writer.h" +#include "rapidjson/stringbuffer.h" + +#include "zipkin_core_types.h" +#include "zipkin_core_constants.h" +#include "zipkin_json_field_names.h" +#include "util.h" +#include "span_context.h" + +#include + +namespace Zipkin { + +Endpoint::Endpoint(const Endpoint& ep) { + ipv4_ = ep.ipv4(); + port_ = ep.port(); + service_name_ = ep.serviceName(); + ipv6_ = ep.ipv6(); + isset_ipv6_ = ep.isSetIpv6(); +} + +Endpoint& Endpoint::operator=(const Endpoint& ep) { + ipv4_ = ep.ipv4(); + port_ = ep.port(); + service_name_ = ep.serviceName(); + ipv6_ = ep.ipv6(); + isset_ipv6_ = ep.isSetIpv6(); + + return *this; +} + +const std::string& Endpoint::toJson() { + rapidjson::StringBuffer s; + rapidjson::Writer writer(s); + writer.StartObject(); + writer.Key(ZipkinJsonFieldNames::ENDPOINT_IPV4.c_str()); + writer.String(ipv4_.c_str()); + writer.Key(ZipkinJsonFieldNames::ENDPOINT_PORT.c_str()); + writer.Uint(port_); + writer.Key(ZipkinJsonFieldNames::ENDPOINT_SERVICE_NAME.c_str()); + writer.String(service_name_.c_str()); + if (isset_ipv6_) { + writer.Key(ZipkinJsonFieldNames::ENDPOINT_IPV6.c_str()); + writer.String(ipv6_.c_str()); + } + writer.EndObject(); + json_string_ = s.GetString(); + + return json_string_; +} + +Annotation::Annotation(const Annotation& ann) { + timestamp_ = ann.timestamp(); + value_ = ann.value(); + host_ = ann.host(); + isset_host_ = ann.isSetHost(); +} + +Annotation& Annotation::operator=(const Annotation& ann) { + timestamp_ = ann.timestamp(); + value_ = ann.value(); + host_ = ann.host(); + isset_host_ = ann.isSetHost(); + + return *this; +} + +const std::string& Annotation::toJson() { + rapidjson::StringBuffer s; + rapidjson::Writer writer(s); + writer.StartObject(); + writer.Key(ZipkinJsonFieldNames::ANNOTATION_TIMESTAMP.c_str()); + writer.Uint64(timestamp_); + writer.Key(ZipkinJsonFieldNames::ANNOTATION_VALUE.c_str()); + writer.String(value_.c_str()); + writer.EndObject(); + + json_string_ = s.GetString(); + + if (isset_host_) { + Util::mergeJsons(json_string_, host_.toJson(), + ZipkinJsonFieldNames::ANNOTATION_ENDPOINT.c_str()); + } + + return json_string_; +} + +BinaryAnnotation::BinaryAnnotation(const BinaryAnnotation& ann) { + key_ = ann.key(); + value_ = ann.value(); + annotation_type_ = ann.annotationType(); + host_ = ann.host(); + isset_host_ = ann.isSetHost(); +} + +BinaryAnnotation& BinaryAnnotation::operator=(const BinaryAnnotation& ann) { + key_ = ann.key(); + value_ = ann.value(); + annotation_type_ = ann.annotationType(); + host_ = ann.host(); + isset_host_ = ann.isSetHost(); + + return *this; +} + +const std::string& BinaryAnnotation::toJson() { + rapidjson::StringBuffer s; + rapidjson::Writer writer(s); + writer.StartObject(); + writer.Key(ZipkinJsonFieldNames::BINARY_ANNOTATION_KEY.c_str()); + writer.String(key_.c_str()); + writer.Key(ZipkinJsonFieldNames::BINARY_ANNOTATION_VALUE.c_str()); + writer.String(value_.c_str()); + writer.EndObject(); + + json_string_ = s.GetString(); + + if (isset_host_) { + Util::mergeJsons(json_string_, host_.toJson(), + ZipkinJsonFieldNames::BINARY_ANNOTATION_ENDPOINT.c_str()); + } + + return json_string_; +} + +Span::Span(const Span& span) { + trace_id_ = span.traceId(); + name_ = span.name(); + id_ = span.id(); + parent_id_ = span.parentId(); + annotations_ = span.annotations(); + binary_annotations_ = span.binaryAnnotations(); + debug_ = span.isDebug(); + timestamp_ = span.timestamp(); + duration_ = span.duration(); + trace_id_high_ = span.traceIdHigh(); + isset_ = span.isSet(); + start_time_ = span.startTime(); + tracer_ = span.tracer(); +} + +Span& Span::operator=(const Span& span) { + trace_id_ = span.traceId(); + name_ = span.name(); + id_ = span.id(); + parent_id_ = span.parentId(); + annotations_ = span.annotations(); + binary_annotations_ = span.binaryAnnotations(); + debug_ = span.isDebug(); + timestamp_ = span.timestamp(); + duration_ = span.duration(); + trace_id_high_ = span.traceIdHigh(); + isset_ = span.isSet(); + start_time_ = span.startTime(); + tracer_ = span.tracer(); + + return *this; +} + +const std::string& Span::toJson() { + rapidjson::StringBuffer s; + rapidjson::Writer writer(s); + writer.StartObject(); + writer.Key(ZipkinJsonFieldNames::SPAN_TRACE_ID.c_str()); + writer.String(Util::uint64ToBase16(trace_id_).c_str()); + writer.Key(ZipkinJsonFieldNames::SPAN_NAME.c_str()); + writer.String(name_.c_str()); + writer.Key(ZipkinJsonFieldNames::SPAN_ID.c_str()); + writer.String(Util::uint64ToBase16(id_).c_str()); + + if (isset_.parent_id && parent_id_) { + writer.Key(ZipkinJsonFieldNames::SPAN_PARENT_ID.c_str()); + writer.String(Util::uint64ToBase16(parent_id_).c_str()); + } + + if (isset_.timestamp) { + writer.Key(ZipkinJsonFieldNames::SPAN_TIMESTAMP.c_str()); + writer.Int64(timestamp_); + } + + if (isset_.duration) { + writer.Key(ZipkinJsonFieldNames::SPAN_DURATION.c_str()); + writer.Int64(duration_); + } + + if (isset_.debug) { + writer.Key(ZipkinJsonFieldNames::SPAN_DEBUG.c_str()); + writer.Bool(debug_); + } + + if (isset_.trace_id_high) { + writer.Key(ZipkinJsonFieldNames::SPAN_TRACE_ID_HIGH.c_str()); + writer.Int64(trace_id_high_); + } + + writer.EndObject(); + + json_string_ = s.GetString(); + + std::vector annotation_json_vector; + + for (auto it = annotations_.begin(); it != annotations_.end(); it++) { + annotation_json_vector.push_back(&(it->toJson())); + } + Util::addArrayToJson(json_string_, annotation_json_vector, + ZipkinJsonFieldNames::SPAN_ANNOTATIONS.c_str()); + + std::vector binary_annotation_json_vector; + for (auto it = binary_annotations_.begin(); it != binary_annotations_.end(); it++) { + binary_annotation_json_vector.push_back(&(it->toJson())); + } + Util::addArrayToJson(json_string_, binary_annotation_json_vector, + ZipkinJsonFieldNames::SPAN_BINARY_ANNOTATIONS.c_str()); + + return json_string_; +} + +void Span::finish() { + // Assumption: Span will have only one annotation when this method is called + SpanContext context(*this); + if ((context.isSetAnnotation().sr) && (!context.isSetAnnotation().ss)) { + // Need to set the SS annotation + Annotation ss; + ss.setHost(annotations_[0].host()); + ss.setTimestamp(Util::timeSinceEpochMicro()); + ss.setValue(ZipkinCoreConstants::SERVER_SEND); + annotations_.push_back(ss); + } else if ((context.isSetAnnotation().cs) && (!context.isSetAnnotation().cr)) { + // Need to set the CR annotation + Annotation cr; + uint64_t stop_time = Util::timeSinceEpochMicro(); + cr.setHost(annotations_[0].host()); + cr.setTimestamp(stop_time); + cr.setValue(ZipkinCoreConstants::CLIENT_RECV); + annotations_.push_back(cr); + setDuration(stop_time - timestamp_); + } + + std::cerr << "Span: " << toJson() << std::endl; + auto t = tracer(); + if (t) { + std::cerr << "Will call Tracer::reportSpan" << std::endl; + t->reportSpan(std::move(*this)); + } +} + +void Span::setTag(const std::string& name, const std::string& value) { + std::cerr << "NO-OP setTag called --> Name: " << name << "; Value: " << value << std::endl; +} +} // Zipkin diff --git a/source/zipkin/zipkin_core_types.h b/source/zipkin/zipkin_core_types.h new file mode 100755 index 0000000000000..b248c89254cc9 --- /dev/null +++ b/source/zipkin/zipkin_core_types.h @@ -0,0 +1,273 @@ +#pragma once + +#include +#include +#include + +#include "util.h" +#include "tracer_interface.h" + +namespace Zipkin { + +class ZipkinBase { +public: + virtual ~ZipkinBase() {} + + virtual const std::string& toJson() = 0; + +protected: + std::string json_string_; +}; + +class Endpoint : public ZipkinBase { +public: + Endpoint(const Endpoint&); + + Endpoint& operator=(const Endpoint&); + + Endpoint() : ipv4_(), port_(0), service_name_(), isset_ipv6_(false) {} + + Endpoint(std::string& ipv4, uint16_t port, const std::string& service_name) + : ipv4_(ipv4), port_(port), service_name_(service_name), isset_ipv6_(false) {} + + const std::string& ipv4() const { return ipv4_; } + + void setIpv4(std::string& ipv4) { ipv4_ = ipv4; } + + const std::string& ipv6() const { return ipv6_; } + + void setIpv6(const std::string& ipv6) { + ipv6_ = ipv6; + isset_ipv6_ = true; + } + + bool isSetIpv6() const { return isset_ipv6_; } + + uint16_t port() const { return port_; } + + void setPort(uint16_t port) { port_ = port; } + + const std::string& serviceName() const { return service_name_; } + + void setServiceName(const std::string& service_name) { service_name_ = service_name; } + + const std::string& toJson() override; + +private: + std::string ipv4_; + uint16_t port_; + std::string service_name_; + std::string ipv6_; + + bool isset_ipv6_; +}; + +class Annotation : public ZipkinBase { +public: + Annotation(const Annotation&); + + Annotation& operator=(const Annotation&); + + Annotation() : timestamp_(0), value_(), isset_host_(false) {} + + Annotation(uint64_t timestamp, const std::string value, Endpoint& host) + : timestamp_(timestamp), value_(value), host_(host), isset_host_(true) {} + + const Endpoint& host() const { return host_; } + + void setHost(const Endpoint& host) { + host_ = host; + isset_host_ = true; + } + + uint64_t timestamp() const { return timestamp_; } + + void setTimestamp(uint64_t timestamp) { timestamp_ = timestamp; } + + const std::string& value() const { return value_; } + + void setValue(const std::string& value) { value_ = value; } + + bool isSetHost() const { return isset_host_; } + + const std::string& toJson() override; + +private: + uint64_t timestamp_; + std::string value_; + Endpoint host_; + + bool isset_host_; +}; + +enum class AnnotationType { + BOOL = 0, + BYTES = 1, + I16 = 2, + I32 = 3, + I64 = 4, + DOUBLE = 5, + STRING = 6 +}; + +class BinaryAnnotation : public ZipkinBase { +public: + BinaryAnnotation(const BinaryAnnotation&); + + BinaryAnnotation& operator=(const BinaryAnnotation&); + + BinaryAnnotation() : key_(), value_(), annotation_type_(), isset_host_(false) {} + + AnnotationType annotationType() const { return annotation_type_; } + + void setAnnotationType(AnnotationType annotationType) { annotation_type_ = annotationType; } + + const Endpoint& host() const { return host_; } + + void setHost(const Endpoint& host) { + host_ = host; + isset_host_ = true; + } + + bool isSetHost() const { return isset_host_; } + + const std::string& key() const { return key_; } + + void setKey(const std::string& key) { key_ = key; } + + const std::string& value() const { return value_; } + + void setValue(const std::string& value) { value_ = value; } + + const std::string& toJson() override; + +private: + std::string key_; + std::string value_; + Endpoint host_; + + AnnotationType annotation_type_; + + bool isset_host_; +}; + +typedef struct _Span__isset { + _Span__isset() + : parent_id(false), debug(false), timestamp(false), duration(false), trace_id_high(false) {} + bool parent_id : 1; + bool debug : 1; + bool timestamp : 1; + bool duration : 1; + bool trace_id_high : 1; +} _Span__isset; + +class Span : public ZipkinBase { +public: + Span(const Span&); + + Span& operator=(const Span&); + + Span() + : trace_id_(0), name_(), id_(0), parent_id_(0), debug_(false), timestamp_(0), duration_(0), + trace_id_high_(0), start_time_(0), tracer_(nullptr) {} + + void setTraceId(const uint64_t val) { trace_id_ = val; } + + void setName(const std::string& val) { name_ = val; } + + void setId(const uint64_t val) { id_ = val; } + + void setParentId(const uint64_t val) { + parent_id_ = val; + isset_.parent_id = true; + } + + void setAannotations(const std::vector& val) { annotations_ = val; } + + void addAnnotation(const Annotation& ann) { annotations_.push_back(ann); } + + void setBinaryAnnotations(const std::vector& val) { binary_annotations_ = val; } + + void addBinaryAnnotation(const BinaryAnnotation& bann) { binary_annotations_.push_back(bann); } + + void setDebug(const bool val) { + debug_ = val; + isset_.debug = true; + } + + void setTimestamp(const int64_t val) { + timestamp_ = val; + isset_.timestamp = true; + } + + void setDuration(const int64_t val) { + duration_ = val; + isset_.duration = true; + } + + void setTraceIdHigh(const int64_t val) { + trace_id_high_ = val; + isset_.trace_id_high = true; + } + + void setStartTime(const int64_t time) { start_time_ = time; } + + const std::vector& annotations() const { return annotations_; } + + const std::vector& binaryAnnotations() const { return binary_annotations_; } + + bool isDebug() const { return debug_; } + + int64_t duration() const { return duration_; } + + uint64_t id() const { return id_; } + + std::string idAsHexString() const { return Util::uint64ToBase16(id_); } + + const _Span__isset& isSet() const { return isset_; } + + const std::string& name() const { return name_; } + + uint64_t parentId() const { return parent_id_; } + + std::string parentIdAsHexString() const { return Util::uint64ToBase16(parent_id_); } + + int64_t timestamp() const { return timestamp_; } + + uint64_t traceId() const { return trace_id_; } + + std::string traceIdAsHexString() const { return Util::uint64ToBase16(trace_id_); } + + uint64_t traceIdHigh() const { return trace_id_high_; } + + int64_t startTime() const { return start_time_; } + + const std::string& toJson() override; + + void setTracer(TracerPtr tracer) { tracer_ = tracer; } + + TracerPtr tracer() const { return tracer_; } + + void finish(); + + void setTag(const std::string& name, const std::string& value); + +private: + uint64_t trace_id_; + std::string name_; + uint64_t id_; + uint64_t parent_id_; + std::vector annotations_; + std::vector binary_annotations_; + bool debug_; + int64_t timestamp_; + int64_t duration_; + uint64_t trace_id_high_; + + int64_t start_time_; + + TracerPtr tracer_; + + _Span__isset isset_; +}; +} // Zipkin diff --git a/source/zipkin/zipkin_json_field_names.cc b/source/zipkin/zipkin_json_field_names.cc new file mode 100755 index 0000000000000..828aa8beb2459 --- /dev/null +++ b/source/zipkin/zipkin_json_field_names.cc @@ -0,0 +1,25 @@ +#include "zipkin_json_field_names.h" + +const std::string Zipkin::ZipkinJsonFieldNames::SPAN_TRACE_ID = "traceId"; +const std::string Zipkin::ZipkinJsonFieldNames::SPAN_TRACE_ID_HIGH = "traceIdHigh"; +const std::string Zipkin::ZipkinJsonFieldNames::SPAN_PARENT_ID = "parentId"; +const std::string Zipkin::ZipkinJsonFieldNames::SPAN_NAME = "name"; +const std::string Zipkin::ZipkinJsonFieldNames::SPAN_ID = "id"; +const std::string Zipkin::ZipkinJsonFieldNames::SPAN_TIMESTAMP = "timestamp"; +const std::string Zipkin::ZipkinJsonFieldNames::SPAN_DURATION = "duration"; +const std::string Zipkin::ZipkinJsonFieldNames::SPAN_DEBUG = "debug"; +const std::string Zipkin::ZipkinJsonFieldNames::SPAN_ANNOTATIONS = "annotations"; +const std::string Zipkin::ZipkinJsonFieldNames::SPAN_BINARY_ANNOTATIONS = "binaryAnnotations"; + +const std::string Zipkin::ZipkinJsonFieldNames::ANNOTATION_ENDPOINT = "endpoint"; +const std::string Zipkin::ZipkinJsonFieldNames::ANNOTATION_TIMESTAMP = "timestamp"; +const std::string Zipkin::ZipkinJsonFieldNames::ANNOTATION_VALUE = "value"; + +const std::string Zipkin::ZipkinJsonFieldNames::BINARY_ANNOTATION_ENDPOINT = "endpoint"; +const std::string Zipkin::ZipkinJsonFieldNames::BINARY_ANNOTATION_KEY = "key"; +const std::string Zipkin::ZipkinJsonFieldNames::BINARY_ANNOTATION_VALUE = "value"; + +const std::string Zipkin::ZipkinJsonFieldNames::ENDPOINT_SERVICE_NAME = "serviceName"; +const std::string Zipkin::ZipkinJsonFieldNames::ENDPOINT_PORT = "port"; +const std::string Zipkin::ZipkinJsonFieldNames::ENDPOINT_IPV4 = "ipv4"; +const std::string Zipkin::ZipkinJsonFieldNames::ENDPOINT_IPV6 = "ipv6"; diff --git a/source/zipkin/zipkin_json_field_names.h b/source/zipkin/zipkin_json_field_names.h new file mode 100755 index 0000000000000..01cd36fc4a570 --- /dev/null +++ b/source/zipkin/zipkin_json_field_names.h @@ -0,0 +1,33 @@ +#pragma once + +#include + +namespace Zipkin { + +class ZipkinJsonFieldNames { +public: + static const std::string SPAN_TRACE_ID; + static const std::string SPAN_TRACE_ID_HIGH; + static const std::string SPAN_PARENT_ID; + static const std::string SPAN_NAME; + static const std::string SPAN_ID; + static const std::string SPAN_TIMESTAMP; + static const std::string SPAN_DURATION; + static const std::string SPAN_DEBUG; + static const std::string SPAN_ANNOTATIONS; + static const std::string SPAN_BINARY_ANNOTATIONS; + + static const std::string ANNOTATION_ENDPOINT; + static const std::string ANNOTATION_TIMESTAMP; + static const std::string ANNOTATION_VALUE; + + static const std::string BINARY_ANNOTATION_ENDPOINT; + static const std::string BINARY_ANNOTATION_KEY; + static const std::string BINARY_ANNOTATION_VALUE; + + static const std::string ENDPOINT_SERVICE_NAME; + static const std::string ENDPOINT_PORT; + static const std::string ENDPOINT_IPV4; + static const std::string ENDPOINT_IPV6; +}; +} // Zipkin diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100755 index 0000000000000..e3aed234ff533 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,207 @@ +set(CMAKE_EXE_LINKER_FLAGS "${ENVOY_TEST_EXTRA_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS}") + +# TODO: Generic function for building protos +file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/generated) +add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated/helloworld.pb.h ${CMAKE_CURRENT_BINARY_DIR}/generated/helloworld.pb.cc + COMMAND ${ENVOY_PROTOBUF_PROTOC} -I=${CMAKE_CURRENT_SOURCE_DIR}/proto/ + --cpp_out=${CMAKE_CURRENT_BINARY_DIR}/generated + ${CMAKE_CURRENT_SOURCE_DIR}/proto/helloworld.proto + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/proto/helloworld.proto +) + +set_source_files_properties(generated/helloworld.pb.cc PROPERTIES COMPILE_FLAGS -Wno-unused-parameter) + +include_directories(${ENVOY_GMOCK_INCLUDE_DIR}) +include_directories(${ENVOY_GTEST_INCLUDE_DIR}) +include_directories(SYSTEM ${ENVOY_HTTP_PARSER_INCLUDE_DIR}) +include_directories(${PROJECT_SOURCE_DIR}) +include_directories(${PROJECT_BINARY_DIR}) +include_directories(SYSTEM ${ENVOY_OPENSSL_INCLUDE_DIR}) +include_directories(${ENVOY_NGHTTP2_INCLUDE_DIR}) +include_directories(SYSTEM ${ENVOY_CARES_INCLUDE_DIR}) +include_directories(SYSTEM ${ENVOY_LIGHTSTEP_TRACER_INCLUDE_DIR}) +include_directories(${ENVOY_LIBEVENT_INCLUDE_DIR}) + +add_executable(envoy-test + $ + $ + $ + ${ENVOY_TEST_EXTRA_OBJECTS} + common/access_log/access_log_manager_impl_test.cc + common/api/api_impl_test.cc + common/common/base64_test.cc + common/common/hex_test.cc + common/common/optional_test.cc + common/common/utility_test.cc + common/event/dispatcher_impl_test.cc + common/event/file_event_impl_test.cc + common/filesystem/filesystem_impl_test.cc + common/filesystem/watcher_impl_test.cc + common/grpc/codec_test.cc + common/grpc/common_test.cc + common/grpc/http1_bridge_filter_test.cc + common/grpc/rpc_channel_impl_test.cc + common/http/access_log/access_log_formatter_test.cc + common/http/access_log/access_log_impl_test.cc + common/http/async_client_impl_test.cc + common/http/codec_client_test.cc + common/http/codes_test.cc + common/http/common.cc + common/http/conn_manager_impl_test.cc + common/http/conn_manager_utility_test.cc + common/http/date_provider_impl_test.cc + common/http/filter/buffer_filter_test.cc + common/http/filter/fault_filter_test.cc + common/http/filter/ratelimit_test.cc + common/http/header_map_impl_test.cc + common/http/http1/codec_impl_test.cc + common/http/http1/conn_pool_test.cc + common/http/http2/codec_impl_test.cc + common/http/http2/conn_pool_test.cc + common/http/user_agent_test.cc + common/http/utility_test.cc + common/filter/auth/client_ssl_test.cc + common/filter/ratelimit_test.cc + common/filter/tcp_proxy_test.cc + common/dynamo/dynamo_filter_test.cc + common/dynamo/dynamo_request_parser_test.cc + common/dynamo/dynamo_utility_test.cc + common/json/json_loader_test.cc + common/mongo/bson_impl_test.cc + common/mongo/codec_impl_test.cc + common/mongo/proxy_test.cc + common/mongo/utility_test.cc + common/network/address_impl_test.cc + common/network/cidr_range_test.cc + common/network/connection_impl_test.cc + common/network/dns_impl_test.cc + common/network/filter_manager_impl_test.cc + common/network/listener_impl_test.cc + common/network/listen_socket_impl_test.cc + common/network/proxy_protocol_test.cc + common/network/utility_test.cc + common/ratelimit/ratelimit_impl_test.cc + common/redis/codec_impl_test.cc + common/redis/command_splitter_impl_test.cc + common/redis/conn_pool_impl_test.cc + common/redis/proxy_filter_test.cc + common/router/config_impl_test.cc + common/router/rds_impl_test.cc + common/router/retry_state_impl_test.cc + common/router/router_test.cc + common/router/router_ratelimit_test.cc + common/router/shadow_writer_impl_test.cc + common/runtime/runtime_impl_test.cc + common/runtime/uuid_util_test.cc + common/ssl/connection_impl_test.cc + common/ssl/context_impl_test.cc + common/stats/stats_impl_test.cc + common/stats/statsd_test.cc + common/stats/thread_local_store_test.cc + common/tracing/http_tracer_impl_test.cc + common/tracing/lightstep_tracer_impl_test.cc + common/upstream/cds_api_impl_test.cc + common/upstream/cluster_manager_impl_test.cc + common/upstream/health_checker_impl_test.cc + common/upstream/host_utility_test.cc + common/upstream/load_balancer_impl_test.cc + common/upstream/load_balancer_simulation_test.cc + common/upstream/logical_dns_cluster_test.cc + common/upstream/outlier_detection_impl_test.cc + common/upstream/resource_manager_impl_test.cc + common/upstream/ring_hash_lb_test.cc + common/upstream/sds_test.cc + common/upstream/upstream_impl_test.cc + generated/helloworld.pb.cc + integration/fake_upstream.cc + integration/http2_integration_test.cc + integration/http2_upstream_integration_test.cc + integration/integration.cc + integration/integration_test.cc + integration/integration_admin_test.cc + integration/proxy_proto_integration_test.cc + integration/ssl_integration_test.cc + integration/uds_integration_test.cc + integration/server.cc + integration/utility.cc + main.cc + mocks/access_log/mocks.cc + mocks/api/mocks.cc + mocks/common.cc + mocks/event/mocks.cc + mocks/filesystem/mocks.cc + mocks/grpc/mocks.cc + mocks/http/mocks.cc + mocks/init/mocks.cc + mocks/local_info/mocks.cc + mocks/network/mocks.cc + mocks/ratelimit/mocks.cc + mocks/redis/mocks.cc + mocks/router/mocks.cc + mocks/runtime/mocks.cc + mocks/server/mocks.cc + mocks/ssl/mocks.cc + mocks/stats/mocks.cc + mocks/thread_local/mocks.cc + mocks/tracing/mocks.cc + mocks/upstream/mocks.cc + server/config/http/config_test.cc + server/config/network/config_test.cc + server/config/network/http_connection_manager_test.cc + server/configuration_impl_test.cc + server/connection_handler_test.cc + server/drain_manager_impl_test.cc + server/guarddog_impl_test.cc + server/http/admin_test.cc + server/http/health_check_test.cc + server/options_impl_test.cc + server/server_test.cc + test_common/environment.cc + test_common/network_utility.cc + test_common/network_utility_test.cc + test_common/printers.cc + test_common/utility.cc) + +# The MOCK_METHOD* macros from gtest triggers this clang warning and it's hard +# to work around, so we just ignore it. +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + set_target_properties(envoy-test PROPERTIES COMPILE_FLAGS "-Wno-inconsistent-missing-override") +endif() + +if (ENVOY_TCMALLOC) + target_link_libraries(envoy-test tcmalloc_and_profiler) +endif() + +target_link_libraries(envoy-test event) +target_link_libraries(envoy-test event_pthreads) +target_link_libraries(envoy-test http_parser) +target_link_libraries(envoy-test ssl) +target_link_libraries(envoy-test crypto) +target_link_libraries(envoy-test nghttp2) +target_link_libraries(envoy-test lightstep_core_cxx11) +target_link_libraries(envoy-test cares) +target_link_libraries(envoy-test protobuf) +target_link_libraries(envoy-test gmock) +target_link_libraries(envoy-test pthread) +target_link_libraries(envoy-test anl) +target_link_libraries(envoy-test dl) + +set(ENVOY_TEST_EXTRA_SETUP_SCRIPT "" CACHE STRING + "extra setup script to call from run_envoy_tests.sh after basic setup") + +add_custom_target( + ${PROJECT_NAME}.check ${PROJECT_SOURCE_DIR}/test/run_envoy_tests.sh ${PROJECT_SOURCE_DIR} + ${PROJECT_BINARY_DIR} ${ENVOY_TEST_EXTRA_SETUP_SCRIPT} + DEPENDS envoy envoy-test +) + +if (ENVOY_CODE_COVERAGE) +add_custom_target( + ${PROJECT_NAME}.check-coverage + DEPENDS envoy envoy-test + COMMAND ${PROJECT_SOURCE_DIR}/test/run_envoy_coverage.sh ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR} + ${PROJECT_SOURCE_DIR} ${PROJECT_BINARY_DIR} ${ENVOY_GCOVR} ${ENVOY_GCOVR_EXTRA_ARGS} + ${ENVOY_TEST_EXTRA_SETUP_SCRIPT} +) +endif() From 3f1225cc3161c99ef7b624b8fe1a88d177489e51 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Wed, 29 Mar 2017 23:33:08 -0400 Subject: [PATCH 02/55] Add span buffer method to generate a JSON payload for posting spans to Zipkin. (#430) --- source/zipkin/span_buffer.cc | 29 ++++++++++++++++++++++++++++- source/zipkin/span_buffer.h | 6 ++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/source/zipkin/span_buffer.cc b/source/zipkin/span_buffer.cc index e22b0b11fa1b6..9edd44513c25a 100755 --- a/source/zipkin/span_buffer.cc +++ b/source/zipkin/span_buffer.cc @@ -1,13 +1,21 @@ #include "span_buffer.h" +#include + namespace Zipkin { +void SpanBuffer::allocateBuffer(uint64_t size) { + span_buffer_.resize(size); + size_ = size; +} + bool SpanBuffer::addSpan(Span&& span) { if (next_position_ == size_) { // Buffer full return false; } - span_buffer_[next_position_++] = &span; + // span_buffer_[next_position_++] = &span; + span_buffer_[next_position_++] = std::make_shared(span); return true; } @@ -16,4 +24,23 @@ void SpanBuffer::flush() { next_position_ = 0; std::fill(span_buffer_.begin(), span_buffer_.end(), nullptr); } + +std::string SpanBuffer::toStringifiedJsonArray() { + std::string stringifiedJsonArray = "["; + + std::cerr << "Inside SpanBuffer::toStringifiedJsonArray()" << std::endl; + + if (pendingSpans()) { + std::cerr << "SpanBuffer::toStringifiedJsonArray() will call span.toJson()" << std::endl; + stringifiedJsonArray += span_buffer_[0]->toJson(); + std::cerr << "SpanBuffer::toStringifiedJsonArray() done with span.toJson()" << std::endl; + for (uint64_t i = 1; i < next_position_; i++) { + stringifiedJsonArray += ","; + stringifiedJsonArray += span_buffer_[i]->toJson(); + } + } + stringifiedJsonArray += "]"; + + return stringifiedJsonArray; +} } // Zipkin diff --git a/source/zipkin/span_buffer.h b/source/zipkin/span_buffer.h index d2008552c3cb7..98408b5238fcc 100755 --- a/source/zipkin/span_buffer.h +++ b/source/zipkin/span_buffer.h @@ -7,7 +7,7 @@ namespace Zipkin { -typedef Span* SpanPtr; +typedef std::shared_ptr SpanPtr; class SpanBuffer { public: @@ -17,7 +17,7 @@ class SpanBuffer { virtual ~SpanBuffer() {} - void allocateBuffer(uint64_t size) { span_buffer_.resize(size); } + void allocateBuffer(uint64_t size); bool addSpan(Span&& span); @@ -25,6 +25,8 @@ class SpanBuffer { uint64_t pendingSpans() { return next_position_; } + std::string toStringifiedJsonArray(); + private: // We use a pre-allocated vector to improve performance std::vector span_buffer_; From 05bf311581a0b2ceca126a442e354874760d9e35 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Wed, 29 Mar 2017 23:40:27 -0400 Subject: [PATCH 03/55] Have ZipkinReporter use Zipkin::SpanBuffer (#430) --- source/zipkin/tracer.cc | 2 ++ source/zipkin/zipkin_core_types.cc | 2 ++ 2 files changed, 4 insertions(+) diff --git a/source/zipkin/tracer.cc b/source/zipkin/tracer.cc index c7687ea722023..74d8a207778e1 100755 --- a/source/zipkin/tracer.cc +++ b/source/zipkin/tracer.cc @@ -4,6 +4,8 @@ #include "tracer.h" #include "zipkin_core_constants.h" +#include + namespace Zipkin { Span Tracer::startSpan(const std::string& operation_name, uint64_t start_time) { diff --git a/source/zipkin/zipkin_core_types.cc b/source/zipkin/zipkin_core_types.cc index 7a5d181fb1648..d94f370b38d72 100755 --- a/source/zipkin/zipkin_core_types.cc +++ b/source/zipkin/zipkin_core_types.cc @@ -197,6 +197,8 @@ const std::string& Span::toJson() { json_string_ = s.GetString(); + std::cerr << "Will convert annotations to JSON" << std::endl; + std::vector annotation_json_vector; for (auto it = annotations_.begin(); it != annotations_.end(); it++) { From 01838f87c048be0cb825728f915844fe7b03e532 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Thu, 30 Mar 2017 21:18:34 -0400 Subject: [PATCH 04/55] Make ZipkinReporter use HttpAsyncClient (#430) --- source/common/json/config_schemas.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/common/json/config_schemas.cc b/source/common/json/config_schemas.cc index 846e4bb59f0e0..9dbf2d5d3e0e1 100644 --- a/source/common/json/config_schemas.cc +++ b/source/common/json/config_schemas.cc @@ -945,9 +945,10 @@ const std::string Json::Schema::TOP_LEVEL_CONFIG_SCHEMA(R"EOF( "type" : "object", "properties" : { "collector_cluster" : {"type" : "string"}, - "access_token_file" : {"type" : "string"} + "access_token_file" : {"type" : "string"}, + "endpoint": {"type": "string"} }, - "required": ["collector_cluster", "access_token_file"], + "required": ["collector_cluster"], "additionalProperties" : false } }, From 40e945e67d2d7d6bb6d1eb00fe3a79f1df78c6cc Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Tue, 4 Apr 2017 14:50:36 -0400 Subject: [PATCH 05/55] Add binary annotations to Zipkin spans, and a few optimizations (#430) --- source/zipkin/tracer.cc | 17 ++++++++-------- source/zipkin/tracer.h | 5 ++--- source/zipkin/zipkin_core_types.cc | 9 ++++++--- source/zipkin/zipkin_core_types.h | 31 ++++++++++++++++++++---------- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/source/zipkin/tracer.cc b/source/zipkin/tracer.cc index 74d8a207778e1..ec88310612ddc 100755 --- a/source/zipkin/tracer.cc +++ b/source/zipkin/tracer.cc @@ -8,7 +8,7 @@ namespace Zipkin { -Span Tracer::startSpan(const std::string& operation_name, uint64_t start_time) { +Span Tracer::startSpan(const std::string& span_name, uint64_t start_time) { Span span; Annotation cs; std::string ip; @@ -23,11 +23,11 @@ Span Tracer::startSpan(const std::string& operation_name, uint64_t start_time) { ep.setServiceName(service_name_); // Build the CS annotation - cs.setHost(ep); + cs.setHost(std::move(ep)); cs.setValue(ZipkinCoreConstants::CLIENT_SEND); // Create an all-new span, with no parent id - span.setName(operation_name); + span.setName(span_name); uint64_t randonNumber = Util::generateRandom64(); span.setId(randonNumber); span.setTraceId(randonNumber); @@ -39,7 +39,7 @@ Span Tracer::startSpan(const std::string& operation_name, uint64_t start_time) { span.setTimestamp(timestampMicro); // Add CS annotation to the span - span.addAnnotation(cs); + span.addAnnotation(std::move(cs)); std::cerr << "Span's tracer pointer before: " << span.tracer() << std::endl; span.setTracer(this); @@ -48,7 +48,7 @@ Span Tracer::startSpan(const std::string& operation_name, uint64_t start_time) { return span; } -Span Tracer::startSpan(const std::string& operation_name, uint64_t start_time, +Span Tracer::startSpan(const std::string& span_name, uint64_t start_time, SpanContext& previous_context) { Span span; Annotation annotation; @@ -68,6 +68,8 @@ Span Tracer::startSpan(const std::string& operation_name, uint64_t start_time, uint64_t randonNumber = Util::generateRandom64(); span.setId(randonNumber); + span.setName(span_name); + // Set the parent id to the id of the previous span span.setParentId(previous_context.id()); @@ -101,14 +103,13 @@ Span Tracer::startSpan(const std::string& operation_name, uint64_t start_time, ep.setServiceName(service_name_); // Add the newly-created annotation to the span - annotation.setHost(ep); + annotation.setHost(std::move(ep)); annotation.setTimestamp(timestampMicro); - span.addAnnotation(annotation); + span.addAnnotation(std::move(annotation)); // Keep the same trace id span.setTraceId(previous_context.trace_id()); - span.setName(operation_name); span.setStartTime(start_time); std::cerr << "Span's tracer pointer before: " << span.tracer() << std::endl; diff --git a/source/zipkin/tracer.h b/source/zipkin/tracer.h index 3547d726ff0c7..0b45780723516 100755 --- a/source/zipkin/tracer.h +++ b/source/zipkin/tracer.h @@ -29,13 +29,12 @@ class Tracer : public TracerInterface { /** * Creates a "root" span */ - Span startSpan(const std::string& operation_name, uint64_t start_time); + Span startSpan(const std::string& span_name, uint64_t start_time); /** * Based on the given context, creates either a "child" or a "shared-context" span */ - Span startSpan(const std::string& operation_name, uint64_t start_time, - SpanContext& previous_context); + Span startSpan(const std::string& span_name, uint64_t start_time, SpanContext& previous_context); /** * Called when the Span is finished diff --git a/source/zipkin/zipkin_core_types.cc b/source/zipkin/zipkin_core_types.cc index d94f370b38d72..675fc5bfb07dd 100755 --- a/source/zipkin/zipkin_core_types.cc +++ b/source/zipkin/zipkin_core_types.cc @@ -226,7 +226,7 @@ void Span::finish() { ss.setHost(annotations_[0].host()); ss.setTimestamp(Util::timeSinceEpochMicro()); ss.setValue(ZipkinCoreConstants::SERVER_SEND); - annotations_.push_back(ss); + annotations_.push_back(std::move(ss)); } else if ((context.isSetAnnotation().cs) && (!context.isSetAnnotation().cr)) { // Need to set the CR annotation Annotation cr; @@ -234,7 +234,7 @@ void Span::finish() { cr.setHost(annotations_[0].host()); cr.setTimestamp(stop_time); cr.setValue(ZipkinCoreConstants::CLIENT_RECV); - annotations_.push_back(cr); + annotations_.push_back(std::move(cr)); setDuration(stop_time - timestamp_); } @@ -247,6 +247,9 @@ void Span::finish() { } void Span::setTag(const std::string& name, const std::string& value) { - std::cerr << "NO-OP setTag called --> Name: " << name << "; Value: " << value << std::endl; + std::cerr << "setTag called --> Name: " << name << "; Value: " << value << std::endl; + if ((name.size() > 0) && (value.size() > 0)) { + addBinaryAnnotation(BinaryAnnotation(name, value)); + } } } // Zipkin diff --git a/source/zipkin/zipkin_core_types.h b/source/zipkin/zipkin_core_types.h index b248c89254cc9..ff861cac35bcf 100755 --- a/source/zipkin/zipkin_core_types.h +++ b/source/zipkin/zipkin_core_types.h @@ -80,6 +80,11 @@ class Annotation : public ZipkinBase { isset_host_ = true; } + void setHost(const Endpoint&& host) { + host_ = host; + isset_host_ = true; + } + uint64_t timestamp() const { return timestamp_; } void setTimestamp(uint64_t timestamp) { timestamp_ = timestamp; } @@ -100,15 +105,7 @@ class Annotation : public ZipkinBase { bool isset_host_; }; -enum class AnnotationType { - BOOL = 0, - BYTES = 1, - I16 = 2, - I32 = 3, - I64 = 4, - DOUBLE = 5, - STRING = 6 -}; +enum AnnotationType { BOOL = 0, BYTES = 1, I16 = 2, I32 = 3, I64 = 4, DOUBLE = 5, STRING = 6 }; class BinaryAnnotation : public ZipkinBase { public: @@ -116,7 +113,10 @@ class BinaryAnnotation : public ZipkinBase { BinaryAnnotation& operator=(const BinaryAnnotation&); - BinaryAnnotation() : key_(), value_(), annotation_type_(), isset_host_(false) {} + BinaryAnnotation() : key_(), value_(), annotation_type_(STRING), isset_host_(false) {} + + BinaryAnnotation(const std::string& key, const std::string& value) + : key_(key), value_(value), annotation_type_(STRING), isset_host_(false) {} AnnotationType annotationType() const { return annotation_type_; } @@ -129,6 +129,11 @@ class BinaryAnnotation : public ZipkinBase { isset_host_ = true; } + void setHost(const Endpoint&& host) { + host_ = host; + isset_host_ = true; + } + bool isSetHost() const { return isset_host_; } const std::string& key() const { return key_; } @@ -182,14 +187,20 @@ class Span : public ZipkinBase { isset_.parent_id = true; } + const std::vector& annotations() { return annotations_; } + void setAannotations(const std::vector& val) { annotations_ = val; } void addAnnotation(const Annotation& ann) { annotations_.push_back(ann); } + void addAnnotation(const Annotation&& ann) { annotations_.push_back(ann); } + void setBinaryAnnotations(const std::vector& val) { binary_annotations_ = val; } void addBinaryAnnotation(const BinaryAnnotation& bann) { binary_annotations_.push_back(bann); } + void addBinaryAnnotation(const BinaryAnnotation&& bann) { binary_annotations_.push_back(bann); } + void setDebug(const bool val) { debug_ = val; isset_.debug = true; From 7e7bf38275fd6159afc5d6ada3da89e40d128229 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Tue, 4 Apr 2017 15:16:14 -0400 Subject: [PATCH 06/55] Fixed file permissions (#430) --- source/CMakeLists.txt | 0 source/exe/CMakeLists.txt | 0 source/zipkin/CMakeLists.txt | 0 source/zipkin/span_buffer.cc | 0 source/zipkin/span_buffer.h | 0 source/zipkin/span_context.cc | 0 source/zipkin/span_context.h | 0 source/zipkin/tracer.cc | 0 source/zipkin/tracer.h | 0 source/zipkin/tracer_interface.h | 0 source/zipkin/util.cc | 0 source/zipkin/util.h | 0 source/zipkin/zipkin_core_constants.cc | 0 source/zipkin/zipkin_core_constants.h | 0 source/zipkin/zipkin_core_types.cc | 0 source/zipkin/zipkin_core_types.h | 0 source/zipkin/zipkin_json_field_names.cc | 0 source/zipkin/zipkin_json_field_names.h | 0 test/CMakeLists.txt | 0 19 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 source/CMakeLists.txt mode change 100755 => 100644 source/exe/CMakeLists.txt mode change 100755 => 100644 source/zipkin/CMakeLists.txt mode change 100755 => 100644 source/zipkin/span_buffer.cc mode change 100755 => 100644 source/zipkin/span_buffer.h mode change 100755 => 100644 source/zipkin/span_context.cc mode change 100755 => 100644 source/zipkin/span_context.h mode change 100755 => 100644 source/zipkin/tracer.cc mode change 100755 => 100644 source/zipkin/tracer.h mode change 100755 => 100644 source/zipkin/tracer_interface.h mode change 100755 => 100644 source/zipkin/util.cc mode change 100755 => 100644 source/zipkin/util.h mode change 100755 => 100644 source/zipkin/zipkin_core_constants.cc mode change 100755 => 100644 source/zipkin/zipkin_core_constants.h mode change 100755 => 100644 source/zipkin/zipkin_core_types.cc mode change 100755 => 100644 source/zipkin/zipkin_core_types.h mode change 100755 => 100644 source/zipkin/zipkin_json_field_names.cc mode change 100755 => 100644 source/zipkin/zipkin_json_field_names.h mode change 100755 => 100644 test/CMakeLists.txt diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/source/exe/CMakeLists.txt b/source/exe/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/source/zipkin/CMakeLists.txt b/source/zipkin/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/source/zipkin/span_buffer.cc b/source/zipkin/span_buffer.cc old mode 100755 new mode 100644 diff --git a/source/zipkin/span_buffer.h b/source/zipkin/span_buffer.h old mode 100755 new mode 100644 diff --git a/source/zipkin/span_context.cc b/source/zipkin/span_context.cc old mode 100755 new mode 100644 diff --git a/source/zipkin/span_context.h b/source/zipkin/span_context.h old mode 100755 new mode 100644 diff --git a/source/zipkin/tracer.cc b/source/zipkin/tracer.cc old mode 100755 new mode 100644 diff --git a/source/zipkin/tracer.h b/source/zipkin/tracer.h old mode 100755 new mode 100644 diff --git a/source/zipkin/tracer_interface.h b/source/zipkin/tracer_interface.h old mode 100755 new mode 100644 diff --git a/source/zipkin/util.cc b/source/zipkin/util.cc old mode 100755 new mode 100644 diff --git a/source/zipkin/util.h b/source/zipkin/util.h old mode 100755 new mode 100644 diff --git a/source/zipkin/zipkin_core_constants.cc b/source/zipkin/zipkin_core_constants.cc old mode 100755 new mode 100644 diff --git a/source/zipkin/zipkin_core_constants.h b/source/zipkin/zipkin_core_constants.h old mode 100755 new mode 100644 diff --git a/source/zipkin/zipkin_core_types.cc b/source/zipkin/zipkin_core_types.cc old mode 100755 new mode 100644 diff --git a/source/zipkin/zipkin_core_types.h b/source/zipkin/zipkin_core_types.h old mode 100755 new mode 100644 diff --git a/source/zipkin/zipkin_json_field_names.cc b/source/zipkin/zipkin_json_field_names.cc old mode 100755 new mode 100644 diff --git a/source/zipkin/zipkin_json_field_names.h b/source/zipkin/zipkin_json_field_names.h old mode 100755 new mode 100644 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt old mode 100755 new mode 100644 From 9cdf72f19f345c929f3d7b7b93b62344d8ab7769 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Wed, 5 Apr 2017 11:12:10 -0400 Subject: [PATCH 07/55] Reverting some files in order to split PR 692 (#430) --- include/envoy/http/header_map.h | 7 +------ source/CMakeLists.txt | 1 - source/common/http/conn_manager_impl.cc | 1 - source/common/http/headers.h | 5 ----- source/common/json/config_schemas.cc | 7 +++---- source/exe/CMakeLists.txt | 2 +- test/CMakeLists.txt | 1 - 7 files changed, 5 insertions(+), 19 deletions(-) diff --git a/include/envoy/http/header_map.h b/include/envoy/http/header_map.h index 5ddf36a130139..447853f8a1248 100644 --- a/include/envoy/http/header_map.h +++ b/include/envoy/http/header_map.h @@ -227,12 +227,7 @@ class HeaderEntry { HEADER_FUNC(Status) \ HEADER_FUNC(TransferEncoding) \ HEADER_FUNC(Upgrade) \ - HEADER_FUNC(UserAgent) \ - HEADER_FUNC(XB3TraceId) \ - HEADER_FUNC(XB3SpanId) \ - HEADER_FUNC(XB3ParentSpanId) \ - HEADER_FUNC(XB3Sampled) \ - HEADER_FUNC(XB3Flags) + HEADER_FUNC(UserAgent) /** * The following functions are defined for each inline header above. E.g., for ContentLength we diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 88c9fae4edb88..52e699aadb358 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -1,4 +1,3 @@ add_subdirectory(common) add_subdirectory(exe) add_subdirectory(server) -add_subdirectory(zipkin) diff --git a/source/common/http/conn_manager_impl.cc b/source/common/http/conn_manager_impl.cc index 22226e2c4e881..f4d08c8e8d94d 100644 --- a/source/common/http/conn_manager_impl.cc +++ b/source/common/http/conn_manager_impl.cc @@ -462,7 +462,6 @@ void ConnectionManagerImpl::ActiveStream::decodeHeaders(HeaderMapPtr&& headers, connection_manager_.config_.tracingStats()); if (tracing_decision.is_tracing) { - stream_log_debug("XXX Fabio: About to call startSpan", *this); active_span_ = connection_manager_.tracer_.startSpan(*this, *request_headers_, request_info_); } } diff --git a/source/common/http/headers.h b/source/common/http/headers.h index 731217ed106fc..6c1e438abf637 100644 --- a/source/common/http/headers.h +++ b/source/common/http/headers.h @@ -56,11 +56,6 @@ class Headers { const LowerCaseString TransferEncoding{"transfer-encoding"}; const LowerCaseString Upgrade{"upgrade"}; const LowerCaseString UserAgent{"user-agent"}; - const LowerCaseString XB3TraceId{"x-b3-traceid"}; - const LowerCaseString XB3SpanId{"x-b3-spanid"}; - const LowerCaseString XB3ParentSpanId{"x-b3-parentspanid"}; - const LowerCaseString XB3Sampled{"x-b3-sampled"}; - const LowerCaseString XB3Flags{"x-b3-flags"}; struct { const std::string Close{"close"}; diff --git a/source/common/json/config_schemas.cc b/source/common/json/config_schemas.cc index 9dbf2d5d3e0e1..bf757a7fed409 100644 --- a/source/common/json/config_schemas.cc +++ b/source/common/json/config_schemas.cc @@ -939,16 +939,15 @@ const std::string Json::Schema::TOP_LEVEL_CONFIG_SCHEMA(R"EOF( "properties" : { "type" : { "type" : "string", - "enum" : ["lightstep", "zipkin"] + "enum" : ["lightstep"] }, "config" : { "type" : "object", "properties" : { "collector_cluster" : {"type" : "string"}, - "access_token_file" : {"type" : "string"}, - "endpoint": {"type": "string"} + "access_token_file" : {"type" : "string"} }, - "required": ["collector_cluster"], + "required": ["collector_cluster", "access_token_file"], "additionalProperties" : false } }, diff --git a/source/exe/CMakeLists.txt b/source/exe/CMakeLists.txt index 3c84add4cc039..0348c0226edf0 100644 --- a/source/exe/CMakeLists.txt +++ b/source/exe/CMakeLists.txt @@ -1,4 +1,4 @@ -add_executable(envoy hot_restart.cc main.cc $ $ $ ${ENVOY_EXE_EXTRA_OBJECTS}) +add_executable(envoy hot_restart.cc main.cc $ $ ${ENVOY_EXE_EXTRA_OBJECTS}) if (ENVOY_TCMALLOC) target_link_libraries(envoy tcmalloc_and_profiler) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e3aed234ff533..9ffe8aab94db6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,7 +26,6 @@ include_directories(${ENVOY_LIBEVENT_INCLUDE_DIR}) add_executable(envoy-test $ $ - $ ${ENVOY_TEST_EXTRA_OBJECTS} common/access_log/access_log_manager_impl_test.cc common/api/api_impl_test.cc From d264083e731024bfed9e8610240d738e1f44c0bc Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Wed, 5 Apr 2017 13:05:14 -0400 Subject: [PATCH 08/55] Changed include statements to conform with Bazel (#430) --- source/zipkin/span_buffer.cc | 2 +- source/zipkin/span_buffer.h | 2 +- source/zipkin/span_context.cc | 4 ++-- source/zipkin/span_context.h | 4 ++-- source/zipkin/tracer.cc | 6 +++--- source/zipkin/tracer.h | 6 +++--- source/zipkin/util.cc | 2 +- source/zipkin/zipkin_core_constants.cc | 24 +----------------------- source/zipkin/zipkin_core_types.cc | 10 +++++----- source/zipkin/zipkin_core_types.h | 4 ++-- source/zipkin/zipkin_json_field_names.cc | 2 +- 11 files changed, 22 insertions(+), 44 deletions(-) diff --git a/source/zipkin/span_buffer.cc b/source/zipkin/span_buffer.cc index 9edd44513c25a..072450d32a08a 100644 --- a/source/zipkin/span_buffer.cc +++ b/source/zipkin/span_buffer.cc @@ -1,4 +1,4 @@ -#include "span_buffer.h" +#include "zipkin/span_buffer.h" #include diff --git a/source/zipkin/span_buffer.h b/source/zipkin/span_buffer.h index 98408b5238fcc..47e3d5c95c408 100644 --- a/source/zipkin/span_buffer.h +++ b/source/zipkin/span_buffer.h @@ -3,7 +3,7 @@ #include #include -#include "zipkin_core_types.h" +#include "zipkin/zipkin_core_types.h" namespace Zipkin { diff --git a/source/zipkin/span_context.cc b/source/zipkin/span_context.cc index 40777cffbf6de..2163efeb17267 100644 --- a/source/zipkin/span_context.cc +++ b/source/zipkin/span_context.cc @@ -2,8 +2,8 @@ #include #include -#include "zipkin_core_constants.h" -#include "span_context.h" +#include "zipkin/zipkin_core_constants.h" +#include "zipkin/span_context.h" namespace Zipkin { diff --git a/source/zipkin/span_context.h b/source/zipkin/span_context.h index c651cdd28781f..ef7a15a36fc97 100644 --- a/source/zipkin/span_context.h +++ b/source/zipkin/span_context.h @@ -3,8 +3,8 @@ #include #include -#include "zipkin_core_types.h" -#include "util.h" +#include "zipkin/zipkin_core_types.h" +#include "zipkin/util.h" namespace Zipkin { diff --git a/source/zipkin/tracer.cc b/source/zipkin/tracer.cc index ec88310612ddc..b6e1050b7a31e 100644 --- a/source/zipkin/tracer.cc +++ b/source/zipkin/tracer.cc @@ -1,8 +1,8 @@ #include -#include "util.h" -#include "tracer.h" -#include "zipkin_core_constants.h" +#include "zipkin/util.h" +#include "zipkin/tracer.h" +#include "zipkin/zipkin_core_constants.h" #include diff --git a/source/zipkin/tracer.h b/source/zipkin/tracer.h index 0b45780723516..6696db574aa55 100644 --- a/source/zipkin/tracer.h +++ b/source/zipkin/tracer.h @@ -1,8 +1,8 @@ #pragma once -#include "zipkin_core_types.h" -#include "span_context.h" -#include "tracer_interface.h" +#include "zipkin/zipkin_core_types.h" +#include "zipkin/span_context.h" +#include "zipkin/tracer_interface.h" namespace Zipkin { diff --git a/source/zipkin/util.cc b/source/zipkin/util.cc index f0e5bf4f24910..5ce4d9bb96a61 100644 --- a/source/zipkin/util.cc +++ b/source/zipkin/util.cc @@ -7,7 +7,7 @@ #include "rapidjson/stringbuffer.h" #include "rapidjson/document.h" -#include "util.h" +#include "zipkin/util.h" namespace Zipkin { diff --git a/source/zipkin/zipkin_core_constants.cc b/source/zipkin/zipkin_core_constants.cc index 8d33f21cc6c7e..eb6c91132c9b0 100644 --- a/source/zipkin/zipkin_core_constants.cc +++ b/source/zipkin/zipkin_core_constants.cc @@ -1,54 +1,32 @@ -#include "zipkin_core_constants.h" +#include "zipkin/zipkin_core_constants.h" const std::string Zipkin::ZipkinCoreConstants::CLIENT_SEND = "cs"; - const std::string Zipkin::ZipkinCoreConstants::CLIENT_RECV = "cr"; - const std::string Zipkin::ZipkinCoreConstants::SERVER_SEND = "ss"; - const std::string Zipkin::ZipkinCoreConstants::SERVER_RECV = "sr"; - const std::string Zipkin::ZipkinCoreConstants::WIRE_SEND = "ws"; - const std::string Zipkin::ZipkinCoreConstants::WIRE_RECV = "wr"; - const std::string Zipkin::ZipkinCoreConstants::CLIENT_SEND_FRAGMENT = "csf"; - const std::string Zipkin::ZipkinCoreConstants::CLIENT_RECV_FRAGMENT = "crf"; - const std::string Zipkin::ZipkinCoreConstants::SERVER_SEND_FRAGMENT = "ssf"; - const std::string Zipkin::ZipkinCoreConstants::SERVER_RECV_FRAGMENT = "srf"; const std::string Zipkin::ZipkinCoreConstants::HTTP_HOST = "http.host"; - const std::string Zipkin::ZipkinCoreConstants::HTTP_METHOD = "http.method"; - const std::string Zipkin::ZipkinCoreConstants::HTTP_PATH = "http.path"; - const std::string Zipkin::ZipkinCoreConstants::HTTP_URL = "http.url"; - const std::string Zipkin::ZipkinCoreConstants::HTTP_STATUS_CODE = "http.status_code"; - const std::string Zipkin::ZipkinCoreConstants::HTTP_REQUEST_SIZE = "http.request.size"; - const std::string Zipkin::ZipkinCoreConstants::HTTP_RESPONSE_SIZE = "http.response.size"; const std::string Zipkin::ZipkinCoreConstants::LOCAL_COMPONENT = "lc"; - const std::string Zipkin::ZipkinCoreConstants::ERROR = "error"; - const std::string Zipkin::ZipkinCoreConstants::CLIENT_ADDR = "ca"; - const std::string Zipkin::ZipkinCoreConstants::SERVER_ADDR = "sa"; // Zipkin B3 headers const std::string Zipkin::ZipkinCoreConstants::X_B3_TRACE_ID = "X-B3-TraceId"; - const std::string Zipkin::ZipkinCoreConstants::X_B3_SPAN_ID = "X-B3-SpanId"; - const std::string Zipkin::ZipkinCoreConstants::X_B3_PARENT_SPAN_ID = "X-B3-ParentSpanId"; - const std::string Zipkin::ZipkinCoreConstants::X_B3_SAMPLED = "X-B3-Sampled"; - const std::string Zipkin::ZipkinCoreConstants::X_B3_FLAGS = "X-B3-Flags"; diff --git a/source/zipkin/zipkin_core_types.cc b/source/zipkin/zipkin_core_types.cc index 675fc5bfb07dd..cf138b0617cfe 100644 --- a/source/zipkin/zipkin_core_types.cc +++ b/source/zipkin/zipkin_core_types.cc @@ -1,11 +1,11 @@ #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" -#include "zipkin_core_types.h" -#include "zipkin_core_constants.h" -#include "zipkin_json_field_names.h" -#include "util.h" -#include "span_context.h" +#include "zipkin/zipkin_core_types.h" +#include "zipkin/zipkin_core_constants.h" +#include "zipkin/zipkin_json_field_names.h" +#include "zipkin/util.h" +#include "zipkin/span_context.h" #include diff --git a/source/zipkin/zipkin_core_types.h b/source/zipkin/zipkin_core_types.h index ff861cac35bcf..d126b1aa63056 100644 --- a/source/zipkin/zipkin_core_types.h +++ b/source/zipkin/zipkin_core_types.h @@ -4,8 +4,8 @@ #include #include -#include "util.h" -#include "tracer_interface.h" +#include "zipkin/util.h" +#include "zipkin/tracer_interface.h" namespace Zipkin { diff --git a/source/zipkin/zipkin_json_field_names.cc b/source/zipkin/zipkin_json_field_names.cc index 828aa8beb2459..20e1967478b1d 100644 --- a/source/zipkin/zipkin_json_field_names.cc +++ b/source/zipkin/zipkin_json_field_names.cc @@ -1,4 +1,4 @@ -#include "zipkin_json_field_names.h" +#include "zipkin/zipkin_json_field_names.h" const std::string Zipkin::ZipkinJsonFieldNames::SPAN_TRACE_ID = "traceId"; const std::string Zipkin::ZipkinJsonFieldNames::SPAN_TRACE_ID_HIGH = "traceIdHigh"; From aeb15f56e17360bc00d68a87874d08bafd437c3b Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Wed, 5 Apr 2017 13:40:24 -0400 Subject: [PATCH 09/55] Added BUILD file for Bazel (#430) --- source/zipkin/BUILD | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 source/zipkin/BUILD diff --git a/source/zipkin/BUILD b/source/zipkin/BUILD new file mode 100644 index 0000000000000..9449af3504b6e --- /dev/null +++ b/source/zipkin/BUILD @@ -0,0 +1,28 @@ +package(default_visibility = ["//visibility:public"]) + +load("//bazel:envoy_build_system.bzl", "envoy_cc_library") + +envoy_cc_library( + name = "zipkin_lib", + srcs = [ + "zipkin_core_constants.cc", + "zipkin_core_types.cc", + "zipkin_json_field_names.cc", + "util.cc", + "tracer.cc", + "span_context.cc", + "span_buffer.cc" + ], + hdrs = [ + "zipkin_core_constants.h", + "zipkin_core_types.h", + "zipkin_json_field_names.h", + "util.h", + "tracer_interface.h", + "tracer.h", + "span_context.h", + "span_buffer.h" + ], + external_deps = ["rapidjson"], + deps = [], +) From 7e9fc9764b44716e7b7637ba3a3b7efc3a4e2675 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Wed, 5 Apr 2017 14:27:28 -0400 Subject: [PATCH 10/55] Reformatted CMakelists.txt and BUILD - clang (#430) --- source/zipkin/BUILD | 46 ++++++++++++++++-------------------- source/zipkin/CMakeLists.txt | 16 ++++--------- 2 files changed, 25 insertions(+), 37 deletions(-) diff --git a/source/zipkin/BUILD b/source/zipkin/BUILD index 9449af3504b6e..f1515b7bb8f1a 100644 --- a/source/zipkin/BUILD +++ b/source/zipkin/BUILD @@ -1,28 +1,24 @@ package(default_visibility = ["//visibility:public"]) -load("//bazel:envoy_build_system.bzl", "envoy_cc_library") + load("//bazel:envoy_build_system.bzl", "envoy_cc_library") -envoy_cc_library( - name = "zipkin_lib", - srcs = [ - "zipkin_core_constants.cc", - "zipkin_core_types.cc", - "zipkin_json_field_names.cc", - "util.cc", - "tracer.cc", - "span_context.cc", - "span_buffer.cc" - ], - hdrs = [ - "zipkin_core_constants.h", - "zipkin_core_types.h", - "zipkin_json_field_names.h", - "util.h", - "tracer_interface.h", - "tracer.h", - "span_context.h", - "span_buffer.h" - ], - external_deps = ["rapidjson"], - deps = [], -) + envoy_cc_library(name = "zipkin_lib", srcs = [ + "zipkin_core_constants.cc", + "zipkin_core_types.cc", + "zipkin_json_field_names.cc", + "util.cc", + "tracer.cc", + "span_context.cc", + "span_buffer.cc" + ], + hdrs = [ + "zipkin_core_constants.h", + "zipkin_core_types.h", + "zipkin_json_field_names.h", + "util.h", + "tracer_interface.h", + "tracer.h", + "span_context.h", + "span_buffer.h" + ], + external_deps = ["rapidjson"], deps = [], ) diff --git a/source/zipkin/CMakeLists.txt b/source/zipkin/CMakeLists.txt index e381e7c37aab1..1a80f527cd199 100644 --- a/source/zipkin/CMakeLists.txt +++ b/source/zipkin/CMakeLists.txt @@ -1,14 +1,6 @@ -add_library( - zipkin OBJECT - tracer.cc - util.cc - zipkin_core_constants.cc - zipkin_core_types.cc - zipkin_json_field_names.cc - span_context.cc - span_buffer.cc -) +add_library(zipkin OBJECT tracer.cc util.cc zipkin_core_constants.cc zipkin_core_types + .cc zipkin_json_field_names.cc span_context.cc span_buffer.cc) -include_directories(${ENVOY_RAPIDJSON_INCLUDE_DIR}) + include_directories(${ENVOY_RAPIDJSON_INCLUDE_DIR}) -cotire(zipkin) + cotire(zipkin) From 4a5d9012a3c6a3441723fe6f39f6719781d37b88 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Wed, 5 Apr 2017 14:47:07 -0400 Subject: [PATCH 11/55] Reformatted BUILD and CMakeLists.txt (#430) --- source/zipkin/BUILD | 46 ++++++++++++++++++++---------------- source/zipkin/CMakeLists.txt | 16 +++++++++---- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/source/zipkin/BUILD b/source/zipkin/BUILD index f1515b7bb8f1a..1dd85c3b1f62b 100644 --- a/source/zipkin/BUILD +++ b/source/zipkin/BUILD @@ -1,24 +1,28 @@ package(default_visibility = ["//visibility:public"]) - load("//bazel:envoy_build_system.bzl", "envoy_cc_library") +load("//bazel:envoy_build_system.bzl", "envoy_cc_library") - envoy_cc_library(name = "zipkin_lib", srcs = [ - "zipkin_core_constants.cc", - "zipkin_core_types.cc", - "zipkin_json_field_names.cc", - "util.cc", - "tracer.cc", - "span_context.cc", - "span_buffer.cc" - ], - hdrs = [ - "zipkin_core_constants.h", - "zipkin_core_types.h", - "zipkin_json_field_names.h", - "util.h", - "tracer_interface.h", - "tracer.h", - "span_context.h", - "span_buffer.h" - ], - external_deps = ["rapidjson"], deps = [], ) +envoy_cc_library( + name = "zipkin_lib", + srcs = [ + "span_buffer.cc", + "span_context.cc", + "tracer.cc", + "util.cc", + "zipkin_core_constants.cc", + "zipkin_core_types.cc", + "zipkin_json_field_names.cc", + ], + hdrs = [ + "span_buffer.h", + "span_context.h", + "tracer.h", + "tracer_interface.h", + "util.h", + "zipkin_core_constants.h", + "zipkin_core_types.h", + "zipkin_json_field_names.h", + ], + external_deps = ["rapidjson"], + deps = [], +) diff --git a/source/zipkin/CMakeLists.txt b/source/zipkin/CMakeLists.txt index 1a80f527cd199..e381e7c37aab1 100644 --- a/source/zipkin/CMakeLists.txt +++ b/source/zipkin/CMakeLists.txt @@ -1,6 +1,14 @@ -add_library(zipkin OBJECT tracer.cc util.cc zipkin_core_constants.cc zipkin_core_types - .cc zipkin_json_field_names.cc span_context.cc span_buffer.cc) +add_library( + zipkin OBJECT + tracer.cc + util.cc + zipkin_core_constants.cc + zipkin_core_types.cc + zipkin_json_field_names.cc + span_context.cc + span_buffer.cc +) - include_directories(${ENVOY_RAPIDJSON_INCLUDE_DIR}) +include_directories(${ENVOY_RAPIDJSON_INCLUDE_DIR}) - cotire(zipkin) +cotire(zipkin) From 9f0e96e9efe5db4d2da80dc3de6cf33870fac9fb Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Thu, 6 Apr 2017 09:23:06 -0400 Subject: [PATCH 12/55] Removed empty deps array from BUILD file (#430) --- source/zipkin/BUILD | 1 - 1 file changed, 1 deletion(-) diff --git a/source/zipkin/BUILD b/source/zipkin/BUILD index 1dd85c3b1f62b..da08b730888ec 100644 --- a/source/zipkin/BUILD +++ b/source/zipkin/BUILD @@ -24,5 +24,4 @@ envoy_cc_library( "zipkin_json_field_names.h", ], external_deps = ["rapidjson"], - deps = [], ) From 4ae612ceffa88023e6a2edbef8a482117b13460a Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Thu, 6 Apr 2017 16:21:08 -0400 Subject: [PATCH 13/55] Added first batch of test cases for the zipkin library (#430) --- source/CMakeLists.txt | 1 + source/exe/CMakeLists.txt | 2 +- source/zipkin/span_buffer.cc | 6 -- source/zipkin/span_context.cc | 2 +- source/zipkin/zipkin_core_types.cc | 2 - test/CMakeLists.txt | 6 +- test/zipkin/span_buffer_test.cc | 100 +++++++++++++++++++ test/zipkin/span_context_test.cc | 152 +++++++++++++++++++++++++++++ test/zipkin/util_test.cc | 46 +++++++++ 9 files changed, 306 insertions(+), 11 deletions(-) create mode 100755 test/zipkin/span_buffer_test.cc create mode 100755 test/zipkin/span_context_test.cc create mode 100755 test/zipkin/util_test.cc diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 52e699aadb358..88c9fae4edb88 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(common) add_subdirectory(exe) add_subdirectory(server) +add_subdirectory(zipkin) diff --git a/source/exe/CMakeLists.txt b/source/exe/CMakeLists.txt index 0348c0226edf0..3c84add4cc039 100644 --- a/source/exe/CMakeLists.txt +++ b/source/exe/CMakeLists.txt @@ -1,4 +1,4 @@ -add_executable(envoy hot_restart.cc main.cc $ $ ${ENVOY_EXE_EXTRA_OBJECTS}) +add_executable(envoy hot_restart.cc main.cc $ $ $ ${ENVOY_EXE_EXTRA_OBJECTS}) if (ENVOY_TCMALLOC) target_link_libraries(envoy tcmalloc_and_profiler) diff --git a/source/zipkin/span_buffer.cc b/source/zipkin/span_buffer.cc index 072450d32a08a..fe8ccf035acad 100644 --- a/source/zipkin/span_buffer.cc +++ b/source/zipkin/span_buffer.cc @@ -1,7 +1,5 @@ #include "zipkin/span_buffer.h" -#include - namespace Zipkin { void SpanBuffer::allocateBuffer(uint64_t size) { @@ -28,12 +26,8 @@ void SpanBuffer::flush() { std::string SpanBuffer::toStringifiedJsonArray() { std::string stringifiedJsonArray = "["; - std::cerr << "Inside SpanBuffer::toStringifiedJsonArray()" << std::endl; - if (pendingSpans()) { - std::cerr << "SpanBuffer::toStringifiedJsonArray() will call span.toJson()" << std::endl; stringifiedJsonArray += span_buffer_[0]->toJson(); - std::cerr << "SpanBuffer::toStringifiedJsonArray() done with span.toJson()" << std::endl; for (uint64_t i = 1; i < next_position_; i++) { stringifiedJsonArray += ","; stringifiedJsonArray += span_buffer_[i]->toJson(); diff --git a/source/zipkin/span_context.cc b/source/zipkin/span_context.cc index 2163efeb17267..d9576a2de88d2 100644 --- a/source/zipkin/span_context.cc +++ b/source/zipkin/span_context.cc @@ -34,7 +34,7 @@ SpanContext::SpanContext(const Span& span) { const std::string SpanContext::serializeToString() { if (!is_populated_) { - return ""; + return "0000000000000000;0000000000000000;0000000000000000"; } std::string s; diff --git a/source/zipkin/zipkin_core_types.cc b/source/zipkin/zipkin_core_types.cc index cf138b0617cfe..ce7bf3678f946 100644 --- a/source/zipkin/zipkin_core_types.cc +++ b/source/zipkin/zipkin_core_types.cc @@ -197,8 +197,6 @@ const std::string& Span::toJson() { json_string_ = s.GetString(); - std::cerr << "Will convert annotations to JSON" << std::endl; - std::vector annotation_json_vector; for (auto it = annotations_.begin(); it != annotations_.end(); it++) { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9ffe8aab94db6..1efd8dd9723f2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,6 +26,7 @@ include_directories(${ENVOY_LIBEVENT_INCLUDE_DIR}) add_executable(envoy-test $ $ + $ ${ENVOY_TEST_EXTRA_OBJECTS} common/access_log/access_log_manager_impl_test.cc common/api/api_impl_test.cc @@ -160,7 +161,10 @@ add_executable(envoy-test test_common/network_utility.cc test_common/network_utility_test.cc test_common/printers.cc - test_common/utility.cc) + test_common/utility.cc + zipkin/span_buffer_test.cc + zipkin/span_context_test.cc + zipkin/util_test.cc) # The MOCK_METHOD* macros from gtest triggers this clang warning and it's hard # to work around, so we just ignore it. diff --git a/test/zipkin/span_buffer_test.cc b/test/zipkin/span_buffer_test.cc new file mode 100755 index 0000000000000..91cac526127c2 --- /dev/null +++ b/test/zipkin/span_buffer_test.cc @@ -0,0 +1,100 @@ +#include "zipkin/span_buffer.h" + +#include "gtest/gtest.h" + +namespace Zipkin { + +TEST(ZipkinSpanBufferTest, defaultConstructorEndToEnd) { + SpanBuffer buffer; + Span span; + + EXPECT_EQ(0ULL, buffer.pendingSpans()); + EXPECT_EQ("[]", buffer.toStringifiedJsonArray()); + + buffer.allocateBuffer(2); + EXPECT_EQ(0ULL, buffer.pendingSpans()); + EXPECT_EQ("[]", buffer.toStringifiedJsonArray()); + + buffer.addSpan(std::move(span)); + EXPECT_EQ(1ULL, buffer.pendingSpans()); + std::string expectedJsonArrayString = "" + "[{\"traceId\":\"0000000000000000\"," + "\"name\":\"\"," + "\"id\":\"0000000000000000\"," + "\"annotations\":[]," + "\"binaryAnnotations\":[]}" + "]"; + EXPECT_EQ(expectedJsonArrayString, buffer.toStringifiedJsonArray()); + + buffer.flush(); + EXPECT_EQ(0ULL, buffer.pendingSpans()); + EXPECT_EQ("[]", buffer.toStringifiedJsonArray()); + + buffer.addSpan(std::move(span)); + buffer.addSpan(Span(span)); + expectedJsonArrayString = "" + "[" + "{\"traceId\":\"0000000000000000\"," + "\"name\":\"\"," + "\"id\":\"0000000000000000\"," + "\"annotations\":[]," + "\"binaryAnnotations\":[]}," + "{\"traceId\":\"0000000000000000\"," + "\"name\":\"\"," + "\"id\":\"0000000000000000\"," + "\"annotations\":[]," + "\"binaryAnnotations\":[]}" + "]"; + EXPECT_EQ(2ULL, buffer.pendingSpans()); + EXPECT_EQ(expectedJsonArrayString, buffer.toStringifiedJsonArray()); + + buffer.flush(); + EXPECT_EQ(0ULL, buffer.pendingSpans()); + EXPECT_EQ("[]", buffer.toStringifiedJsonArray()); +} + +TEST(ZipkinSpanBufferTest, sizeConstructorEndtoEnd) { + SpanBuffer buffer(2); + Span span; + + EXPECT_EQ(0ULL, buffer.pendingSpans()); + EXPECT_EQ("[]", buffer.toStringifiedJsonArray()); + + buffer.addSpan(std::move(span)); + EXPECT_EQ(1ULL, buffer.pendingSpans()); + std::string expectedJsonArrayString = "" + "[{\"traceId\":\"0000000000000000\"," + "\"name\":\"\"," + "\"id\":\"0000000000000000\"," + "\"annotations\":[]," + "\"binaryAnnotations\":[]}" + "]"; + EXPECT_EQ(expectedJsonArrayString, buffer.toStringifiedJsonArray()); + + buffer.flush(); + EXPECT_EQ(0ULL, buffer.pendingSpans()); + EXPECT_EQ("[]", buffer.toStringifiedJsonArray()); + + buffer.addSpan(std::move(span)); + buffer.addSpan(Span(span)); + expectedJsonArrayString = "" + "[" + "{\"traceId\":\"0000000000000000\"," + "\"name\":\"\"," + "\"id\":\"0000000000000000\"," + "\"annotations\":[]," + "\"binaryAnnotations\":[]}," + "{\"traceId\":\"0000000000000000\"," + "\"name\":\"\"," + "\"id\":\"0000000000000000\"," + "\"annotations\":[]," + "\"binaryAnnotations\":[]}" + "]"; + EXPECT_EQ(2ULL, buffer.pendingSpans()); + EXPECT_EQ(expectedJsonArrayString, buffer.toStringifiedJsonArray()); + + buffer.flush(); + EXPECT_EQ(0ULL, buffer.pendingSpans()); + EXPECT_EQ("[]", buffer.toStringifiedJsonArray()); +} +} // Zipkin diff --git a/test/zipkin/span_context_test.cc b/test/zipkin/span_context_test.cc new file mode 100755 index 0000000000000..339fbac951def --- /dev/null +++ b/test/zipkin/span_context_test.cc @@ -0,0 +1,152 @@ +#include "zipkin/span_context.h" +#include "zipkin/zipkin_core_constants.h" + +#include "gtest/gtest.h" + +namespace Zipkin { + +TEST(ZipkinSpanContextTest, populateFromString) { + SpanContext spanContext; + + // Non-initialized span context + EXPECT_EQ(0ULL, spanContext.trace_id()); + EXPECT_EQ("0000000000000000", spanContext.traceIdAsHexString()); + EXPECT_EQ(0ULL, spanContext.id()); + EXPECT_EQ("0000000000000000", spanContext.idAsHexString()); + EXPECT_EQ(0ULL, spanContext.parent_id()); + EXPECT_EQ("0000000000000000", spanContext.parentIdAsHexString()); + EXPECT_FALSE(spanContext.isSetAnnotation().cr); + EXPECT_FALSE(spanContext.isSetAnnotation().cs); + EXPECT_FALSE(spanContext.isSetAnnotation().sr); + EXPECT_FALSE(spanContext.isSetAnnotation().ss); + EXPECT_EQ("0000000000000000;0000000000000000;0000000000000000", spanContext.serializeToString()); + + // Span context populated with trace id, id, parent id, and no annotations + spanContext.populateFromString("25c6f38dd0600e79;56707c7b3e1092af;c49193ea42335d1c"); + EXPECT_EQ(2722130815203937913ULL, spanContext.trace_id()); + EXPECT_EQ("25c6f38dd0600e79", spanContext.traceIdAsHexString()); + EXPECT_EQ(6228615153417491119ULL, spanContext.id()); + EXPECT_EQ("56707c7b3e1092af", spanContext.idAsHexString()); + EXPECT_EQ(14164264937399213340ULL, spanContext.parent_id()); + EXPECT_EQ("c49193ea42335d1c", spanContext.parentIdAsHexString()); + EXPECT_FALSE(spanContext.isSetAnnotation().cr); + EXPECT_FALSE(spanContext.isSetAnnotation().cs); + EXPECT_FALSE(spanContext.isSetAnnotation().sr); + EXPECT_FALSE(spanContext.isSetAnnotation().ss); + EXPECT_EQ("25c6f38dd0600e79;56707c7b3e1092af;c49193ea42335d1c", spanContext.serializeToString()); + + // Span context populated with trace id, id, parent id, and one annotation + spanContext.populateFromString("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cs"); + EXPECT_EQ(2722130815203937912ULL, spanContext.trace_id()); + EXPECT_EQ("25c6f38dd0600e78", spanContext.traceIdAsHexString()); + EXPECT_EQ(6228615153417491119ULL, spanContext.id()); + EXPECT_EQ("56707c7b3e1092af", spanContext.idAsHexString()); + EXPECT_EQ(14164264937399213340ULL, spanContext.parent_id()); + EXPECT_EQ("c49193ea42335d1c", spanContext.parentIdAsHexString()); + EXPECT_FALSE(spanContext.isSetAnnotation().cr); + EXPECT_TRUE(spanContext.isSetAnnotation().cs); + EXPECT_FALSE(spanContext.isSetAnnotation().sr); + EXPECT_FALSE(spanContext.isSetAnnotation().ss); + EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cs", + spanContext.serializeToString()); + + // Span context populated with trace id, id, parent id, and multiple annotations + spanContext.populateFromString("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cs;cr"); + EXPECT_EQ(2722130815203937912ULL, spanContext.trace_id()); + EXPECT_EQ("25c6f38dd0600e78", spanContext.traceIdAsHexString()); + EXPECT_EQ(6228615153417491119ULL, spanContext.id()); + EXPECT_EQ("56707c7b3e1092af", spanContext.idAsHexString()); + EXPECT_EQ(14164264937399213340ULL, spanContext.parent_id()); + EXPECT_EQ("c49193ea42335d1c", spanContext.parentIdAsHexString()); + EXPECT_TRUE(spanContext.isSetAnnotation().cr); + EXPECT_TRUE(spanContext.isSetAnnotation().cs); + EXPECT_FALSE(spanContext.isSetAnnotation().sr); + EXPECT_FALSE(spanContext.isSetAnnotation().ss); + EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cr;cs", + spanContext.serializeToString()); + + // Span context populated with invalid string: it gets reset to its non-initialized state + spanContext.populateFromString("invalid string"); + EXPECT_EQ(0ULL, spanContext.trace_id()); + EXPECT_EQ("0000000000000000", spanContext.traceIdAsHexString()); + EXPECT_EQ(0ULL, spanContext.id()); + EXPECT_EQ("0000000000000000", spanContext.idAsHexString()); + EXPECT_EQ(0ULL, spanContext.parent_id()); + EXPECT_EQ("0000000000000000", spanContext.parentIdAsHexString()); + EXPECT_FALSE(spanContext.isSetAnnotation().cr); + EXPECT_FALSE(spanContext.isSetAnnotation().cs); + EXPECT_FALSE(spanContext.isSetAnnotation().sr); + EXPECT_FALSE(spanContext.isSetAnnotation().ss); + EXPECT_EQ("0000000000000000;0000000000000000;0000000000000000", spanContext.serializeToString()); +} + +TEST(ZipkinSpanContextTest, populateFromSpan) { + Span span; + SpanContext spanContext(span); + + // Non-initialized span context + EXPECT_EQ(0ULL, spanContext.trace_id()); + EXPECT_EQ("0000000000000000", spanContext.traceIdAsHexString()); + EXPECT_EQ(0ULL, spanContext.id()); + EXPECT_EQ("0000000000000000", spanContext.idAsHexString()); + EXPECT_EQ(0ULL, spanContext.parent_id()); + EXPECT_EQ("0000000000000000", spanContext.parentIdAsHexString()); + EXPECT_FALSE(spanContext.isSetAnnotation().cr); + EXPECT_FALSE(spanContext.isSetAnnotation().cs); + EXPECT_FALSE(spanContext.isSetAnnotation().sr); + EXPECT_FALSE(spanContext.isSetAnnotation().ss); + EXPECT_EQ("0000000000000000;0000000000000000;0000000000000000", spanContext.serializeToString()); + + // Span context populated with trace id, id, parent id, and no annotations + span.setTraceId(2722130815203937912ULL); + span.setId(6228615153417491119ULL); + span.setParentId(14164264937399213340ULL); + SpanContext spanContext2(span); + EXPECT_EQ(2722130815203937912ULL, spanContext2.trace_id()); + EXPECT_EQ("25c6f38dd0600e78", spanContext2.traceIdAsHexString()); + EXPECT_EQ(6228615153417491119ULL, spanContext2.id()); + EXPECT_EQ("56707c7b3e1092af", spanContext2.idAsHexString()); + EXPECT_EQ(14164264937399213340ULL, spanContext2.parent_id()); + EXPECT_EQ("c49193ea42335d1c", spanContext2.parentIdAsHexString()); + EXPECT_FALSE(spanContext2.isSetAnnotation().cr); + EXPECT_FALSE(spanContext2.isSetAnnotation().cs); + EXPECT_FALSE(spanContext2.isSetAnnotation().sr); + EXPECT_FALSE(spanContext2.isSetAnnotation().ss); + EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c", spanContext2.serializeToString()); + + // Span context populated with trace id, id, parent id, and one annotation + Annotation ann; + ann.setValue(ZipkinCoreConstants::SERVER_RECV); + span.addAnnotation(ann); + SpanContext spanContext3(span); + EXPECT_EQ(2722130815203937912ULL, spanContext3.trace_id()); + EXPECT_EQ("25c6f38dd0600e78", spanContext3.traceIdAsHexString()); + EXPECT_EQ(6228615153417491119ULL, spanContext3.id()); + EXPECT_EQ("56707c7b3e1092af", spanContext3.idAsHexString()); + EXPECT_EQ(14164264937399213340ULL, spanContext3.parent_id()); + EXPECT_EQ("c49193ea42335d1c", spanContext3.parentIdAsHexString()); + EXPECT_FALSE(spanContext3.isSetAnnotation().cr); + EXPECT_FALSE(spanContext3.isSetAnnotation().cs); + EXPECT_TRUE(spanContext3.isSetAnnotation().sr); + EXPECT_FALSE(spanContext3.isSetAnnotation().ss); + EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;sr", + spanContext3.serializeToString()); + + // Span context populated with trace id, id, parent id, and multiple annotations + ann.setValue(ZipkinCoreConstants::SERVER_SEND); + span.addAnnotation(ann); + SpanContext spanContext4(span); + EXPECT_EQ(2722130815203937912ULL, spanContext4.trace_id()); + EXPECT_EQ("25c6f38dd0600e78", spanContext4.traceIdAsHexString()); + EXPECT_EQ(6228615153417491119ULL, spanContext4.id()); + EXPECT_EQ("56707c7b3e1092af", spanContext4.idAsHexString()); + EXPECT_EQ(14164264937399213340ULL, spanContext4.parent_id()); + EXPECT_EQ("c49193ea42335d1c", spanContext4.parentIdAsHexString()); + EXPECT_FALSE(spanContext4.isSetAnnotation().cr); + EXPECT_FALSE(spanContext4.isSetAnnotation().cs); + EXPECT_TRUE(spanContext4.isSetAnnotation().sr); + EXPECT_TRUE(spanContext4.isSetAnnotation().ss); + EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;sr;ss", + spanContext4.serializeToString()); +} +} // Zipkin diff --git a/test/zipkin/util_test.cc b/test/zipkin/util_test.cc new file mode 100755 index 0000000000000..5429894e0358b --- /dev/null +++ b/test/zipkin/util_test.cc @@ -0,0 +1,46 @@ +#include "zipkin/util.h" + +#include "gtest/gtest.h" + +namespace Zipkin { + +TEST(ZipkinUtilTest, utilTests) { + EXPECT_EQ(typeid(uint64_t).name(), typeid(Util::generateRandom64()).name()); + EXPECT_EQ(typeid(uint64_t).name(), typeid(Util::timeSinceEpochMicro()).name()); + EXPECT_EQ(typeid(uint64_t).name(), typeid(Util::timeSinceEpochNano()).name()); + + std::string base16_string = Util::uint64ToBase16(2722130815203937912ULL); + EXPECT_EQ(typeid(std::string).name(), typeid(base16_string).name()); + EXPECT_EQ("25c6f38dd0600e78", base16_string); + + // Test JSON merging + + std::string merged_json = "{}"; + std::string source_json = "{\"field1\":\"val1\"}"; + Util::mergeJsons(merged_json, source_json, "sub_json"); + std::string expected_json = "{\"sub_json\":{\"field1\":\"val1\"}}"; + EXPECT_EQ(expected_json, merged_json); + + Util::mergeJsons(merged_json, merged_json, "second_merge"); + expected_json = + "{\"sub_json\":{\"field1\":\"val1\"},\"second_merge\":{\"sub_json\":{\"field1\":\"val1\"}}}"; + EXPECT_EQ(expected_json, merged_json); + + // Test adding an array to a JSON + + std::vector json_array; + Util::addArrayToJson(merged_json, json_array, "array_field"); + expected_json = "{\"sub_json\":{\"field1\":\"val1\"},\"second_merge\":{\"sub_json\":{\"field1\":" + "\"val1\"}},\"array_field\":[]}"; + EXPECT_EQ(expected_json, merged_json); + + std::string str1 = "{\"a1\":10}"; + std::string str2 = "{\"a2\":\"10\"}"; + json_array.push_back(&str1); + json_array.push_back(&str2); + Util::addArrayToJson(merged_json, json_array, "second_array"); + expected_json = "{\"sub_json\":{\"field1\":\"val1\"},\"second_merge\":{\"sub_json\":{\"field1\":" + "\"val1\"}},\"array_field\":[],\"second_array\":[{\"a1\":10},{\"a2\":\"10\"}]}"; + EXPECT_EQ(expected_json, merged_json); +} +} // Zipkin From 6e2e7b6c15fb4d078ebf2c3cfdfaaea9a2cb6cc5 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Thu, 6 Apr 2017 23:44:08 -0400 Subject: [PATCH 14/55] Fixed memory safety issue (#430) --- source/zipkin/span_context.cc | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/source/zipkin/span_context.cc b/source/zipkin/span_context.cc index d9576a2de88d2..393fb111ae1b0 100644 --- a/source/zipkin/span_context.cc +++ b/source/zipkin/span_context.cc @@ -79,21 +79,23 @@ void SpanContext::populateFromString(std::string s) { id_ = stoull(match.str(2), nullptr, 16); parent_id_ = stoull(match.str(3), nullptr, 16); - if (match.str(4).size() > 0) { - char* annotation_value_strings = const_cast(match.str(4).c_str()); - char* annotation_value = strtok(annotation_value_strings, FIELD_SEPARATOR_.c_str()); + std::string matched_annotations = match.str(4); + if (matched_annotations.size() > 0) { + char* annotation_value_strings = const_cast(matched_annotations.c_str()); + char* annotation_value = std::strtok(annotation_value_strings, FIELD_SEPARATOR_.c_str()); + while (annotation_value) { - if (!strcmp(annotation_value, ZipkinCoreConstants::CLIENT_RECV.c_str())) { + if (!std::strcmp(annotation_value, ZipkinCoreConstants::CLIENT_RECV.c_str())) { annotation_values_.cr = true; - } else if (!strcmp(annotation_value, ZipkinCoreConstants::CLIENT_SEND.c_str())) { + } else if (!std::strcmp(annotation_value, ZipkinCoreConstants::CLIENT_SEND.c_str())) { annotation_values_.cs = true; - } else if (!strcmp(annotation_value, ZipkinCoreConstants::SERVER_RECV.c_str())) { + } else if (!std::strcmp(annotation_value, ZipkinCoreConstants::SERVER_RECV.c_str())) { annotation_values_.sr = true; - } else if (!strcmp(annotation_value, ZipkinCoreConstants::SERVER_SEND.c_str())) { + } else if (!std::strcmp(annotation_value, ZipkinCoreConstants::SERVER_SEND.c_str())) { annotation_values_.ss = true; } - annotation_value = strtok(NULL, FIELD_SEPARATOR_.c_str()); + annotation_value = std::strtok(NULL, FIELD_SEPARATOR_.c_str()); } } From eaedc3261d78db182c164d20ac1a82c8b528a947 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Mon, 10 Apr 2017 23:18:04 -0400 Subject: [PATCH 15/55] Renamed host to endpoint in the zipkin data structures and cleaned the code (#430) --- source/zipkin/tracer.cc | 12 +----- source/zipkin/zipkin_core_types.cc | 45 ++++++-------------- source/zipkin/zipkin_core_types.h | 68 ++++++++++++++---------------- 3 files changed, 47 insertions(+), 78 deletions(-) diff --git a/source/zipkin/tracer.cc b/source/zipkin/tracer.cc index b6e1050b7a31e..f870b8cf12148 100644 --- a/source/zipkin/tracer.cc +++ b/source/zipkin/tracer.cc @@ -4,8 +4,6 @@ #include "zipkin/tracer.h" #include "zipkin/zipkin_core_constants.h" -#include - namespace Zipkin { Span Tracer::startSpan(const std::string& span_name, uint64_t start_time) { @@ -23,7 +21,7 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time) { ep.setServiceName(service_name_); // Build the CS annotation - cs.setHost(std::move(ep)); + cs.setEndpoint(std::move(ep)); cs.setValue(ZipkinCoreConstants::CLIENT_SEND); // Create an all-new span, with no parent id @@ -41,9 +39,7 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time) { // Add CS annotation to the span span.addAnnotation(std::move(cs)); - std::cerr << "Span's tracer pointer before: " << span.tracer() << std::endl; span.setTracer(this); - std::cerr << "Span's tracer pointer after: " << span.tracer() << std::endl; return span; } @@ -103,7 +99,7 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time, ep.setServiceName(service_name_); // Add the newly-created annotation to the span - annotation.setHost(std::move(ep)); + annotation.setEndpoint(std::move(ep)); annotation.setTimestamp(timestampMicro); span.addAnnotation(std::move(annotation)); @@ -112,18 +108,14 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time, span.setStartTime(start_time); - std::cerr << "Span's tracer pointer before: " << span.tracer() << std::endl; span.setTracer(this); - std::cerr << "Span's tracer pointer after: " << span.tracer() << std::endl; return span; } void Tracer::reportSpan(Span&& span) { - std::cerr << "Tracer::reportSpan() called." << std::endl; auto r = reporter(); if (r) { - std::cerr << "Will call Reporter::reportSpan()" << std::endl; r->reportSpan(std::move(span)); } } diff --git a/source/zipkin/zipkin_core_types.cc b/source/zipkin/zipkin_core_types.cc index ce7bf3678f946..0ab3188226273 100644 --- a/source/zipkin/zipkin_core_types.cc +++ b/source/zipkin/zipkin_core_types.cc @@ -7,8 +7,6 @@ #include "zipkin/util.h" #include "zipkin/span_context.h" -#include - namespace Zipkin { Endpoint::Endpoint(const Endpoint& ep) { @@ -52,15 +50,15 @@ const std::string& Endpoint::toJson() { Annotation::Annotation(const Annotation& ann) { timestamp_ = ann.timestamp(); value_ = ann.value(); - host_ = ann.host(); - isset_host_ = ann.isSetHost(); + endpoint_ = ann.endpoint(); + isset_endpoint_ = ann.isSetEndpoint(); } Annotation& Annotation::operator=(const Annotation& ann) { timestamp_ = ann.timestamp(); value_ = ann.value(); - host_ = ann.host(); - isset_host_ = ann.isSetHost(); + endpoint_ = ann.endpoint(); + isset_endpoint_ = ann.isSetEndpoint(); return *this; } @@ -77,8 +75,8 @@ const std::string& Annotation::toJson() { json_string_ = s.GetString(); - if (isset_host_) { - Util::mergeJsons(json_string_, host_.toJson(), + if (isset_endpoint_) { + Util::mergeJsons(json_string_, endpoint_.toJson(), ZipkinJsonFieldNames::ANNOTATION_ENDPOINT.c_str()); } @@ -89,16 +87,16 @@ BinaryAnnotation::BinaryAnnotation(const BinaryAnnotation& ann) { key_ = ann.key(); value_ = ann.value(); annotation_type_ = ann.annotationType(); - host_ = ann.host(); - isset_host_ = ann.isSetHost(); + endpoint_ = ann.endpoint(); + isset_endpoint_ = ann.isSetEndpoint(); } BinaryAnnotation& BinaryAnnotation::operator=(const BinaryAnnotation& ann) { key_ = ann.key(); value_ = ann.value(); annotation_type_ = ann.annotationType(); - host_ = ann.host(); - isset_host_ = ann.isSetHost(); + endpoint_ = ann.endpoint(); + isset_endpoint_ = ann.isSetEndpoint(); return *this; } @@ -115,8 +113,8 @@ const std::string& BinaryAnnotation::toJson() { json_string_ = s.GetString(); - if (isset_host_) { - Util::mergeJsons(json_string_, host_.toJson(), + if (isset_endpoint_) { + Util::mergeJsons(json_string_, endpoint_.toJson(), ZipkinJsonFieldNames::BINARY_ANNOTATION_ENDPOINT.c_str()); } @@ -130,7 +128,6 @@ Span::Span(const Span& span) { parent_id_ = span.parentId(); annotations_ = span.annotations(); binary_annotations_ = span.binaryAnnotations(); - debug_ = span.isDebug(); timestamp_ = span.timestamp(); duration_ = span.duration(); trace_id_high_ = span.traceIdHigh(); @@ -146,7 +143,6 @@ Span& Span::operator=(const Span& span) { parent_id_ = span.parentId(); annotations_ = span.annotations(); binary_annotations_ = span.binaryAnnotations(); - debug_ = span.isDebug(); timestamp_ = span.timestamp(); duration_ = span.duration(); trace_id_high_ = span.traceIdHigh(); @@ -183,16 +179,6 @@ const std::string& Span::toJson() { writer.Int64(duration_); } - if (isset_.debug) { - writer.Key(ZipkinJsonFieldNames::SPAN_DEBUG.c_str()); - writer.Bool(debug_); - } - - if (isset_.trace_id_high) { - writer.Key(ZipkinJsonFieldNames::SPAN_TRACE_ID_HIGH.c_str()); - writer.Int64(trace_id_high_); - } - writer.EndObject(); json_string_ = s.GetString(); @@ -221,7 +207,7 @@ void Span::finish() { if ((context.isSetAnnotation().sr) && (!context.isSetAnnotation().ss)) { // Need to set the SS annotation Annotation ss; - ss.setHost(annotations_[0].host()); + ss.setEndpoint(annotations_[0].endpoint()); ss.setTimestamp(Util::timeSinceEpochMicro()); ss.setValue(ZipkinCoreConstants::SERVER_SEND); annotations_.push_back(std::move(ss)); @@ -229,23 +215,20 @@ void Span::finish() { // Need to set the CR annotation Annotation cr; uint64_t stop_time = Util::timeSinceEpochMicro(); - cr.setHost(annotations_[0].host()); + cr.setEndpoint(annotations_[0].endpoint()); cr.setTimestamp(stop_time); cr.setValue(ZipkinCoreConstants::CLIENT_RECV); annotations_.push_back(std::move(cr)); setDuration(stop_time - timestamp_); } - std::cerr << "Span: " << toJson() << std::endl; auto t = tracer(); if (t) { - std::cerr << "Will call Tracer::reportSpan" << std::endl; t->reportSpan(std::move(*this)); } } void Span::setTag(const std::string& name, const std::string& value) { - std::cerr << "setTag called --> Name: " << name << "; Value: " << value << std::endl; if ((name.size() > 0) && (value.size() > 0)) { addBinaryAnnotation(BinaryAnnotation(name, value)); } diff --git a/source/zipkin/zipkin_core_types.h b/source/zipkin/zipkin_core_types.h index d126b1aa63056..218d49339d631 100644 --- a/source/zipkin/zipkin_core_types.h +++ b/source/zipkin/zipkin_core_types.h @@ -27,12 +27,12 @@ class Endpoint : public ZipkinBase { Endpoint() : ipv4_(), port_(0), service_name_(), isset_ipv6_(false) {} - Endpoint(std::string& ipv4, uint16_t port, const std::string& service_name) + Endpoint(const std::string& ipv4, uint16_t port, const std::string& service_name) : ipv4_(ipv4), port_(port), service_name_(service_name), isset_ipv6_(false) {} const std::string& ipv4() const { return ipv4_; } - void setIpv4(std::string& ipv4) { ipv4_ = ipv4; } + void setIpv4(const std::string& ipv4) { ipv4_ = ipv4; } const std::string& ipv6() const { return ipv6_; } @@ -68,21 +68,21 @@ class Annotation : public ZipkinBase { Annotation& operator=(const Annotation&); - Annotation() : timestamp_(0), value_(), isset_host_(false) {} + Annotation() : timestamp_(0), value_(), isset_endpoint_(false) {} - Annotation(uint64_t timestamp, const std::string value, Endpoint& host) - : timestamp_(timestamp), value_(value), host_(host), isset_host_(true) {} + Annotation(uint64_t timestamp, const std::string value, Endpoint& endpoint) + : timestamp_(timestamp), value_(value), endpoint_(endpoint), isset_endpoint_(true) {} - const Endpoint& host() const { return host_; } + const Endpoint& endpoint() const { return endpoint_; } - void setHost(const Endpoint& host) { - host_ = host; - isset_host_ = true; + void setEndpoint(const Endpoint& endpoint) { + endpoint_ = endpoint; + isset_endpoint_ = true; } - void setHost(const Endpoint&& host) { - host_ = host; - isset_host_ = true; + void setEndpoint(const Endpoint&& endpoint) { + endpoint_ = endpoint; + isset_endpoint_ = true; } uint64_t timestamp() const { return timestamp_; } @@ -93,19 +93,19 @@ class Annotation : public ZipkinBase { void setValue(const std::string& value) { value_ = value; } - bool isSetHost() const { return isset_host_; } + bool isSetEndpoint() const { return isset_endpoint_; } const std::string& toJson() override; private: uint64_t timestamp_; std::string value_; - Endpoint host_; + Endpoint endpoint_; - bool isset_host_; + bool isset_endpoint_; }; -enum AnnotationType { BOOL = 0, BYTES = 1, I16 = 2, I32 = 3, I64 = 4, DOUBLE = 5, STRING = 6 }; +enum AnnotationType { BOOL = 0, STRING = 1 }; class BinaryAnnotation : public ZipkinBase { public: @@ -113,28 +113,28 @@ class BinaryAnnotation : public ZipkinBase { BinaryAnnotation& operator=(const BinaryAnnotation&); - BinaryAnnotation() : key_(), value_(), annotation_type_(STRING), isset_host_(false) {} + BinaryAnnotation() : key_(), value_(), annotation_type_(STRING), isset_endpoint_(false) {} BinaryAnnotation(const std::string& key, const std::string& value) - : key_(key), value_(value), annotation_type_(STRING), isset_host_(false) {} + : key_(key), value_(value), annotation_type_(STRING), isset_endpoint_(false) {} AnnotationType annotationType() const { return annotation_type_; } void setAnnotationType(AnnotationType annotationType) { annotation_type_ = annotationType; } - const Endpoint& host() const { return host_; } + const Endpoint& endpoint() const { return endpoint_; } - void setHost(const Endpoint& host) { - host_ = host; - isset_host_ = true; + void setEndpoint(const Endpoint& endpoint) { + endpoint_ = endpoint; + isset_endpoint_ = true; } - void setHost(const Endpoint&& host) { - host_ = host; - isset_host_ = true; + void setEndpoint(const Endpoint&& endpoint) { + endpoint_ = endpoint; + isset_endpoint_ = true; } - bool isSetHost() const { return isset_host_; } + bool isSetEndpoint() const { return isset_endpoint_; } const std::string& key() const { return key_; } @@ -149,11 +149,11 @@ class BinaryAnnotation : public ZipkinBase { private: std::string key_; std::string value_; - Endpoint host_; + Endpoint endpoint_; AnnotationType annotation_type_; - bool isset_host_; + bool isset_endpoint_; }; typedef struct _Span__isset { @@ -173,7 +173,7 @@ class Span : public ZipkinBase { Span& operator=(const Span&); Span() - : trace_id_(0), name_(), id_(0), parent_id_(0), debug_(false), timestamp_(0), duration_(0), + : trace_id_(0), name_(), id_(0), parent_id_(0), timestamp_(0), duration_(0), trace_id_high_(0), start_time_(0), tracer_(nullptr) {} void setTraceId(const uint64_t val) { trace_id_ = val; } @@ -201,10 +201,7 @@ class Span : public ZipkinBase { void addBinaryAnnotation(const BinaryAnnotation&& bann) { binary_annotations_.push_back(bann); } - void setDebug(const bool val) { - debug_ = val; - isset_.debug = true; - } + void setDebug() { isset_.debug = true; } void setTimestamp(const int64_t val) { timestamp_ = val; @@ -216,7 +213,7 @@ class Span : public ZipkinBase { isset_.duration = true; } - void setTraceIdHigh(const int64_t val) { + void setTraceIdHigh(const uint64_t val) { trace_id_high_ = val; isset_.trace_id_high = true; } @@ -227,8 +224,6 @@ class Span : public ZipkinBase { const std::vector& binaryAnnotations() const { return binary_annotations_; } - bool isDebug() const { return debug_; } - int64_t duration() const { return duration_; } uint64_t id() const { return id_; } @@ -270,7 +265,6 @@ class Span : public ZipkinBase { uint64_t parent_id_; std::vector annotations_; std::vector binary_annotations_; - bool debug_; int64_t timestamp_; int64_t duration_; uint64_t trace_id_high_; From f899947209effc3558faa32dad7e45e8cf812040 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Mon, 10 Apr 2017 23:19:27 -0400 Subject: [PATCH 16/55] Removed un-used Zipkin JSON field names (#430) --- source/zipkin/zipkin_json_field_names.cc | 2 -- source/zipkin/zipkin_json_field_names.h | 2 -- 2 files changed, 4 deletions(-) diff --git a/source/zipkin/zipkin_json_field_names.cc b/source/zipkin/zipkin_json_field_names.cc index 20e1967478b1d..b013ccbe118a9 100644 --- a/source/zipkin/zipkin_json_field_names.cc +++ b/source/zipkin/zipkin_json_field_names.cc @@ -1,13 +1,11 @@ #include "zipkin/zipkin_json_field_names.h" const std::string Zipkin::ZipkinJsonFieldNames::SPAN_TRACE_ID = "traceId"; -const std::string Zipkin::ZipkinJsonFieldNames::SPAN_TRACE_ID_HIGH = "traceIdHigh"; const std::string Zipkin::ZipkinJsonFieldNames::SPAN_PARENT_ID = "parentId"; const std::string Zipkin::ZipkinJsonFieldNames::SPAN_NAME = "name"; const std::string Zipkin::ZipkinJsonFieldNames::SPAN_ID = "id"; const std::string Zipkin::ZipkinJsonFieldNames::SPAN_TIMESTAMP = "timestamp"; const std::string Zipkin::ZipkinJsonFieldNames::SPAN_DURATION = "duration"; -const std::string Zipkin::ZipkinJsonFieldNames::SPAN_DEBUG = "debug"; const std::string Zipkin::ZipkinJsonFieldNames::SPAN_ANNOTATIONS = "annotations"; const std::string Zipkin::ZipkinJsonFieldNames::SPAN_BINARY_ANNOTATIONS = "binaryAnnotations"; diff --git a/source/zipkin/zipkin_json_field_names.h b/source/zipkin/zipkin_json_field_names.h index 01cd36fc4a570..cf49d5d67bedb 100644 --- a/source/zipkin/zipkin_json_field_names.h +++ b/source/zipkin/zipkin_json_field_names.h @@ -7,13 +7,11 @@ namespace Zipkin { class ZipkinJsonFieldNames { public: static const std::string SPAN_TRACE_ID; - static const std::string SPAN_TRACE_ID_HIGH; static const std::string SPAN_PARENT_ID; static const std::string SPAN_NAME; static const std::string SPAN_ID; static const std::string SPAN_TIMESTAMP; static const std::string SPAN_DURATION; - static const std::string SPAN_DEBUG; static const std::string SPAN_ANNOTATIONS; static const std::string SPAN_BINARY_ANNOTATIONS; From 6d740b478be998fedf2e7cfeec3df0a6dfbd8d12 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Mon, 10 Apr 2017 23:21:13 -0400 Subject: [PATCH 17/55] Added the last batch of tests for the zipkin library (#430) --- test/CMakeLists.txt | 4 +- test/zipkin/tracer_test.cc | 168 +++++++++ test/zipkin/zipkin_core_types_test.cc | 525 ++++++++++++++++++++++++++ 3 files changed, 696 insertions(+), 1 deletion(-) create mode 100755 test/zipkin/tracer_test.cc create mode 100755 test/zipkin/zipkin_core_types_test.cc diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 1efd8dd9723f2..96b22ef93c80c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -164,7 +164,9 @@ add_executable(envoy-test test_common/utility.cc zipkin/span_buffer_test.cc zipkin/span_context_test.cc - zipkin/util_test.cc) + zipkin/util_test.cc + zipkin/tracer_test.cc + zipkin/zipkin_core_types_test.cc) # The MOCK_METHOD* macros from gtest triggers this clang warning and it's hard # to work around, so we just ignore it. diff --git a/test/zipkin/tracer_test.cc b/test/zipkin/tracer_test.cc new file mode 100755 index 0000000000000..cf27a0477205f --- /dev/null +++ b/test/zipkin/tracer_test.cc @@ -0,0 +1,168 @@ +#include "zipkin/tracer.h" +#include "zipkin/util.h" +#include "zipkin/zipkin_core_constants.h" + +#include "gtest/gtest.h" + +namespace Zipkin { + +TEST(ZipkinTracerTest, spanCreation) { + Tracer tracer("my_service_name", "127.0.0.1:9000"); + int64_t timestamp = Util::timeSinceEpochMicro(); + + // ============== + // Test the creation of a root span --> CS + // ============== + + Span root_span = tracer.startSpan("my_span", timestamp); + + EXPECT_EQ("my_span", root_span.name()); + EXPECT_EQ(timestamp, root_span.startTime()); + + EXPECT_NE(0ULL, root_span.traceId()); // trace id must be set + EXPECT_EQ(root_span.traceId(), root_span.id()); // span id and trace id must be the same + EXPECT_FALSE(root_span.isSet().parent_id); // no parent set + EXPECT_NE(0LL, root_span.timestamp()); // span's timestamp must be set + + // A CS annotation must have been added + EXPECT_EQ(1ULL, root_span.annotations().size()); + Annotation ann = root_span.annotations()[0]; + EXPECT_EQ(ZipkinCoreConstants::CLIENT_SEND, ann.value()); + EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set + EXPECT_TRUE(ann.isSetEndpoint()); + Endpoint endpoint = ann.endpoint(); + EXPECT_EQ("127.0.0.1", endpoint.ipv4()); + EXPECT_EQ(9000, endpoint.port()); + EXPECT_EQ("my_service_name", endpoint.serviceName()); + EXPECT_FALSE(endpoint.isSetIpv6()); + + // The tracer must have been properly set + EXPECT_EQ(dynamic_cast(&tracer), root_span.tracer()); + + // Duration is not set at span-creation time + EXPECT_FALSE(root_span.isSet().duration); + + // ============== + // Test the creation of a shared-context span --> SR + // ============== + + SpanContext root_span_context(root_span); + Span server_side_shared_context_span = tracer.startSpan("my_span", timestamp, root_span_context); + + EXPECT_EQ(timestamp, server_side_shared_context_span.startTime()); + + // span name should NOT be set (it was set in the CS side) + EXPECT_EQ("", server_side_shared_context_span.name()); + + // trace id must be the same in the CS and SR sides + EXPECT_EQ(root_span.traceId(), server_side_shared_context_span.traceId()); + + // span id must be the same in the CS and SR sides + EXPECT_EQ(root_span.id(), server_side_shared_context_span.id()); + + // The parent should be the same as in the CS side (none in this case) + EXPECT_FALSE(server_side_shared_context_span.isSet().parent_id); + + // span timestamp should not be set (it was set in the CS side) + EXPECT_EQ(0LL, server_side_shared_context_span.timestamp()); + EXPECT_FALSE(server_side_shared_context_span.isSet().timestamp); + + // An SR annotation must have been added + EXPECT_EQ(1ULL, server_side_shared_context_span.annotations().size()); + ann = server_side_shared_context_span.annotations()[0]; + EXPECT_EQ(ZipkinCoreConstants::SERVER_RECV, ann.value()); + EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set + EXPECT_TRUE(ann.isSetEndpoint()); + endpoint = ann.endpoint(); + EXPECT_EQ("127.0.0.1", endpoint.ipv4()); + EXPECT_EQ(9000, endpoint.port()); + EXPECT_EQ("my_service_name", endpoint.serviceName()); + EXPECT_FALSE(endpoint.isSetIpv6()); + + // The tracer must have been properly set + EXPECT_EQ(dynamic_cast(&tracer), server_side_shared_context_span.tracer()); + + // Duration is not set at span-creation time + EXPECT_FALSE(server_side_shared_context_span.isSet().duration); + + // ============== + // Test the creation of a child span --> CS + // ============== + + SpanContext server_side_context(server_side_shared_context_span); + Span child_span = tracer.startSpan("my_child_span", timestamp, server_side_context); + + EXPECT_EQ("my_child_span", child_span.name()); + EXPECT_EQ(timestamp, child_span.startTime()); + + // trace id must be retained + EXPECT_NE(0ULL, child_span.traceId()); + EXPECT_EQ(server_side_shared_context_span.traceId(), child_span.traceId()); + + // span id and trace id must NOT be the same + EXPECT_NE(child_span.traceId(), child_span.id()); + + // parent should be the previous span + EXPECT_TRUE(child_span.isSet().parent_id); + EXPECT_EQ(server_side_shared_context_span.id(), child_span.parentId()); + + // span's timestamp must be set + EXPECT_NE(0LL, child_span.timestamp()); + + // A CS annotation must have been added + EXPECT_EQ(1ULL, child_span.annotations().size()); + ann = child_span.annotations()[0]; + EXPECT_EQ(ZipkinCoreConstants::CLIENT_SEND, ann.value()); + EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set + EXPECT_TRUE(ann.isSetEndpoint()); + endpoint = ann.endpoint(); + EXPECT_EQ("127.0.0.1", endpoint.ipv4()); + EXPECT_EQ(9000, endpoint.port()); + EXPECT_EQ("my_service_name", endpoint.serviceName()); + EXPECT_FALSE(endpoint.isSetIpv6()); + + // The tracer must have been properly set + EXPECT_EQ(dynamic_cast(&tracer), child_span.tracer()); + + // Duration is not set at span-creation time + EXPECT_FALSE(child_span.isSet().duration); +} + +TEST(ZipkinTracerTest, finishSpan) { + Tracer tracer("my_service_name", "127.0.0.1:9000"); + int64_t timestamp = Util::timeSinceEpochMicro(); + + // ============== + // Test finishing a span containing a CS annotation + // ============== + + // Creates a root-span with a CS annotation + Span span = tracer.startSpan("my_span", timestamp); + + // Finishing a root span with a CS annotation must add a CR annotation + span.finish(); + EXPECT_EQ(2ULL, span.annotations().size()); + + // Check the CS annotation added at span-creation time + Annotation ann = span.annotations()[0]; + EXPECT_EQ(ZipkinCoreConstants::CLIENT_SEND, ann.value()); + EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set + EXPECT_TRUE(ann.isSetEndpoint()); + Endpoint endpoint = ann.endpoint(); + EXPECT_EQ("127.0.0.1", endpoint.ipv4()); + EXPECT_EQ(9000, endpoint.port()); + EXPECT_EQ("my_service_name", endpoint.serviceName()); + EXPECT_FALSE(endpoint.isSetIpv6()); + + // Check the CR annotation added when ending the span + ann = span.annotations()[1]; + EXPECT_EQ(ZipkinCoreConstants::CLIENT_RECV, ann.value()); + EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set + EXPECT_TRUE(ann.isSetEndpoint()); + endpoint = ann.endpoint(); + EXPECT_EQ("127.0.0.1", endpoint.ipv4()); + EXPECT_EQ(9000, endpoint.port()); + EXPECT_EQ("my_service_name", endpoint.serviceName()); + EXPECT_FALSE(endpoint.isSetIpv6()); +} +} // Zipkin diff --git a/test/zipkin/zipkin_core_types_test.cc b/test/zipkin/zipkin_core_types_test.cc new file mode 100755 index 0000000000000..bcacaa971ce59 --- /dev/null +++ b/test/zipkin/zipkin_core_types_test.cc @@ -0,0 +1,525 @@ +#include "zipkin/zipkin_core_types.h" +#include "zipkin/zipkin_core_constants.h" + +#include "gtest/gtest.h" + +namespace Zipkin { + +TEST(ZipkinCoreTypesEndpointTest, defaultConstructor) { + Endpoint ep; + + EXPECT_EQ("", ep.ipv4()); + EXPECT_EQ(0, ep.port()); + EXPECT_EQ("", ep.serviceName()); + EXPECT_FALSE(ep.isSetIpv6()); + EXPECT_EQ("{\"ipv4\":\"\",\"port\":0,\"serviceName\":\"\"}", ep.toJson()); + + ep.setIpv4(std::string("127.0.0.1")); + EXPECT_EQ("127.0.0.1", ep.ipv4()); + + ep.setIpv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); + EXPECT_EQ("2001:0db8:85a3:0000:0000:8a2e:0370:7334", ep.ipv6()); + EXPECT_TRUE(ep.isSetIpv6()); + + ep.setPort(3306); + EXPECT_EQ(3306, ep.port()); + + ep.setServiceName("my_service"); + EXPECT_EQ("my_service", ep.serviceName()); + + EXPECT_EQ("{\"ipv4\":\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\",\"ipv6\":\"2001:" + "0db8:85a3:0000:0000:8a2e:0370:7334\"}", + ep.toJson()); +} + +TEST(ZipkinCoreTypesEndpointTest, customConstructor) { + Endpoint ep(std::string("127.0.0.1"), 3306, std::string("my_service")); + + EXPECT_EQ("127.0.0.1", ep.ipv4()); + EXPECT_EQ(3306, ep.port()); + EXPECT_EQ("my_service", ep.serviceName()); + EXPECT_FALSE(ep.isSetIpv6()); + EXPECT_EQ("{\"ipv4\":\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\"}", ep.toJson()); + + ep.setIpv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); + EXPECT_EQ("2001:0db8:85a3:0000:0000:8a2e:0370:7334", ep.ipv6()); + EXPECT_TRUE(ep.isSetIpv6()); + + EXPECT_EQ("{\"ipv4\":\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\",\"ipv6\":\"2001:" + "0db8:85a3:0000:0000:8a2e:0370:7334\"}", + ep.toJson()); +} + +TEST(ZipkinCoreTypesEndpointTest, copyOperator) { + Endpoint ep1(std::string("127.0.0.1"), 3306, std::string("my_service")); + Endpoint ep2(ep1); + + EXPECT_EQ("127.0.0.1", ep1.ipv4()); + EXPECT_EQ(3306, ep1.port()); + EXPECT_EQ("my_service", ep1.serviceName()); + EXPECT_FALSE(ep1.isSetIpv6()); + EXPECT_EQ("{\"ipv4\":\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\"}", ep1.toJson()); + + EXPECT_EQ(ep1.ipv4(), ep2.ipv4()); + EXPECT_EQ(ep1.port(), ep2.port()); + EXPECT_EQ(ep1.serviceName(), ep2.serviceName()); + EXPECT_FALSE(ep2.isSetIpv6()); + EXPECT_EQ(ep1.toJson(), ep2.toJson()); +} + +TEST(ZipkinCoreTypesEndpointTest, assignmentOperator) { + Endpoint ep1(std::string("127.0.0.1"), 3306, std::string("my_service")); + Endpoint ep2 = ep1; + + EXPECT_EQ("127.0.0.1", ep1.ipv4()); + EXPECT_EQ(3306, ep1.port()); + EXPECT_EQ("my_service", ep1.serviceName()); + EXPECT_FALSE(ep1.isSetIpv6()); + EXPECT_EQ("{\"ipv4\":\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\"}", ep1.toJson()); + + EXPECT_EQ(ep1.ipv4(), ep2.ipv4()); + EXPECT_EQ(ep1.port(), ep2.port()); + EXPECT_EQ(ep1.serviceName(), ep2.serviceName()); + EXPECT_FALSE(ep2.isSetIpv6()); + EXPECT_EQ(ep1.toJson(), ep2.toJson()); +} + +TEST(ZipkinCoreTypesAnnotationTest, defaultConstructor) { + Annotation ann; + + EXPECT_EQ(0ULL, ann.timestamp()); + EXPECT_EQ("", ann.value()); + EXPECT_FALSE(ann.isSetEndpoint()); + + uint64_t timestamp = Util::timeSinceEpochMicro(); + ann.setTimestamp(timestamp); + EXPECT_EQ(timestamp, ann.timestamp()); + + ann.setValue(ZipkinCoreConstants::CLIENT_SEND); + EXPECT_EQ(ZipkinCoreConstants::CLIENT_SEND, ann.value()); + + std::string expected_json = "{\"timestamp\":" + std::to_string(timestamp) + ",\"value\":\"" + + ZipkinCoreConstants::CLIENT_SEND + "\"}"; + EXPECT_EQ(expected_json, ann.toJson()); + + // Test the copy-semantics flavor of setEndpoint + + Endpoint ep(std::string("127.0.0.1"), 3306, std::string("my_service")); + ann.setEndpoint(ep); + EXPECT_TRUE(ann.isSetEndpoint()); + EXPECT_EQ("127.0.0.1", ann.endpoint().ipv4()); + EXPECT_EQ(3306, ann.endpoint().port()); + EXPECT_EQ("my_service", ann.endpoint().serviceName()); + EXPECT_FALSE(ann.endpoint().isSetIpv6()); + EXPECT_EQ("{\"ipv4\":\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\"}", + (const_cast(ann.endpoint())).toJson()); + + expected_json = "{\"timestamp\":" + std::to_string(timestamp) + ",\"value\":\"" + + ZipkinCoreConstants::CLIENT_SEND + + "\",\"endpoint\":{\"ipv4\":" + "\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\"}}"; + EXPECT_EQ(expected_json, ann.toJson()); + + // Test the move-semantics flavor of setEndpoint + + Endpoint ep2(std::string("192.168.1.1"), 5555, std::string("my_service_2")); + ann.setEndpoint(std::move(ep2)); + EXPECT_TRUE(ann.isSetEndpoint()); + EXPECT_EQ("192.168.1.1", ann.endpoint().ipv4()); + EXPECT_EQ(5555, ann.endpoint().port()); + EXPECT_EQ("my_service_2", ann.endpoint().serviceName()); + EXPECT_FALSE(ann.endpoint().isSetIpv6()); + EXPECT_EQ("{\"ipv4\":\"192.168.1.1\",\"port\":5555,\"serviceName\":\"my_service_2\"}", + (const_cast(ann.endpoint())).toJson()); + + expected_json = "{\"timestamp\":" + std::to_string(timestamp) + ",\"value\":\"" + + ZipkinCoreConstants::CLIENT_SEND + + "\",\"endpoint\":{\"ipv4\":" + "\"192.168.1.1\",\"port\":5555,\"serviceName\":\"my_service_2\"}}"; + EXPECT_EQ(expected_json, ann.toJson()); +} + +TEST(ZipkinCoreTypesAnnotationTest, customConstructor) { + Endpoint ep(std::string("127.0.0.1"), 3306, std::string("my_service")); + uint64_t timestamp = Util::timeSinceEpochMicro(); + Annotation ann(timestamp, ZipkinCoreConstants::CLIENT_SEND, ep); + + EXPECT_EQ(timestamp, ann.timestamp()); + EXPECT_EQ(ZipkinCoreConstants::CLIENT_SEND, ann.value()); + EXPECT_TRUE(ann.isSetEndpoint()); + + EXPECT_EQ("127.0.0.1", ann.endpoint().ipv4()); + EXPECT_EQ(3306, ann.endpoint().port()); + EXPECT_EQ("my_service", ann.endpoint().serviceName()); + EXPECT_FALSE(ann.endpoint().isSetIpv6()); + EXPECT_EQ("{\"ipv4\":\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\"}", + (const_cast(ann.endpoint())).toJson()); + + std::string expected_json = "{\"timestamp\":" + std::to_string(timestamp) + ",\"value\":\"" + + ZipkinCoreConstants::CLIENT_SEND + + "\",\"endpoint\":{\"ipv4\":" + "\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\"}}"; + EXPECT_EQ(expected_json, ann.toJson()); +} + +TEST(ZipkinCoreTypesAnnotationTest, copyConstructor) { + Endpoint ep(std::string("127.0.0.1"), 3306, std::string("my_service")); + uint64_t timestamp = Util::timeSinceEpochMicro(); + Annotation ann(timestamp, ZipkinCoreConstants::CLIENT_SEND, ep); + Annotation ann2(ann); + + EXPECT_EQ(ann.value(), ann2.value()); + EXPECT_EQ(ann.timestamp(), ann2.timestamp()); + EXPECT_EQ(ann.isSetEndpoint(), ann2.isSetEndpoint()); + EXPECT_EQ(ann.toJson(), ann2.toJson()); + + EXPECT_EQ(ann.endpoint().ipv4(), ann2.endpoint().ipv4()); + EXPECT_EQ(ann.endpoint().port(), ann2.endpoint().port()); + EXPECT_EQ(ann.endpoint().serviceName(), ann2.endpoint().serviceName()); +} + +TEST(ZipkinCoreTypesAnnotationTest, assignmentOperator) { + Endpoint ep(std::string("127.0.0.1"), 3306, std::string("my_service")); + uint64_t timestamp = Util::timeSinceEpochMicro(); + Annotation ann(timestamp, ZipkinCoreConstants::CLIENT_SEND, ep); + Annotation ann2 = ann; + + EXPECT_EQ(ann.value(), ann2.value()); + EXPECT_EQ(ann.timestamp(), ann2.timestamp()); + EXPECT_EQ(ann.isSetEndpoint(), ann2.isSetEndpoint()); + EXPECT_EQ(ann.toJson(), ann2.toJson()); + + EXPECT_EQ(ann.endpoint().ipv4(), ann2.endpoint().ipv4()); + EXPECT_EQ(ann.endpoint().port(), ann2.endpoint().port()); + EXPECT_EQ(ann.endpoint().serviceName(), ann2.endpoint().serviceName()); +} + +TEST(ZipkinCoreTypesBinaryAnnotationTest, defaultConstructor) { + BinaryAnnotation ann; + + EXPECT_EQ("", ann.key()); + EXPECT_EQ("", ann.value()); + EXPECT_FALSE(ann.isSetEndpoint()); + EXPECT_EQ(AnnotationType::STRING, ann.annotationType()); + + ann.setKey("key"); + EXPECT_EQ("key", ann.key()); + + ann.setValue("value"); + EXPECT_EQ("value", ann.value()); + + std::string expected_json = "{\"key\":\"key\",\"value\":\"value\"}"; + EXPECT_EQ(expected_json, ann.toJson()); + + // Test the copy-semantics flavor of setEndpoint + + Endpoint ep(std::string("127.0.0.1"), 3306, std::string("my_service")); + ann.setEndpoint(ep); + EXPECT_TRUE(ann.isSetEndpoint()); + EXPECT_EQ("127.0.0.1", ann.endpoint().ipv4()); + EXPECT_EQ(3306, ann.endpoint().port()); + EXPECT_EQ("my_service", ann.endpoint().serviceName()); + EXPECT_FALSE(ann.endpoint().isSetIpv6()); + EXPECT_EQ("{\"ipv4\":\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\"}", + (const_cast(ann.endpoint())).toJson()); + + expected_json = "{\"key\":\"key\",\"value\":\"value\",\"endpoint\":{\"ipv4\":" + "\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\"}}"; + EXPECT_EQ(expected_json, ann.toJson()); + + // Test the move-semantics flavor of setEndpoint + + Endpoint ep2(std::string("192.168.1.1"), 5555, std::string("my_service_2")); + ann.setEndpoint(std::move(ep2)); + EXPECT_TRUE(ann.isSetEndpoint()); + EXPECT_EQ("192.168.1.1", ann.endpoint().ipv4()); + EXPECT_EQ(5555, ann.endpoint().port()); + EXPECT_EQ("my_service_2", ann.endpoint().serviceName()); + EXPECT_FALSE(ann.endpoint().isSetIpv6()); + EXPECT_EQ("{\"ipv4\":\"192.168.1.1\",\"port\":5555,\"serviceName\":\"my_service_2\"}", + (const_cast(ann.endpoint())).toJson()); + + expected_json = "{\"key\":\"key\",\"value\":\"value\",\"endpoint\":{\"ipv4\":" + "\"192.168.1.1\",\"port\":5555,\"serviceName\":\"my_service_2\"}}"; + EXPECT_EQ(expected_json, ann.toJson()); +} + +TEST(ZipkinCoreTypesBinaryAnnotationTest, customConstructor) { + BinaryAnnotation ann("key", "value"); + + EXPECT_EQ("key", ann.key()); + EXPECT_EQ("value", ann.value()); + EXPECT_FALSE(ann.isSetEndpoint()); + EXPECT_EQ(AnnotationType::STRING, ann.annotationType()); + std::string expected_json = "{\"key\":\"key\",\"value\":\"value\"}"; + EXPECT_EQ(expected_json, ann.toJson()); +} + +TEST(ZipkinCoreTypesBinaryAnnotationTest, copyConstructor) { + BinaryAnnotation ann("key", "value"); + BinaryAnnotation ann2(ann); + + EXPECT_EQ(ann.value(), ann2.value()); + EXPECT_EQ(ann.key(), ann2.key()); + EXPECT_EQ(ann.isSetEndpoint(), ann2.isSetEndpoint()); + EXPECT_EQ(ann.toJson(), ann2.toJson()); + EXPECT_EQ(ann.annotationType(), ann2.annotationType()); +} + +TEST(ZipkinCoreTypesBinaryAnnotationTest, assignmentOperator) { + BinaryAnnotation ann("key", "value"); + BinaryAnnotation ann2 = ann; + + EXPECT_EQ(ann.value(), ann2.value()); + EXPECT_EQ(ann.key(), ann2.key()); + EXPECT_EQ(ann.isSetEndpoint(), ann2.isSetEndpoint()); + EXPECT_EQ(ann.toJson(), ann2.toJson()); + EXPECT_EQ(ann.annotationType(), ann2.annotationType()); +} + +TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { + Span span; + + EXPECT_EQ(0ULL, span.id()); + EXPECT_EQ(0ULL, span.parentId()); + EXPECT_EQ(0ULL, span.traceId()); + EXPECT_EQ(0ULL, span.traceIdHigh()); + EXPECT_EQ("", span.name()); + EXPECT_EQ(0ULL, span.annotations().size()); + EXPECT_EQ(0ULL, span.binaryAnnotations().size()); + EXPECT_EQ("0000000000000000", span.idAsHexString()); + EXPECT_EQ("0000000000000000", span.parentIdAsHexString()); + EXPECT_EQ("0000000000000000", span.traceIdAsHexString()); + EXPECT_EQ(0LL, span.timestamp()); + EXPECT_EQ(0LL, span.duration()); + EXPECT_EQ(0LL, span.startTime()); + EXPECT_FALSE(span.isSet().debug); + EXPECT_FALSE(span.isSet().duration); + EXPECT_FALSE(span.isSet().parent_id); + EXPECT_FALSE(span.isSet().timestamp); + EXPECT_FALSE(span.isSet().trace_id_high); + EXPECT_EQ("{\"traceId\":\"0000000000000000\",\"name\":\"\",\"id\":\"0000000000000000\"," + "\"annotations\":[],\"binaryAnnotations\":[]}", + span.toJson()); + + uint64_t id = Util::generateRandom64(); + std::string id_hex = Util::uint64ToBase16(id); + span.setId(id); + EXPECT_EQ(id, span.id()); + EXPECT_EQ(id_hex, span.idAsHexString()); + + id = Util::generateRandom64(); + id_hex = Util::uint64ToBase16(id); + span.setParentId(id); + EXPECT_EQ(id, span.parentId()); + EXPECT_EQ(id_hex, span.parentIdAsHexString()); + EXPECT_TRUE(span.isSet().parent_id); + + id = Util::generateRandom64(); + id_hex = Util::uint64ToBase16(id); + span.setTraceId(id); + EXPECT_EQ(id, span.traceId()); + EXPECT_EQ(id_hex, span.traceIdAsHexString()); + + id = Util::generateRandom64(); + id_hex = Util::uint64ToBase16(id); + span.setTraceIdHigh(id); + EXPECT_EQ(id, span.traceIdHigh()); + EXPECT_TRUE(span.isSet().trace_id_high); + + int64_t timestamp = Util::timeSinceEpochMicro(); + span.setTimestamp(timestamp); + EXPECT_EQ(timestamp, span.timestamp()); + EXPECT_TRUE(span.isSet().timestamp); + + span.setStartTime(timestamp); + EXPECT_EQ(timestamp, span.startTime()); + + span.setDuration(3000LL); + EXPECT_EQ(3000LL, span.duration()); + EXPECT_TRUE(span.isSet().duration); + + span.setName("span_name"); + EXPECT_EQ("span_name", span.name()); + + span.setDebug(); + EXPECT_TRUE(span.isSet().debug); + + Endpoint endpoint; + Annotation ann; + BinaryAnnotation bann; + std::vector annotations; + std::vector bAnnotations; + + endpoint.setServiceName("my_service_name"); + std::string ip = "192.168.1.2"; + endpoint.setIpv4(ip); + endpoint.setPort(3306); + + ann.setValue(Zipkin::ZipkinCoreConstants::CLIENT_SEND); + ann.setTimestamp(timestamp); + ann.setEndpoint(endpoint); + + annotations.push_back(ann); + span.setAannotations(annotations); + EXPECT_EQ(1ULL, span.annotations().size()); + + bann.setKey(Zipkin::ZipkinCoreConstants::LOCAL_COMPONENT); + bann.setValue("my_component_name"); + bann.setEndpoint(endpoint); + + bAnnotations.push_back(bann); + span.setBinaryAnnotations(bAnnotations); + EXPECT_EQ(1ULL, span.binaryAnnotations().size()); + + EXPECT_EQ( + "{\"traceId\":\"" + span.traceIdAsHexString() + "\",\"name\":\"span_name\",\"id\":\"" + + span.idAsHexString() + "\",\"parentId\":\"" + span.parentIdAsHexString() + + "\",\"timestamp\":" + std::to_string(span.timestamp()) + ",\"duration\":3000," + "\"annotations\":[" + "{\"timestamp\":" + + std::to_string(span.timestamp()) + + ",\"value\":\"cs\",\"endpoint\":" + "{\"ipv4\":\"192.168.1.2\",\"port\":3306,\"serviceName\":\"my_service_name\"}}]," + "\"binaryAnnotations\":[{\"key\":\"lc\",\"value\":\"my_component_name\",\"endpoint\":" + "{\"ipv4\":\"192.168.1.2\",\"port\":3306,\"serviceName\":\"my_service_name\"}}]}", + span.toJson()); + + // Test the copy-semantics flavor of addAnnotation and addBinaryAnnotation + + ann.setValue(Zipkin::ZipkinCoreConstants::SERVER_SEND); + span.addAnnotation(ann); + bann.setKey("http.return_code"); + bann.setValue("200"); + span.addBinaryAnnotation(bann); + + EXPECT_EQ(2ULL, span.annotations().size()); + EXPECT_EQ(2ULL, span.binaryAnnotations().size()); + + // Test the move-semantics flavor of addAnnotation and addBinaryAnnotation + + ann.setValue(Zipkin::ZipkinCoreConstants::SERVER_RECV); + span.addAnnotation(std::move(ann)); + bann.setKey("http.return_code"); + bann.setValue("400"); + span.addBinaryAnnotation(std::move(bann)); + + EXPECT_EQ(3ULL, span.annotations().size()); + EXPECT_EQ(3ULL, span.binaryAnnotations().size()); + + EXPECT_EQ("{\"traceId\":\"" + span.traceIdAsHexString() + "\",\"name\":\"span_name\",\"id\":\"" + + span.idAsHexString() + "\",\"parentId\":\"" + span.parentIdAsHexString() + + "\",\"timestamp\":" + std::to_string(span.timestamp()) + ",\"duration\":3000," + "\"annotations\":[" + "{\"timestamp\":" + + std::to_string(timestamp) + + ",\"value\":\"cs\",\"endpoint\":" + "{\"ipv4\":\"192.168.1.2\",\"port\":3306,\"serviceName\":\"my_service_name\"}}," + "{\"timestamp\":" + + std::to_string(timestamp) + ",\"value\":\"ss\"," + "\"endpoint\":{\"ipv4\":\"192.168.1.2\",\"port\":3306," + "\"serviceName\":\"my_service_name\"}}," + "{\"timestamp\":" + + std::to_string(timestamp) + + ",\"value\":\"sr\"," + "\"endpoint\":{\"ipv4\":\"192.168.1.2\",\"port\":3306,\"serviceName\":\"my_service_" + "name\"}}]," + "\"binaryAnnotations\":[{\"key\":\"lc\",\"value\":\"my_component_name\"," + "\"endpoint\":{\"ipv4\":\"192.168.1.2\",\"port\":3306,\"serviceName\":\"my_service_" + "name\"}}," + "{\"key\":\"http.return_code\",\"value\":\"200\"," + "\"endpoint\":{\"ipv4\":\"192.168.1.2\",\"port\":3306,\"serviceName\":\"my_service_" + "name\"}}," + "{\"key\":\"http.return_code\",\"value\":\"400\"," + "\"endpoint\":{\"ipv4\":\"192.168.1.2\",\"port\":3306,\"serviceName\":\"my_service_" + "name\"}}]}", + span.toJson()); +} + +TEST(ZipkinCoreTypesSpanTest, copyConstructor) { + Span span; + + uint64_t id = Util::generateRandom64(); + std::string id_hex = Util::uint64ToBase16(id); + span.setId(id); + span.setParentId(id); + span.setTraceId(id); + int64_t timestamp = Util::timeSinceEpochMicro(); + span.setTimestamp(timestamp); + span.setDuration(3000LL); + span.setName("span_name"); + + Span span2(span); + + EXPECT_EQ(span.id(), span2.id()); + EXPECT_EQ(span.parentId(), span2.parentId()); + EXPECT_EQ(span.traceId(), span2.traceId()); + EXPECT_EQ(span.traceIdHigh(), span2.traceIdHigh()); + EXPECT_EQ(span.name(), span2.name()); + EXPECT_EQ(span.annotations().size(), span2.annotations().size()); + EXPECT_EQ(span.binaryAnnotations().size(), span2.binaryAnnotations().size()); + EXPECT_EQ(span.idAsHexString(), span2.idAsHexString()); + EXPECT_EQ(span.parentIdAsHexString(), span2.parentIdAsHexString()); + EXPECT_EQ(span.traceIdAsHexString(), span2.traceIdAsHexString()); + EXPECT_EQ(span.timestamp(), span2.timestamp()); + EXPECT_EQ(span.duration(), span2.duration()); + EXPECT_EQ(span.startTime(), span2.startTime()); + EXPECT_EQ(span.isSet().debug, span2.isSet().debug); + EXPECT_EQ(span.isSet().duration, span2.isSet().duration); + EXPECT_EQ(span.isSet().parent_id, span2.isSet().parent_id); + EXPECT_EQ(span.isSet().timestamp, span2.isSet().timestamp); + EXPECT_EQ(span.isSet().trace_id_high, span2.isSet().trace_id_high); +} + +TEST(ZipkinCoreTypesSpanTest, assignmentOperator) { + Span span; + + uint64_t id = Util::generateRandom64(); + std::string id_hex = Util::uint64ToBase16(id); + span.setId(id); + span.setParentId(id); + span.setTraceId(id); + int64_t timestamp = Util::timeSinceEpochMicro(); + span.setTimestamp(timestamp); + span.setDuration(3000LL); + span.setName("span_name"); + + Span span2 = span; + + EXPECT_EQ(span.id(), span2.id()); + EXPECT_EQ(span.parentId(), span2.parentId()); + EXPECT_EQ(span.traceId(), span2.traceId()); + EXPECT_EQ(span.traceIdHigh(), span2.traceIdHigh()); + EXPECT_EQ(span.name(), span2.name()); + EXPECT_EQ(span.annotations().size(), span2.annotations().size()); + EXPECT_EQ(span.binaryAnnotations().size(), span2.binaryAnnotations().size()); + EXPECT_EQ(span.idAsHexString(), span2.idAsHexString()); + EXPECT_EQ(span.parentIdAsHexString(), span2.parentIdAsHexString()); + EXPECT_EQ(span.traceIdAsHexString(), span2.traceIdAsHexString()); + EXPECT_EQ(span.timestamp(), span2.timestamp()); + EXPECT_EQ(span.duration(), span2.duration()); + EXPECT_EQ(span.startTime(), span2.startTime()); + EXPECT_EQ(span.isSet().debug, span2.isSet().debug); + EXPECT_EQ(span.isSet().duration, span2.isSet().duration); + EXPECT_EQ(span.isSet().parent_id, span2.isSet().parent_id); + EXPECT_EQ(span.isSet().timestamp, span2.isSet().timestamp); + EXPECT_EQ(span.isSet().trace_id_high, span2.isSet().trace_id_high); +} + +TEST(ZipkinCoreTypesSpanTest, setTag) { + Span span; + + span.setTag("key1", "value1"); + span.setTag("key2", "value2"); + + EXPECT_EQ(2ULL, span.binaryAnnotations().size()); + + BinaryAnnotation bann = span.binaryAnnotations()[0]; + EXPECT_EQ("key1", bann.key()); + EXPECT_EQ("value1", bann.value()); + + bann = span.binaryAnnotations()[1]; + EXPECT_EQ("key2", bann.key()); + EXPECT_EQ("value2", bann.value()); +} +} // Zipkin From 6f1b7786c2a82652d8947312073d1428432a47b5 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Mon, 10 Apr 2017 23:22:14 -0400 Subject: [PATCH 18/55] Fixed file permissions (#430) --- test/zipkin/span_buffer_test.cc | 0 test/zipkin/span_context_test.cc | 0 test/zipkin/tracer_test.cc | 0 test/zipkin/util_test.cc | 0 test/zipkin/zipkin_core_types_test.cc | 0 5 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 test/zipkin/span_buffer_test.cc mode change 100755 => 100644 test/zipkin/span_context_test.cc mode change 100755 => 100644 test/zipkin/tracer_test.cc mode change 100755 => 100644 test/zipkin/util_test.cc mode change 100755 => 100644 test/zipkin/zipkin_core_types_test.cc diff --git a/test/zipkin/span_buffer_test.cc b/test/zipkin/span_buffer_test.cc old mode 100755 new mode 100644 diff --git a/test/zipkin/span_context_test.cc b/test/zipkin/span_context_test.cc old mode 100755 new mode 100644 diff --git a/test/zipkin/tracer_test.cc b/test/zipkin/tracer_test.cc old mode 100755 new mode 100644 diff --git a/test/zipkin/util_test.cc b/test/zipkin/util_test.cc old mode 100755 new mode 100644 diff --git a/test/zipkin/zipkin_core_types_test.cc b/test/zipkin/zipkin_core_types_test.cc old mode 100755 new mode 100644 From 0c79766f065c7e9195eeaac4293fcc38820831dc Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Tue, 11 Apr 2017 13:06:00 -0400 Subject: [PATCH 19/55] Added test to verify finishing a span with an SR annotation (#430) --- test/zipkin/tracer_test.cc | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/zipkin/tracer_test.cc b/test/zipkin/tracer_test.cc index cf27a0477205f..3cd619a05d871 100644 --- a/test/zipkin/tracer_test.cc +++ b/test/zipkin/tracer_test.cc @@ -164,5 +164,38 @@ TEST(ZipkinTracerTest, finishSpan) { EXPECT_EQ(9000, endpoint.port()); EXPECT_EQ("my_service_name", endpoint.serviceName()); EXPECT_FALSE(endpoint.isSetIpv6()); + + // ============== + // Test finishing a span containing an SR annotation + // ============== + + SpanContext context(span); + Span server_side = tracer.startSpan("my_span", timestamp, context); + + // Finishing a server-side span with an SR annotation must add an SS annotation + server_side.finish(); + EXPECT_EQ(2ULL, server_side.annotations().size()); + + // Check the SR annotation added at span-creation time + ann = server_side.annotations()[0]; + EXPECT_EQ(ZipkinCoreConstants::SERVER_RECV, ann.value()); + EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set + EXPECT_TRUE(ann.isSetEndpoint()); + endpoint = ann.endpoint(); + EXPECT_EQ("127.0.0.1", endpoint.ipv4()); + EXPECT_EQ(9000, endpoint.port()); + EXPECT_EQ("my_service_name", endpoint.serviceName()); + EXPECT_FALSE(endpoint.isSetIpv6()); + + // Check the SS annotation added when ending the span + ann = server_side.annotations()[1]; + EXPECT_EQ(ZipkinCoreConstants::SERVER_SEND, ann.value()); + EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set + EXPECT_TRUE(ann.isSetEndpoint()); + endpoint = ann.endpoint(); + EXPECT_EQ("127.0.0.1", endpoint.ipv4()); + EXPECT_EQ(9000, endpoint.port()); + EXPECT_EQ("my_service_name", endpoint.serviceName()); + EXPECT_FALSE(endpoint.isSetIpv6()); } } // Zipkin From ea4e8f4076e0c20a7bf0a53aefded6e533478d88 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Tue, 11 Apr 2017 13:34:33 -0400 Subject: [PATCH 20/55] Added test to check SpanContext's behavior when setting Span's traceIdHigh (#430) --- test/zipkin/span_context_test.cc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/zipkin/span_context_test.cc b/test/zipkin/span_context_test.cc index 339fbac951def..6ad70cdae7ad4 100644 --- a/test/zipkin/span_context_test.cc +++ b/test/zipkin/span_context_test.cc @@ -114,6 +114,14 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { EXPECT_FALSE(spanContext2.isSetAnnotation().ss); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c", spanContext2.serializeToString()); + // Test if we can handle 128-bit trace ids + EXPECT_FALSE(span.isSet().trace_id_high); + span.setTraceIdHigh(9922130815203937912ULL); + EXPECT_TRUE(span.isSet().trace_id_high); + SpanContext spanContext5(span); + // We currently drop the high bits. So, we expect the same context as above + EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c", spanContext5.serializeToString()); + // Span context populated with trace id, id, parent id, and one annotation Annotation ann; ann.setValue(ZipkinCoreConstants::SERVER_RECV); From fdd39330b73a912d0dee5c23a82bbe7e99e92740 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Wed, 12 Apr 2017 10:26:01 -0400 Subject: [PATCH 21/55] Moved the Zipkin library to common/tracing/zipkin. Removed precompiled header files. (#430) --- source/CMakeLists.txt | 1 - source/common/CMakeLists.txt | 143 ++++++++++++++++++ source/{ => common/tracing}/zipkin/BUILD | 0 .../tracing}/zipkin/span_buffer.cc | 3 +- .../{ => common/tracing}/zipkin/span_buffer.h | 5 +- .../tracing}/zipkin/span_context.cc | 8 +- .../tracing}/zipkin/span_context.h | 7 +- source/{ => common/tracing}/zipkin/tracer.cc | 8 +- source/{ => common/tracing}/zipkin/tracer.h | 6 +- .../tracing}/zipkin/tracer_interface.h | 0 source/{ => common/tracing}/zipkin/util.cc | 5 +- source/{ => common/tracing}/zipkin/util.h | 4 - .../tracing}/zipkin/zipkin_core_constants.cc | 2 +- .../tracing}/zipkin/zipkin_core_constants.h | 2 - .../tracing}/zipkin/zipkin_core_types.cc | 10 +- .../tracing}/zipkin/zipkin_core_types.h | 8 +- .../zipkin/zipkin_json_field_names.cc | 2 +- .../tracing}/zipkin/zipkin_json_field_names.h | 2 - source/exe/CMakeLists.txt | 2 +- source/zipkin/CMakeLists.txt | 14 -- test/CMakeLists.txt | 13 +- .../tracing}/zipkin/span_buffer_test.cc | 2 +- .../tracing}/zipkin/span_context_test.cc | 4 +- .../tracing}/zipkin/tracer_test.cc | 6 +- test/{ => common/tracing}/zipkin/util_test.cc | 2 +- .../tracing}/zipkin/zipkin_core_types_test.cc | 4 +- 26 files changed, 181 insertions(+), 82 deletions(-) create mode 100644 source/common/CMakeLists.txt rename source/{ => common/tracing}/zipkin/BUILD (100%) rename source/{ => common/tracing}/zipkin/span_buffer.cc (91%) rename source/{ => common/tracing}/zipkin/span_buffer.h (89%) rename source/{ => common/tracing}/zipkin/span_context.cc (96%) rename source/{ => common/tracing}/zipkin/span_context.h (91%) rename source/{ => common/tracing}/zipkin/tracer.cc (96%) rename source/{ => common/tracing}/zipkin/tracer.h (89%) rename source/{ => common/tracing}/zipkin/tracer_interface.h (100%) rename source/{ => common/tracing}/zipkin/util.cc (95%) rename source/{ => common/tracing}/zipkin/util.h (91%) rename source/{ => common/tracing}/zipkin/zipkin_core_constants.cc (97%) rename source/{ => common/tracing}/zipkin/zipkin_core_constants.h (98%) rename source/{ => common/tracing}/zipkin/zipkin_core_types.cc (96%) rename source/{ => common/tracing}/zipkin/zipkin_core_types.h (98%) rename source/{ => common/tracing}/zipkin/zipkin_json_field_names.cc (95%) rename source/{ => common/tracing}/zipkin/zipkin_json_field_names.h (97%) delete mode 100644 source/zipkin/CMakeLists.txt rename test/{ => common/tracing}/zipkin/span_buffer_test.cc (98%) rename test/{ => common/tracing}/zipkin/span_context_test.cc (98%) rename test/{ => common/tracing}/zipkin/tracer_test.cc (98%) rename test/{ => common/tracing}/zipkin/util_test.cc (97%) rename test/{ => common/tracing}/zipkin/zipkin_core_types_test.cc (99%) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 88c9fae4edb88..52e699aadb358 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -1,4 +1,3 @@ add_subdirectory(common) add_subdirectory(exe) add_subdirectory(server) -add_subdirectory(zipkin) diff --git a/source/common/CMakeLists.txt b/source/common/CMakeLists.txt new file mode 100644 index 0000000000000..f101acb25c3ae --- /dev/null +++ b/source/common/CMakeLists.txt @@ -0,0 +1,143 @@ +set(gen_git_sha_target ${CMAKE_CURRENT_BINARY_DIR}/version_generated.cc) +add_custom_target( + gen_git_sha ALL + COMMAND ${PROJECT_SOURCE_DIR}/tools/gen_git_sha.sh ${CMAKE_SOURCE_DIR} ${gen_git_sha_target}) + +add_custom_command( + OUTPUT ${gen_git_sha_target} + DEPENDS gen_git_sha) + +file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/generated) +add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated/ratelimit.pb.h ${CMAKE_CURRENT_BINARY_DIR}/generated/ratelimit.pb.cc + COMMAND ${ENVOY_PROTOBUF_PROTOC} -I=${CMAKE_CURRENT_SOURCE_DIR}/ratelimit/ + --cpp_out=${CMAKE_CURRENT_BINARY_DIR}/generated + ${CMAKE_CURRENT_SOURCE_DIR}/ratelimit/ratelimit.proto + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/ratelimit/ratelimit.proto +) + +set_source_files_properties(generated/ratelimit.pb.cc PROPERTIES COMPILE_FLAGS -Wno-unused-parameter) + +add_library( + envoy-common OBJECT + api/api_impl.cc + access_log/access_log_manager_impl.cc + buffer/buffer_impl.cc + common/base64.cc + common/hex.cc + common/logger.cc + common/thread.cc + common/utility.cc + common/version.cc + dynamo/dynamo_filter.cc + dynamo/dynamo_request_parser.cc + dynamo/dynamo_utility.cc + event/dispatcher_impl.cc + event/event_impl_base.cc + event/file_event_impl.cc + event/libevent.cc + event/signal_impl.cc + event/timer_impl.cc + filesystem/filesystem_impl.cc + filesystem/watcher_impl.cc + filter/auth/client_ssl.cc + filter/echo.cc + filter/ratelimit.cc + filter/tcp_proxy.cc + generated/ratelimit.pb.cc + grpc/codec.cc + grpc/common.cc + grpc/http1_bridge_filter.cc + grpc/rpc_channel_impl.cc + http/access_log/access_log_formatter.cc + http/access_log/access_log_impl.cc + http/async_client_impl.cc + http/codec_client.cc + http/codes.cc + http/conn_manager_impl.cc + http/conn_manager_utility.cc + http/date_provider_impl.cc + http/header_map_impl.cc + http/message_impl.cc + http/http1/codec_impl.cc + http/http1/conn_pool.cc + http/http2/codec_impl.cc + http/http2/conn_pool.cc + http/filter/buffer_filter.cc + http/filter/fault_filter.cc + http/filter/ratelimit.cc + http/rest_api_fetcher.cc + http/user_agent.cc + http/utility.cc + json/config_schemas.cc + json/json_loader.cc + memory/stats.cc + mongo/bson_impl.cc + mongo/codec_impl.cc + mongo/proxy.cc + mongo/utility.cc + network/address_impl.cc + network/cidr_range.cc + network/connection_impl.cc + network/dns_impl.cc + network/filter_manager_impl.cc + network/listener_impl.cc + network/listen_socket_impl.cc + network/proxy_protocol.cc + network/utility.cc + profiler/profiler.cc + ratelimit/ratelimit_impl.cc + redis/codec_impl.cc + redis/command_splitter_impl.cc + redis/conn_pool_impl.cc + redis/proxy_filter.cc + router/config_impl.cc + router/config_utility.cc + router/rds_impl.cc + router/retry_state_impl.cc + router/router.cc + router/router_ratelimit.cc + router/shadow_writer_impl.cc + runtime/runtime_impl.cc + runtime/uuid_util.cc + ssl/connection_impl.cc + ssl/context_config_impl.cc + ssl/context_impl.cc + ssl/context_manager_impl.cc + stats/stats_impl.cc + stats/statsd.cc + stats/thread_local_store.cc + thread_local/thread_local_impl.cc + tracing/http_tracer_impl.cc + tracing/lightstep_tracer_impl.cc + tracing/zipkin/span_buffer.cc + tracing/zipkin/span_context.cc + tracing/zipkin/tracer.cc + tracing/zipkin/util.cc + tracing/zipkin/zipkin_core_constants.cc + tracing/zipkin/zipkin_core_types.cc + tracing/zipkin/zipkin_json_field_names.cc + upstream/cds_api_impl.cc + upstream/cluster_manager_impl.cc + upstream/health_checker_impl.cc + upstream/host_utility.cc + upstream/load_balancer_impl.cc + upstream/logical_dns_cluster.cc + upstream/outlier_detection_impl.cc + upstream/ring_hash_lb.cc + upstream/sds.cc + upstream/upstream_impl.cc + ${gen_git_sha_target}) + +include_directories(SYSTEM ${ENVOY_HTTP_PARSER_INCLUDE_DIR}) +include_directories(${ENVOY_RAPIDJSON_INCLUDE_DIR}) + +if (NOT ENVOY_SANITIZE) + include_directories(${ENVOY_GPERFTOOLS_INCLUDE_DIR}) +endif() + +include_directories(${ENVOY_CARES_INCLUDE_DIR}) +include_directories(${ENVOY_LIBEVENT_INCLUDE_DIR}) +include_directories(${ENVOY_NGHTTP2_INCLUDE_DIR}) +include_directories(SYSTEM ${ENVOY_OPENSSL_INCLUDE_DIR}) +include_directories(SYSTEM ${ENVOY_LIGHTSTEP_TRACER_INCLUDE_DIR}) diff --git a/source/zipkin/BUILD b/source/common/tracing/zipkin/BUILD similarity index 100% rename from source/zipkin/BUILD rename to source/common/tracing/zipkin/BUILD diff --git a/source/zipkin/span_buffer.cc b/source/common/tracing/zipkin/span_buffer.cc similarity index 91% rename from source/zipkin/span_buffer.cc rename to source/common/tracing/zipkin/span_buffer.cc index fe8ccf035acad..5830a4c223a33 100644 --- a/source/zipkin/span_buffer.cc +++ b/source/common/tracing/zipkin/span_buffer.cc @@ -1,4 +1,4 @@ -#include "zipkin/span_buffer.h" +#include "common/tracing/zipkin/span_buffer.h" namespace Zipkin { @@ -12,7 +12,6 @@ bool SpanBuffer::addSpan(Span&& span) { // Buffer full return false; } - // span_buffer_[next_position_++] = &span; span_buffer_[next_position_++] = std::make_shared(span); return true; diff --git a/source/zipkin/span_buffer.h b/source/common/tracing/zipkin/span_buffer.h similarity index 89% rename from source/zipkin/span_buffer.h rename to source/common/tracing/zipkin/span_buffer.h index 47e3d5c95c408..89c236e47d7c5 100644 --- a/source/zipkin/span_buffer.h +++ b/source/common/tracing/zipkin/span_buffer.h @@ -1,9 +1,6 @@ #pragma once -#include -#include - -#include "zipkin/zipkin_core_types.h" +#include "common/tracing/zipkin/zipkin_core_types.h" namespace Zipkin { diff --git a/source/zipkin/span_context.cc b/source/common/tracing/zipkin/span_context.cc similarity index 96% rename from source/zipkin/span_context.cc rename to source/common/tracing/zipkin/span_context.cc index 393fb111ae1b0..6c621228e896e 100644 --- a/source/zipkin/span_context.cc +++ b/source/common/tracing/zipkin/span_context.cc @@ -1,9 +1,5 @@ -#include -#include -#include - -#include "zipkin/zipkin_core_constants.h" -#include "zipkin/span_context.h" +#include "common/tracing/zipkin/zipkin_core_constants.h" +#include "common/tracing/zipkin/span_context.h" namespace Zipkin { diff --git a/source/zipkin/span_context.h b/source/common/tracing/zipkin/span_context.h similarity index 91% rename from source/zipkin/span_context.h rename to source/common/tracing/zipkin/span_context.h index ef7a15a36fc97..e0a70b945dc25 100644 --- a/source/zipkin/span_context.h +++ b/source/common/tracing/zipkin/span_context.h @@ -1,10 +1,7 @@ #pragma once -#include -#include - -#include "zipkin/zipkin_core_types.h" -#include "zipkin/util.h" +#include "common/tracing/zipkin/util.h" +#include "common/tracing/zipkin/zipkin_core_types.h" namespace Zipkin { diff --git a/source/zipkin/tracer.cc b/source/common/tracing/zipkin/tracer.cc similarity index 96% rename from source/zipkin/tracer.cc rename to source/common/tracing/zipkin/tracer.cc index f870b8cf12148..aa58ce167fa8d 100644 --- a/source/zipkin/tracer.cc +++ b/source/common/tracing/zipkin/tracer.cc @@ -1,8 +1,6 @@ -#include - -#include "zipkin/util.h" -#include "zipkin/tracer.h" -#include "zipkin/zipkin_core_constants.h" +#include "common/tracing/zipkin/util.h" +#include "common/tracing/zipkin/zipkin_core_constants.h" +#include "common/tracing/zipkin/tracer.h" namespace Zipkin { diff --git a/source/zipkin/tracer.h b/source/common/tracing/zipkin/tracer.h similarity index 89% rename from source/zipkin/tracer.h rename to source/common/tracing/zipkin/tracer.h index 6696db574aa55..39346f9db4dab 100644 --- a/source/zipkin/tracer.h +++ b/source/common/tracing/zipkin/tracer.h @@ -1,8 +1,8 @@ #pragma once -#include "zipkin/zipkin_core_types.h" -#include "zipkin/span_context.h" -#include "zipkin/tracer_interface.h" +#include "common/tracing/zipkin/span_context.h" +#include "common/tracing/zipkin/tracer_interface.h" +#include "common/tracing/zipkin/zipkin_core_types.h" namespace Zipkin { diff --git a/source/zipkin/tracer_interface.h b/source/common/tracing/zipkin/tracer_interface.h similarity index 100% rename from source/zipkin/tracer_interface.h rename to source/common/tracing/zipkin/tracer_interface.h diff --git a/source/zipkin/util.cc b/source/common/tracing/zipkin/util.cc similarity index 95% rename from source/zipkin/util.cc rename to source/common/tracing/zipkin/util.cc index 5ce4d9bb96a61..96901033e1bb8 100644 --- a/source/zipkin/util.cc +++ b/source/common/tracing/zipkin/util.cc @@ -1,13 +1,10 @@ -#include -#include -#include #include #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" #include "rapidjson/document.h" -#include "zipkin/util.h" +#include "common/tracing/zipkin/util.h" namespace Zipkin { diff --git a/source/zipkin/util.h b/source/common/tracing/zipkin/util.h similarity index 91% rename from source/zipkin/util.h rename to source/common/tracing/zipkin/util.h index 4abbcd9ae0d09..9090695b85b45 100644 --- a/source/zipkin/util.h +++ b/source/common/tracing/zipkin/util.h @@ -1,9 +1,5 @@ #pragma once -#include -#include -#include - namespace Zipkin { typedef std::chrono::system_clock Duration; diff --git a/source/zipkin/zipkin_core_constants.cc b/source/common/tracing/zipkin/zipkin_core_constants.cc similarity index 97% rename from source/zipkin/zipkin_core_constants.cc rename to source/common/tracing/zipkin/zipkin_core_constants.cc index eb6c91132c9b0..0c83c2941e6db 100644 --- a/source/zipkin/zipkin_core_constants.cc +++ b/source/common/tracing/zipkin/zipkin_core_constants.cc @@ -1,4 +1,4 @@ -#include "zipkin/zipkin_core_constants.h" +#include "common/tracing/zipkin/zipkin_core_constants.h" const std::string Zipkin::ZipkinCoreConstants::CLIENT_SEND = "cs"; const std::string Zipkin::ZipkinCoreConstants::CLIENT_RECV = "cr"; diff --git a/source/zipkin/zipkin_core_constants.h b/source/common/tracing/zipkin/zipkin_core_constants.h similarity index 98% rename from source/zipkin/zipkin_core_constants.h rename to source/common/tracing/zipkin/zipkin_core_constants.h index 9021aa286c0f4..4b93deecbaa42 100644 --- a/source/zipkin/zipkin_core_constants.h +++ b/source/common/tracing/zipkin/zipkin_core_constants.h @@ -1,7 +1,5 @@ #pragma once -#include - namespace Zipkin { class ZipkinCoreConstants { diff --git a/source/zipkin/zipkin_core_types.cc b/source/common/tracing/zipkin/zipkin_core_types.cc similarity index 96% rename from source/zipkin/zipkin_core_types.cc rename to source/common/tracing/zipkin/zipkin_core_types.cc index 0ab3188226273..d9d862efef142 100644 --- a/source/zipkin/zipkin_core_types.cc +++ b/source/common/tracing/zipkin/zipkin_core_types.cc @@ -1,11 +1,11 @@ #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" -#include "zipkin/zipkin_core_types.h" -#include "zipkin/zipkin_core_constants.h" -#include "zipkin/zipkin_json_field_names.h" -#include "zipkin/util.h" -#include "zipkin/span_context.h" +#include "common/tracing/zipkin/span_context.h" +#include "common/tracing/zipkin/util.h" +#include "common/tracing/zipkin/zipkin_core_constants.h" +#include "common/tracing/zipkin/zipkin_json_field_names.h" +#include "common/tracing/zipkin/zipkin_core_types.h" namespace Zipkin { diff --git a/source/zipkin/zipkin_core_types.h b/source/common/tracing/zipkin/zipkin_core_types.h similarity index 98% rename from source/zipkin/zipkin_core_types.h rename to source/common/tracing/zipkin/zipkin_core_types.h index 218d49339d631..9e766079e22f7 100644 --- a/source/zipkin/zipkin_core_types.h +++ b/source/common/tracing/zipkin/zipkin_core_types.h @@ -1,11 +1,7 @@ #pragma once -#include -#include -#include - -#include "zipkin/util.h" -#include "zipkin/tracer_interface.h" +#include "common/tracing/zipkin/tracer_interface.h" +#include "common/tracing/zipkin/util.h" namespace Zipkin { diff --git a/source/zipkin/zipkin_json_field_names.cc b/source/common/tracing/zipkin/zipkin_json_field_names.cc similarity index 95% rename from source/zipkin/zipkin_json_field_names.cc rename to source/common/tracing/zipkin/zipkin_json_field_names.cc index b013ccbe118a9..1e7af66c86a81 100644 --- a/source/zipkin/zipkin_json_field_names.cc +++ b/source/common/tracing/zipkin/zipkin_json_field_names.cc @@ -1,4 +1,4 @@ -#include "zipkin/zipkin_json_field_names.h" +#include "common/tracing/zipkin/zipkin_json_field_names.h" const std::string Zipkin::ZipkinJsonFieldNames::SPAN_TRACE_ID = "traceId"; const std::string Zipkin::ZipkinJsonFieldNames::SPAN_PARENT_ID = "parentId"; diff --git a/source/zipkin/zipkin_json_field_names.h b/source/common/tracing/zipkin/zipkin_json_field_names.h similarity index 97% rename from source/zipkin/zipkin_json_field_names.h rename to source/common/tracing/zipkin/zipkin_json_field_names.h index cf49d5d67bedb..7e7d6410c71ca 100644 --- a/source/zipkin/zipkin_json_field_names.h +++ b/source/common/tracing/zipkin/zipkin_json_field_names.h @@ -1,7 +1,5 @@ #pragma once -#include - namespace Zipkin { class ZipkinJsonFieldNames { diff --git a/source/exe/CMakeLists.txt b/source/exe/CMakeLists.txt index 3c84add4cc039..0348c0226edf0 100644 --- a/source/exe/CMakeLists.txt +++ b/source/exe/CMakeLists.txt @@ -1,4 +1,4 @@ -add_executable(envoy hot_restart.cc main.cc $ $ $ ${ENVOY_EXE_EXTRA_OBJECTS}) +add_executable(envoy hot_restart.cc main.cc $ $ ${ENVOY_EXE_EXTRA_OBJECTS}) if (ENVOY_TCMALLOC) target_link_libraries(envoy tcmalloc_and_profiler) diff --git a/source/zipkin/CMakeLists.txt b/source/zipkin/CMakeLists.txt deleted file mode 100644 index e381e7c37aab1..0000000000000 --- a/source/zipkin/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -add_library( - zipkin OBJECT - tracer.cc - util.cc - zipkin_core_constants.cc - zipkin_core_types.cc - zipkin_json_field_names.cc - span_context.cc - span_buffer.cc -) - -include_directories(${ENVOY_RAPIDJSON_INCLUDE_DIR}) - -cotire(zipkin) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 96b22ef93c80c..c452c6a034832 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,7 +26,6 @@ include_directories(${ENVOY_LIBEVENT_INCLUDE_DIR}) add_executable(envoy-test $ $ - $ ${ENVOY_TEST_EXTRA_OBJECTS} common/access_log/access_log_manager_impl_test.cc common/api/api_impl_test.cc @@ -101,6 +100,11 @@ add_executable(envoy-test common/stats/thread_local_store_test.cc common/tracing/http_tracer_impl_test.cc common/tracing/lightstep_tracer_impl_test.cc + common/tracing/zipkin/span_buffer_test.cc + common/tracing/zipkin/span_context_test.cc + common/tracing/zipkin/util_test.cc + common/tracing/zipkin/tracer_test.cc + common/tracing/zipkin/zipkin_core_types_test.cc common/upstream/cds_api_impl_test.cc common/upstream/cluster_manager_impl_test.cc common/upstream/health_checker_impl_test.cc @@ -161,12 +165,7 @@ add_executable(envoy-test test_common/network_utility.cc test_common/network_utility_test.cc test_common/printers.cc - test_common/utility.cc - zipkin/span_buffer_test.cc - zipkin/span_context_test.cc - zipkin/util_test.cc - zipkin/tracer_test.cc - zipkin/zipkin_core_types_test.cc) + test_common/utility.cc) # The MOCK_METHOD* macros from gtest triggers this clang warning and it's hard # to work around, so we just ignore it. diff --git a/test/zipkin/span_buffer_test.cc b/test/common/tracing/zipkin/span_buffer_test.cc similarity index 98% rename from test/zipkin/span_buffer_test.cc rename to test/common/tracing/zipkin/span_buffer_test.cc index 91cac526127c2..8a7c82b19ca43 100644 --- a/test/zipkin/span_buffer_test.cc +++ b/test/common/tracing/zipkin/span_buffer_test.cc @@ -1,4 +1,4 @@ -#include "zipkin/span_buffer.h" +#include "common/tracing/zipkin/span_buffer.h" #include "gtest/gtest.h" diff --git a/test/zipkin/span_context_test.cc b/test/common/tracing/zipkin/span_context_test.cc similarity index 98% rename from test/zipkin/span_context_test.cc rename to test/common/tracing/zipkin/span_context_test.cc index 6ad70cdae7ad4..a63a5093a507b 100644 --- a/test/zipkin/span_context_test.cc +++ b/test/common/tracing/zipkin/span_context_test.cc @@ -1,5 +1,5 @@ -#include "zipkin/span_context.h" -#include "zipkin/zipkin_core_constants.h" +#include "common/tracing/zipkin/span_context.h" +#include "common/tracing/zipkin/zipkin_core_constants.h" #include "gtest/gtest.h" diff --git a/test/zipkin/tracer_test.cc b/test/common/tracing/zipkin/tracer_test.cc similarity index 98% rename from test/zipkin/tracer_test.cc rename to test/common/tracing/zipkin/tracer_test.cc index 3cd619a05d871..873901e33c734 100644 --- a/test/zipkin/tracer_test.cc +++ b/test/common/tracing/zipkin/tracer_test.cc @@ -1,6 +1,6 @@ -#include "zipkin/tracer.h" -#include "zipkin/util.h" -#include "zipkin/zipkin_core_constants.h" +#include "common/tracing/zipkin/tracer.h" +#include "common/tracing/zipkin/util.h" +#include "common/tracing/zipkin/zipkin_core_constants.h" #include "gtest/gtest.h" diff --git a/test/zipkin/util_test.cc b/test/common/tracing/zipkin/util_test.cc similarity index 97% rename from test/zipkin/util_test.cc rename to test/common/tracing/zipkin/util_test.cc index 5429894e0358b..9a9942fd5f18d 100644 --- a/test/zipkin/util_test.cc +++ b/test/common/tracing/zipkin/util_test.cc @@ -1,4 +1,4 @@ -#include "zipkin/util.h" +#include "common/tracing/zipkin/util.h" #include "gtest/gtest.h" diff --git a/test/zipkin/zipkin_core_types_test.cc b/test/common/tracing/zipkin/zipkin_core_types_test.cc similarity index 99% rename from test/zipkin/zipkin_core_types_test.cc rename to test/common/tracing/zipkin/zipkin_core_types_test.cc index bcacaa971ce59..cb3ad365387a2 100644 --- a/test/zipkin/zipkin_core_types_test.cc +++ b/test/common/tracing/zipkin/zipkin_core_types_test.cc @@ -1,5 +1,5 @@ -#include "zipkin/zipkin_core_types.h" -#include "zipkin/zipkin_core_constants.h" +#include "common/tracing/zipkin/zipkin_core_types.h" +#include "common/tracing/zipkin/zipkin_core_constants.h" #include "gtest/gtest.h" From 63f9183261b5862229c8e87fd01a78a536ced990 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Wed, 12 Apr 2017 11:21:13 -0400 Subject: [PATCH 22/55] Make sure all variables use snake case (#430) --- source/common/tracing/zipkin/span_buffer.cc | 12 +++++----- source/common/tracing/zipkin/tracer.cc | 26 ++++++++++----------- source/common/tracing/zipkin/util.cc | 24 +++++++++---------- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/source/common/tracing/zipkin/span_buffer.cc b/source/common/tracing/zipkin/span_buffer.cc index 5830a4c223a33..b9c6182e457b8 100644 --- a/source/common/tracing/zipkin/span_buffer.cc +++ b/source/common/tracing/zipkin/span_buffer.cc @@ -23,17 +23,17 @@ void SpanBuffer::flush() { } std::string SpanBuffer::toStringifiedJsonArray() { - std::string stringifiedJsonArray = "["; + std::string stringified_json_array = "["; if (pendingSpans()) { - stringifiedJsonArray += span_buffer_[0]->toJson(); + stringified_json_array += span_buffer_[0]->toJson(); for (uint64_t i = 1; i < next_position_; i++) { - stringifiedJsonArray += ","; - stringifiedJsonArray += span_buffer_[i]->toJson(); + stringified_json_array += ","; + stringified_json_array += span_buffer_[i]->toJson(); } } - stringifiedJsonArray += "]"; + stringified_json_array += "]"; - return stringifiedJsonArray; + return stringified_json_array; } } // Zipkin diff --git a/source/common/tracing/zipkin/tracer.cc b/source/common/tracing/zipkin/tracer.cc index aa58ce167fa8d..9da23109c4e05 100644 --- a/source/common/tracing/zipkin/tracer.cc +++ b/source/common/tracing/zipkin/tracer.cc @@ -10,7 +10,7 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time) { std::string ip; uint16_t port; Endpoint ep; - uint64_t timestampMicro; + uint64_t timestamp_micro; // Build the endpoint getIPAndPort(address_, ip, port); @@ -24,15 +24,15 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time) { // Create an all-new span, with no parent id span.setName(span_name); - uint64_t randonNumber = Util::generateRandom64(); - span.setId(randonNumber); - span.setTraceId(randonNumber); + uint64_t randon_number = Util::generateRandom64(); + span.setId(randon_number); + span.setTraceId(randon_number); span.setStartTime(start_time); // Set the timestamp globally for the span and also for the CS annotation - timestampMicro = Util::timeSinceEpochMicro(); - cs.setTimestamp(timestampMicro); - span.setTimestamp(timestampMicro); + timestamp_micro = Util::timeSinceEpochMicro(); + cs.setTimestamp(timestamp_micro); + span.setTimestamp(timestamp_micro); // Add CS annotation to the span span.addAnnotation(std::move(cs)); @@ -49,18 +49,18 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time, std::string ip; uint16_t port; Endpoint ep; - uint64_t timestampMicro; + uint64_t timestamp_micro; // TODO We currently ignore the start_time to set the span/annotation timestamps // Is start_time really needed? - timestampMicro = Util::timeSinceEpochMicro(); + timestamp_micro = Util::timeSinceEpochMicro(); if ((previous_context.isSetAnnotation().sr) && (!previous_context.isSetAnnotation().cs)) { // We need to create a new span that is a child of the previous span; no shared context // Create a new span id - uint64_t randonNumber = Util::generateRandom64(); - span.setId(randonNumber); + uint64_t randon_number = Util::generateRandom64(); + span.setId(randon_number); span.setName(span_name); @@ -71,7 +71,7 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time, annotation.setValue(ZipkinCoreConstants::CLIENT_SEND); // Set the timestamp globally for the span - span.setTimestamp(timestampMicro); + span.setTimestamp(timestamp_micro); } else if ((previous_context.isSetAnnotation().cs) && (!previous_context.isSetAnnotation().sr)) { // We need to create a new span that will share context with the previous span @@ -98,7 +98,7 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time, // Add the newly-created annotation to the span annotation.setEndpoint(std::move(ep)); - annotation.setTimestamp(timestampMicro); + annotation.setTimestamp(timestamp_micro); span.addAnnotation(std::move(annotation)); // Keep the same trace id diff --git a/source/common/tracing/zipkin/util.cc b/source/common/tracing/zipkin/util.cc index 96901033e1bb8..9af3d79e88869 100644 --- a/source/common/tracing/zipkin/util.cc +++ b/source/common/tracing/zipkin/util.cc @@ -10,33 +10,33 @@ namespace Zipkin { void Util::mergeJsons(std::string& target, const std::string& source, const std::string& field_name) { - rapidjson::Document targetDoc, sourceDoc; - targetDoc.Parse(target.c_str()); - sourceDoc.Parse(source.c_str()); + rapidjson::Document target_doc, source_doc; + target_doc.Parse(target.c_str()); + source_doc.Parse(source.c_str()); - targetDoc.AddMember(rapidjson::StringRef(field_name.c_str()), sourceDoc, - targetDoc.GetAllocator()); + target_doc.AddMember(rapidjson::StringRef(field_name.c_str()), source_doc, + target_doc.GetAllocator()); rapidjson::StringBuffer sb; rapidjson::Writer w(sb); - targetDoc.Accept(w); + target_doc.Accept(w); target = sb.GetString(); } void Util::addArrayToJson(std::string& target, const std::vector& json_array, const std::string& field_name) { - std::string stringifiedJsonArray = "["; + std::string stringified_json_array = "["; if (json_array.size() > 0) { - stringifiedJsonArray += *(json_array[0]); + stringified_json_array += *(json_array[0]); for (auto it = json_array.begin() + 1; it != json_array.end(); it++) { - stringifiedJsonArray += ","; - stringifiedJsonArray += **it; + stringified_json_array += ","; + stringified_json_array += **it; } } - stringifiedJsonArray += "]"; + stringified_json_array += "]"; - mergeJsons(target, stringifiedJsonArray, field_name); + mergeJsons(target, stringified_json_array, field_name); } uint64_t Util::timeSinceEpochMicro() { From c5daf355a4538f3facbdd499b448b33168e9a8e7 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Wed, 12 Apr 2017 11:45:39 -0400 Subject: [PATCH 23/55] Make sure all variables use snake case (#430) --- .../common/tracing/zipkin/span_buffer_test.cc | 88 +++---- .../tracing/zipkin/span_context_test.cc | 222 +++++++++--------- .../tracing/zipkin/zipkin_core_types_test.cc | 6 +- 3 files changed, 159 insertions(+), 157 deletions(-) diff --git a/test/common/tracing/zipkin/span_buffer_test.cc b/test/common/tracing/zipkin/span_buffer_test.cc index 8a7c82b19ca43..27fbbd1b5d5db 100644 --- a/test/common/tracing/zipkin/span_buffer_test.cc +++ b/test/common/tracing/zipkin/span_buffer_test.cc @@ -17,14 +17,14 @@ TEST(ZipkinSpanBufferTest, defaultConstructorEndToEnd) { buffer.addSpan(std::move(span)); EXPECT_EQ(1ULL, buffer.pendingSpans()); - std::string expectedJsonArrayString = "" - "[{\"traceId\":\"0000000000000000\"," - "\"name\":\"\"," - "\"id\":\"0000000000000000\"," - "\"annotations\":[]," - "\"binaryAnnotations\":[]}" - "]"; - EXPECT_EQ(expectedJsonArrayString, buffer.toStringifiedJsonArray()); + std::string expected_json_array_string = "" + "[{\"traceId\":\"0000000000000000\"," + "\"name\":\"\"," + "\"id\":\"0000000000000000\"," + "\"annotations\":[]," + "\"binaryAnnotations\":[]}" + "]"; + EXPECT_EQ(expected_json_array_string, buffer.toStringifiedJsonArray()); buffer.flush(); EXPECT_EQ(0ULL, buffer.pendingSpans()); @@ -32,21 +32,21 @@ TEST(ZipkinSpanBufferTest, defaultConstructorEndToEnd) { buffer.addSpan(std::move(span)); buffer.addSpan(Span(span)); - expectedJsonArrayString = "" - "[" - "{\"traceId\":\"0000000000000000\"," - "\"name\":\"\"," - "\"id\":\"0000000000000000\"," - "\"annotations\":[]," - "\"binaryAnnotations\":[]}," - "{\"traceId\":\"0000000000000000\"," - "\"name\":\"\"," - "\"id\":\"0000000000000000\"," - "\"annotations\":[]," - "\"binaryAnnotations\":[]}" - "]"; + expected_json_array_string = "" + "[" + "{\"traceId\":\"0000000000000000\"," + "\"name\":\"\"," + "\"id\":\"0000000000000000\"," + "\"annotations\":[]," + "\"binaryAnnotations\":[]}," + "{\"traceId\":\"0000000000000000\"," + "\"name\":\"\"," + "\"id\":\"0000000000000000\"," + "\"annotations\":[]," + "\"binaryAnnotations\":[]}" + "]"; EXPECT_EQ(2ULL, buffer.pendingSpans()); - EXPECT_EQ(expectedJsonArrayString, buffer.toStringifiedJsonArray()); + EXPECT_EQ(expected_json_array_string, buffer.toStringifiedJsonArray()); buffer.flush(); EXPECT_EQ(0ULL, buffer.pendingSpans()); @@ -62,14 +62,14 @@ TEST(ZipkinSpanBufferTest, sizeConstructorEndtoEnd) { buffer.addSpan(std::move(span)); EXPECT_EQ(1ULL, buffer.pendingSpans()); - std::string expectedJsonArrayString = "" - "[{\"traceId\":\"0000000000000000\"," - "\"name\":\"\"," - "\"id\":\"0000000000000000\"," - "\"annotations\":[]," - "\"binaryAnnotations\":[]}" - "]"; - EXPECT_EQ(expectedJsonArrayString, buffer.toStringifiedJsonArray()); + std::string expected_json_array_string = "" + "[{\"traceId\":\"0000000000000000\"," + "\"name\":\"\"," + "\"id\":\"0000000000000000\"," + "\"annotations\":[]," + "\"binaryAnnotations\":[]}" + "]"; + EXPECT_EQ(expected_json_array_string, buffer.toStringifiedJsonArray()); buffer.flush(); EXPECT_EQ(0ULL, buffer.pendingSpans()); @@ -77,21 +77,21 @@ TEST(ZipkinSpanBufferTest, sizeConstructorEndtoEnd) { buffer.addSpan(std::move(span)); buffer.addSpan(Span(span)); - expectedJsonArrayString = "" - "[" - "{\"traceId\":\"0000000000000000\"," - "\"name\":\"\"," - "\"id\":\"0000000000000000\"," - "\"annotations\":[]," - "\"binaryAnnotations\":[]}," - "{\"traceId\":\"0000000000000000\"," - "\"name\":\"\"," - "\"id\":\"0000000000000000\"," - "\"annotations\":[]," - "\"binaryAnnotations\":[]}" - "]"; + expected_json_array_string = "" + "[" + "{\"traceId\":\"0000000000000000\"," + "\"name\":\"\"," + "\"id\":\"0000000000000000\"," + "\"annotations\":[]," + "\"binaryAnnotations\":[]}," + "{\"traceId\":\"0000000000000000\"," + "\"name\":\"\"," + "\"id\":\"0000000000000000\"," + "\"annotations\":[]," + "\"binaryAnnotations\":[]}" + "]"; EXPECT_EQ(2ULL, buffer.pendingSpans()); - EXPECT_EQ(expectedJsonArrayString, buffer.toStringifiedJsonArray()); + EXPECT_EQ(expected_json_array_string, buffer.toStringifiedJsonArray()); buffer.flush(); EXPECT_EQ(0ULL, buffer.pendingSpans()); diff --git a/test/common/tracing/zipkin/span_context_test.cc b/test/common/tracing/zipkin/span_context_test.cc index a63a5093a507b..4e087a0df7bdc 100644 --- a/test/common/tracing/zipkin/span_context_test.cc +++ b/test/common/tracing/zipkin/span_context_test.cc @@ -6,155 +6,157 @@ namespace Zipkin { TEST(ZipkinSpanContextTest, populateFromString) { - SpanContext spanContext; + SpanContext span_context; // Non-initialized span context - EXPECT_EQ(0ULL, spanContext.trace_id()); - EXPECT_EQ("0000000000000000", spanContext.traceIdAsHexString()); - EXPECT_EQ(0ULL, spanContext.id()); - EXPECT_EQ("0000000000000000", spanContext.idAsHexString()); - EXPECT_EQ(0ULL, spanContext.parent_id()); - EXPECT_EQ("0000000000000000", spanContext.parentIdAsHexString()); - EXPECT_FALSE(spanContext.isSetAnnotation().cr); - EXPECT_FALSE(spanContext.isSetAnnotation().cs); - EXPECT_FALSE(spanContext.isSetAnnotation().sr); - EXPECT_FALSE(spanContext.isSetAnnotation().ss); - EXPECT_EQ("0000000000000000;0000000000000000;0000000000000000", spanContext.serializeToString()); + EXPECT_EQ(0ULL, span_context.trace_id()); + EXPECT_EQ("0000000000000000", span_context.traceIdAsHexString()); + EXPECT_EQ(0ULL, span_context.id()); + EXPECT_EQ("0000000000000000", span_context.idAsHexString()); + EXPECT_EQ(0ULL, span_context.parent_id()); + EXPECT_EQ("0000000000000000", span_context.parentIdAsHexString()); + EXPECT_FALSE(span_context.isSetAnnotation().cr); + EXPECT_FALSE(span_context.isSetAnnotation().cs); + EXPECT_FALSE(span_context.isSetAnnotation().sr); + EXPECT_FALSE(span_context.isSetAnnotation().ss); + EXPECT_EQ("0000000000000000;0000000000000000;0000000000000000", span_context.serializeToString()); // Span context populated with trace id, id, parent id, and no annotations - spanContext.populateFromString("25c6f38dd0600e79;56707c7b3e1092af;c49193ea42335d1c"); - EXPECT_EQ(2722130815203937913ULL, spanContext.trace_id()); - EXPECT_EQ("25c6f38dd0600e79", spanContext.traceIdAsHexString()); - EXPECT_EQ(6228615153417491119ULL, spanContext.id()); - EXPECT_EQ("56707c7b3e1092af", spanContext.idAsHexString()); - EXPECT_EQ(14164264937399213340ULL, spanContext.parent_id()); - EXPECT_EQ("c49193ea42335d1c", spanContext.parentIdAsHexString()); - EXPECT_FALSE(spanContext.isSetAnnotation().cr); - EXPECT_FALSE(spanContext.isSetAnnotation().cs); - EXPECT_FALSE(spanContext.isSetAnnotation().sr); - EXPECT_FALSE(spanContext.isSetAnnotation().ss); - EXPECT_EQ("25c6f38dd0600e79;56707c7b3e1092af;c49193ea42335d1c", spanContext.serializeToString()); + span_context.populateFromString("25c6f38dd0600e79;56707c7b3e1092af;c49193ea42335d1c"); + EXPECT_EQ(2722130815203937913ULL, span_context.trace_id()); + EXPECT_EQ("25c6f38dd0600e79", span_context.traceIdAsHexString()); + EXPECT_EQ(6228615153417491119ULL, span_context.id()); + EXPECT_EQ("56707c7b3e1092af", span_context.idAsHexString()); + EXPECT_EQ(14164264937399213340ULL, span_context.parent_id()); + EXPECT_EQ("c49193ea42335d1c", span_context.parentIdAsHexString()); + EXPECT_FALSE(span_context.isSetAnnotation().cr); + EXPECT_FALSE(span_context.isSetAnnotation().cs); + EXPECT_FALSE(span_context.isSetAnnotation().sr); + EXPECT_FALSE(span_context.isSetAnnotation().ss); + EXPECT_EQ("25c6f38dd0600e79;56707c7b3e1092af;c49193ea42335d1c", span_context.serializeToString()); // Span context populated with trace id, id, parent id, and one annotation - spanContext.populateFromString("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cs"); - EXPECT_EQ(2722130815203937912ULL, spanContext.trace_id()); - EXPECT_EQ("25c6f38dd0600e78", spanContext.traceIdAsHexString()); - EXPECT_EQ(6228615153417491119ULL, spanContext.id()); - EXPECT_EQ("56707c7b3e1092af", spanContext.idAsHexString()); - EXPECT_EQ(14164264937399213340ULL, spanContext.parent_id()); - EXPECT_EQ("c49193ea42335d1c", spanContext.parentIdAsHexString()); - EXPECT_FALSE(spanContext.isSetAnnotation().cr); - EXPECT_TRUE(spanContext.isSetAnnotation().cs); - EXPECT_FALSE(spanContext.isSetAnnotation().sr); - EXPECT_FALSE(spanContext.isSetAnnotation().ss); + span_context.populateFromString("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cs"); + EXPECT_EQ(2722130815203937912ULL, span_context.trace_id()); + EXPECT_EQ("25c6f38dd0600e78", span_context.traceIdAsHexString()); + EXPECT_EQ(6228615153417491119ULL, span_context.id()); + EXPECT_EQ("56707c7b3e1092af", span_context.idAsHexString()); + EXPECT_EQ(14164264937399213340ULL, span_context.parent_id()); + EXPECT_EQ("c49193ea42335d1c", span_context.parentIdAsHexString()); + EXPECT_FALSE(span_context.isSetAnnotation().cr); + EXPECT_TRUE(span_context.isSetAnnotation().cs); + EXPECT_FALSE(span_context.isSetAnnotation().sr); + EXPECT_FALSE(span_context.isSetAnnotation().ss); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cs", - spanContext.serializeToString()); + span_context.serializeToString()); // Span context populated with trace id, id, parent id, and multiple annotations - spanContext.populateFromString("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cs;cr"); - EXPECT_EQ(2722130815203937912ULL, spanContext.trace_id()); - EXPECT_EQ("25c6f38dd0600e78", spanContext.traceIdAsHexString()); - EXPECT_EQ(6228615153417491119ULL, spanContext.id()); - EXPECT_EQ("56707c7b3e1092af", spanContext.idAsHexString()); - EXPECT_EQ(14164264937399213340ULL, spanContext.parent_id()); - EXPECT_EQ("c49193ea42335d1c", spanContext.parentIdAsHexString()); - EXPECT_TRUE(spanContext.isSetAnnotation().cr); - EXPECT_TRUE(spanContext.isSetAnnotation().cs); - EXPECT_FALSE(spanContext.isSetAnnotation().sr); - EXPECT_FALSE(spanContext.isSetAnnotation().ss); + span_context.populateFromString("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cs;cr"); + EXPECT_EQ(2722130815203937912ULL, span_context.trace_id()); + EXPECT_EQ("25c6f38dd0600e78", span_context.traceIdAsHexString()); + EXPECT_EQ(6228615153417491119ULL, span_context.id()); + EXPECT_EQ("56707c7b3e1092af", span_context.idAsHexString()); + EXPECT_EQ(14164264937399213340ULL, span_context.parent_id()); + EXPECT_EQ("c49193ea42335d1c", span_context.parentIdAsHexString()); + EXPECT_TRUE(span_context.isSetAnnotation().cr); + EXPECT_TRUE(span_context.isSetAnnotation().cs); + EXPECT_FALSE(span_context.isSetAnnotation().sr); + EXPECT_FALSE(span_context.isSetAnnotation().ss); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cr;cs", - spanContext.serializeToString()); + span_context.serializeToString()); // Span context populated with invalid string: it gets reset to its non-initialized state - spanContext.populateFromString("invalid string"); - EXPECT_EQ(0ULL, spanContext.trace_id()); - EXPECT_EQ("0000000000000000", spanContext.traceIdAsHexString()); - EXPECT_EQ(0ULL, spanContext.id()); - EXPECT_EQ("0000000000000000", spanContext.idAsHexString()); - EXPECT_EQ(0ULL, spanContext.parent_id()); - EXPECT_EQ("0000000000000000", spanContext.parentIdAsHexString()); - EXPECT_FALSE(spanContext.isSetAnnotation().cr); - EXPECT_FALSE(spanContext.isSetAnnotation().cs); - EXPECT_FALSE(spanContext.isSetAnnotation().sr); - EXPECT_FALSE(spanContext.isSetAnnotation().ss); - EXPECT_EQ("0000000000000000;0000000000000000;0000000000000000", spanContext.serializeToString()); + span_context.populateFromString("invalid string"); + EXPECT_EQ(0ULL, span_context.trace_id()); + EXPECT_EQ("0000000000000000", span_context.traceIdAsHexString()); + EXPECT_EQ(0ULL, span_context.id()); + EXPECT_EQ("0000000000000000", span_context.idAsHexString()); + EXPECT_EQ(0ULL, span_context.parent_id()); + EXPECT_EQ("0000000000000000", span_context.parentIdAsHexString()); + EXPECT_FALSE(span_context.isSetAnnotation().cr); + EXPECT_FALSE(span_context.isSetAnnotation().cs); + EXPECT_FALSE(span_context.isSetAnnotation().sr); + EXPECT_FALSE(span_context.isSetAnnotation().ss); + EXPECT_EQ("0000000000000000;0000000000000000;0000000000000000", span_context.serializeToString()); } TEST(ZipkinSpanContextTest, populateFromSpan) { Span span; - SpanContext spanContext(span); + SpanContext span_context(span); // Non-initialized span context - EXPECT_EQ(0ULL, spanContext.trace_id()); - EXPECT_EQ("0000000000000000", spanContext.traceIdAsHexString()); - EXPECT_EQ(0ULL, spanContext.id()); - EXPECT_EQ("0000000000000000", spanContext.idAsHexString()); - EXPECT_EQ(0ULL, spanContext.parent_id()); - EXPECT_EQ("0000000000000000", spanContext.parentIdAsHexString()); - EXPECT_FALSE(spanContext.isSetAnnotation().cr); - EXPECT_FALSE(spanContext.isSetAnnotation().cs); - EXPECT_FALSE(spanContext.isSetAnnotation().sr); - EXPECT_FALSE(spanContext.isSetAnnotation().ss); - EXPECT_EQ("0000000000000000;0000000000000000;0000000000000000", spanContext.serializeToString()); + EXPECT_EQ(0ULL, span_context.trace_id()); + EXPECT_EQ("0000000000000000", span_context.traceIdAsHexString()); + EXPECT_EQ(0ULL, span_context.id()); + EXPECT_EQ("0000000000000000", span_context.idAsHexString()); + EXPECT_EQ(0ULL, span_context.parent_id()); + EXPECT_EQ("0000000000000000", span_context.parentIdAsHexString()); + EXPECT_FALSE(span_context.isSetAnnotation().cr); + EXPECT_FALSE(span_context.isSetAnnotation().cs); + EXPECT_FALSE(span_context.isSetAnnotation().sr); + EXPECT_FALSE(span_context.isSetAnnotation().ss); + EXPECT_EQ("0000000000000000;0000000000000000;0000000000000000", span_context.serializeToString()); // Span context populated with trace id, id, parent id, and no annotations span.setTraceId(2722130815203937912ULL); span.setId(6228615153417491119ULL); span.setParentId(14164264937399213340ULL); - SpanContext spanContext2(span); - EXPECT_EQ(2722130815203937912ULL, spanContext2.trace_id()); - EXPECT_EQ("25c6f38dd0600e78", spanContext2.traceIdAsHexString()); - EXPECT_EQ(6228615153417491119ULL, spanContext2.id()); - EXPECT_EQ("56707c7b3e1092af", spanContext2.idAsHexString()); - EXPECT_EQ(14164264937399213340ULL, spanContext2.parent_id()); - EXPECT_EQ("c49193ea42335d1c", spanContext2.parentIdAsHexString()); - EXPECT_FALSE(spanContext2.isSetAnnotation().cr); - EXPECT_FALSE(spanContext2.isSetAnnotation().cs); - EXPECT_FALSE(spanContext2.isSetAnnotation().sr); - EXPECT_FALSE(spanContext2.isSetAnnotation().ss); - EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c", spanContext2.serializeToString()); + SpanContext span_context_2(span); + EXPECT_EQ(2722130815203937912ULL, span_context_2.trace_id()); + EXPECT_EQ("25c6f38dd0600e78", span_context_2.traceIdAsHexString()); + EXPECT_EQ(6228615153417491119ULL, span_context_2.id()); + EXPECT_EQ("56707c7b3e1092af", span_context_2.idAsHexString()); + EXPECT_EQ(14164264937399213340ULL, span_context_2.parent_id()); + EXPECT_EQ("c49193ea42335d1c", span_context_2.parentIdAsHexString()); + EXPECT_FALSE(span_context_2.isSetAnnotation().cr); + EXPECT_FALSE(span_context_2.isSetAnnotation().cs); + EXPECT_FALSE(span_context_2.isSetAnnotation().sr); + EXPECT_FALSE(span_context_2.isSetAnnotation().ss); + EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c", + span_context_2.serializeToString()); // Test if we can handle 128-bit trace ids EXPECT_FALSE(span.isSet().trace_id_high); span.setTraceIdHigh(9922130815203937912ULL); EXPECT_TRUE(span.isSet().trace_id_high); - SpanContext spanContext5(span); + SpanContext span_context_5(span); // We currently drop the high bits. So, we expect the same context as above - EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c", spanContext5.serializeToString()); + EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c", + span_context_5.serializeToString()); // Span context populated with trace id, id, parent id, and one annotation Annotation ann; ann.setValue(ZipkinCoreConstants::SERVER_RECV); span.addAnnotation(ann); - SpanContext spanContext3(span); - EXPECT_EQ(2722130815203937912ULL, spanContext3.trace_id()); - EXPECT_EQ("25c6f38dd0600e78", spanContext3.traceIdAsHexString()); - EXPECT_EQ(6228615153417491119ULL, spanContext3.id()); - EXPECT_EQ("56707c7b3e1092af", spanContext3.idAsHexString()); - EXPECT_EQ(14164264937399213340ULL, spanContext3.parent_id()); - EXPECT_EQ("c49193ea42335d1c", spanContext3.parentIdAsHexString()); - EXPECT_FALSE(spanContext3.isSetAnnotation().cr); - EXPECT_FALSE(spanContext3.isSetAnnotation().cs); - EXPECT_TRUE(spanContext3.isSetAnnotation().sr); - EXPECT_FALSE(spanContext3.isSetAnnotation().ss); + SpanContext span_context_3(span); + EXPECT_EQ(2722130815203937912ULL, span_context_3.trace_id()); + EXPECT_EQ("25c6f38dd0600e78", span_context_3.traceIdAsHexString()); + EXPECT_EQ(6228615153417491119ULL, span_context_3.id()); + EXPECT_EQ("56707c7b3e1092af", span_context_3.idAsHexString()); + EXPECT_EQ(14164264937399213340ULL, span_context_3.parent_id()); + EXPECT_EQ("c49193ea42335d1c", span_context_3.parentIdAsHexString()); + EXPECT_FALSE(span_context_3.isSetAnnotation().cr); + EXPECT_FALSE(span_context_3.isSetAnnotation().cs); + EXPECT_TRUE(span_context_3.isSetAnnotation().sr); + EXPECT_FALSE(span_context_3.isSetAnnotation().ss); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;sr", - spanContext3.serializeToString()); + span_context_3.serializeToString()); // Span context populated with trace id, id, parent id, and multiple annotations ann.setValue(ZipkinCoreConstants::SERVER_SEND); span.addAnnotation(ann); - SpanContext spanContext4(span); - EXPECT_EQ(2722130815203937912ULL, spanContext4.trace_id()); - EXPECT_EQ("25c6f38dd0600e78", spanContext4.traceIdAsHexString()); - EXPECT_EQ(6228615153417491119ULL, spanContext4.id()); - EXPECT_EQ("56707c7b3e1092af", spanContext4.idAsHexString()); - EXPECT_EQ(14164264937399213340ULL, spanContext4.parent_id()); - EXPECT_EQ("c49193ea42335d1c", spanContext4.parentIdAsHexString()); - EXPECT_FALSE(spanContext4.isSetAnnotation().cr); - EXPECT_FALSE(spanContext4.isSetAnnotation().cs); - EXPECT_TRUE(spanContext4.isSetAnnotation().sr); - EXPECT_TRUE(spanContext4.isSetAnnotation().ss); + SpanContext span_context_4(span); + EXPECT_EQ(2722130815203937912ULL, span_context_4.trace_id()); + EXPECT_EQ("25c6f38dd0600e78", span_context_4.traceIdAsHexString()); + EXPECT_EQ(6228615153417491119ULL, span_context_4.id()); + EXPECT_EQ("56707c7b3e1092af", span_context_4.idAsHexString()); + EXPECT_EQ(14164264937399213340ULL, span_context_4.parent_id()); + EXPECT_EQ("c49193ea42335d1c", span_context_4.parentIdAsHexString()); + EXPECT_FALSE(span_context_4.isSetAnnotation().cr); + EXPECT_FALSE(span_context_4.isSetAnnotation().cs); + EXPECT_TRUE(span_context_4.isSetAnnotation().sr); + EXPECT_TRUE(span_context_4.isSetAnnotation().ss); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;sr;ss", - spanContext4.serializeToString()); + span_context_4.serializeToString()); } } // Zipkin diff --git a/test/common/tracing/zipkin/zipkin_core_types_test.cc b/test/common/tracing/zipkin/zipkin_core_types_test.cc index cb3ad365387a2..c908c263c0cce 100644 --- a/test/common/tracing/zipkin/zipkin_core_types_test.cc +++ b/test/common/tracing/zipkin/zipkin_core_types_test.cc @@ -349,7 +349,7 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { Annotation ann; BinaryAnnotation bann; std::vector annotations; - std::vector bAnnotations; + std::vector binary_annotations; endpoint.setServiceName("my_service_name"); std::string ip = "192.168.1.2"; @@ -368,8 +368,8 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { bann.setValue("my_component_name"); bann.setEndpoint(endpoint); - bAnnotations.push_back(bann); - span.setBinaryAnnotations(bAnnotations); + binary_annotations.push_back(bann); + span.setBinaryAnnotations(binary_annotations); EXPECT_EQ(1ULL, span.binaryAnnotations().size()); EXPECT_EQ( From 2874f8837fb641db53694c39ab7f2b5b62052fcb Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Wed, 12 Apr 2017 11:57:06 -0400 Subject: [PATCH 24/55] Make pointer definitions more explicit (#430) --- source/common/tracing/zipkin/span_buffer.h | 4 ++-- source/common/tracing/zipkin/tracer.cc | 4 ++-- source/common/tracing/zipkin/tracer.h | 10 +++++++--- source/common/tracing/zipkin/tracer_interface.h | 2 +- source/common/tracing/zipkin/zipkin_core_types.h | 6 +++--- test/common/tracing/zipkin/tracer_test.cc | 6 +++--- 6 files changed, 18 insertions(+), 14 deletions(-) diff --git a/source/common/tracing/zipkin/span_buffer.h b/source/common/tracing/zipkin/span_buffer.h index 89c236e47d7c5..03e6cd8e0396a 100644 --- a/source/common/tracing/zipkin/span_buffer.h +++ b/source/common/tracing/zipkin/span_buffer.h @@ -4,7 +4,7 @@ namespace Zipkin { -typedef std::shared_ptr SpanPtr; +typedef std::shared_ptr SpanSharedPtr; class SpanBuffer { public: @@ -26,7 +26,7 @@ class SpanBuffer { private: // We use a pre-allocated vector to improve performance - std::vector span_buffer_; + std::vector span_buffer_; uint64_t size_; uint64_t next_position_; }; diff --git a/source/common/tracing/zipkin/tracer.cc b/source/common/tracing/zipkin/tracer.cc index 9da23109c4e05..ef62a001af59c 100644 --- a/source/common/tracing/zipkin/tracer.cc +++ b/source/common/tracing/zipkin/tracer.cc @@ -118,8 +118,8 @@ void Tracer::reportSpan(Span&& span) { } } -void Tracer::setReporter(std::unique_ptr reporter) { - reporter_ = std::shared_ptr(std::move(reporter)); +void Tracer::setReporter(ReporterUniquePtr reporter) { + reporter_ = ReporterSharedPtr(std::move(reporter)); } void Tracer::getIPAndPort(const std::string& address, std::string& ip, uint16_t& port) { diff --git a/source/common/tracing/zipkin/tracer.h b/source/common/tracing/zipkin/tracer.h index 39346f9db4dab..a1742756ec9b3 100644 --- a/source/common/tracing/zipkin/tracer.h +++ b/source/common/tracing/zipkin/tracer.h @@ -19,6 +19,10 @@ class Reporter { virtual void reportSpan(Span&& span) = 0; }; +typedef std::shared_ptr ReporterSharedPtr; + +typedef std::unique_ptr ReporterUniquePtr; + class Tracer : public TracerInterface { public: Tracer(const std::string& service_name, const std::string& address) @@ -41,9 +45,9 @@ class Tracer : public TracerInterface { */ void reportSpan(Span&& span) override; - std::shared_ptr reporter() { return reporter_; } + ReporterSharedPtr reporter() { return reporter_; } - void setReporter(std::unique_ptr reporter); + void setReporter(ReporterUniquePtr reporter); private: void getIPAndPort(const std::string& address, std::string& ip, uint16_t& port); @@ -51,6 +55,6 @@ class Tracer : public TracerInterface { std::string service_name_; std::string address_; - std::shared_ptr reporter_; + ReporterSharedPtr reporter_; }; } // Zipkin diff --git a/source/common/tracing/zipkin/tracer_interface.h b/source/common/tracing/zipkin/tracer_interface.h index afb344125013b..e812d7b852fd6 100644 --- a/source/common/tracing/zipkin/tracer_interface.h +++ b/source/common/tracing/zipkin/tracer_interface.h @@ -11,6 +11,6 @@ class TracerInterface { virtual void reportSpan(Span&& span) = 0; }; -typedef TracerInterface* TracerPtr; +typedef TracerInterface* TracerRawPtr; } // Zipkin diff --git a/source/common/tracing/zipkin/zipkin_core_types.h b/source/common/tracing/zipkin/zipkin_core_types.h index 9e766079e22f7..69a9df69bf8fa 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.h +++ b/source/common/tracing/zipkin/zipkin_core_types.h @@ -246,9 +246,9 @@ class Span : public ZipkinBase { const std::string& toJson() override; - void setTracer(TracerPtr tracer) { tracer_ = tracer; } + void setTracer(TracerRawPtr tracer) { tracer_ = tracer; } - TracerPtr tracer() const { return tracer_; } + TracerRawPtr tracer() const { return tracer_; } void finish(); @@ -267,7 +267,7 @@ class Span : public ZipkinBase { int64_t start_time_; - TracerPtr tracer_; + TracerRawPtr tracer_; _Span__isset isset_; }; diff --git a/test/common/tracing/zipkin/tracer_test.cc b/test/common/tracing/zipkin/tracer_test.cc index 873901e33c734..9ef0610d8c7ed 100644 --- a/test/common/tracing/zipkin/tracer_test.cc +++ b/test/common/tracing/zipkin/tracer_test.cc @@ -37,7 +37,7 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_FALSE(endpoint.isSetIpv6()); // The tracer must have been properly set - EXPECT_EQ(dynamic_cast(&tracer), root_span.tracer()); + EXPECT_EQ(dynamic_cast(&tracer), root_span.tracer()); // Duration is not set at span-creation time EXPECT_FALSE(root_span.isSet().duration); @@ -80,7 +80,7 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_FALSE(endpoint.isSetIpv6()); // The tracer must have been properly set - EXPECT_EQ(dynamic_cast(&tracer), server_side_shared_context_span.tracer()); + EXPECT_EQ(dynamic_cast(&tracer), server_side_shared_context_span.tracer()); // Duration is not set at span-creation time EXPECT_FALSE(server_side_shared_context_span.isSet().duration); @@ -122,7 +122,7 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_FALSE(endpoint.isSetIpv6()); // The tracer must have been properly set - EXPECT_EQ(dynamic_cast(&tracer), child_span.tracer()); + EXPECT_EQ(dynamic_cast(&tracer), child_span.tracer()); // Duration is not set at span-creation time EXPECT_FALSE(child_span.isSet().duration); From 2c8884a2de46ae66adcaa5f2484f8ac02600af26 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Wed, 12 Apr 2017 12:04:35 -0400 Subject: [PATCH 25/55] Use file-level namespace for constant definitions (#430) --- .../tracing/zipkin/zipkin_core_constants.cc | 55 ++++++++++--------- .../tracing/zipkin/zipkin_json_field_names.cc | 39 +++++++------ 2 files changed, 50 insertions(+), 44 deletions(-) diff --git a/source/common/tracing/zipkin/zipkin_core_constants.cc b/source/common/tracing/zipkin/zipkin_core_constants.cc index 0c83c2941e6db..2ea265b66a5d2 100644 --- a/source/common/tracing/zipkin/zipkin_core_constants.cc +++ b/source/common/tracing/zipkin/zipkin_core_constants.cc @@ -1,32 +1,35 @@ #include "common/tracing/zipkin/zipkin_core_constants.h" -const std::string Zipkin::ZipkinCoreConstants::CLIENT_SEND = "cs"; -const std::string Zipkin::ZipkinCoreConstants::CLIENT_RECV = "cr"; -const std::string Zipkin::ZipkinCoreConstants::SERVER_SEND = "ss"; -const std::string Zipkin::ZipkinCoreConstants::SERVER_RECV = "sr"; -const std::string Zipkin::ZipkinCoreConstants::WIRE_SEND = "ws"; -const std::string Zipkin::ZipkinCoreConstants::WIRE_RECV = "wr"; -const std::string Zipkin::ZipkinCoreConstants::CLIENT_SEND_FRAGMENT = "csf"; -const std::string Zipkin::ZipkinCoreConstants::CLIENT_RECV_FRAGMENT = "crf"; -const std::string Zipkin::ZipkinCoreConstants::SERVER_SEND_FRAGMENT = "ssf"; -const std::string Zipkin::ZipkinCoreConstants::SERVER_RECV_FRAGMENT = "srf"; +namespace Zipkin { -const std::string Zipkin::ZipkinCoreConstants::HTTP_HOST = "http.host"; -const std::string Zipkin::ZipkinCoreConstants::HTTP_METHOD = "http.method"; -const std::string Zipkin::ZipkinCoreConstants::HTTP_PATH = "http.path"; -const std::string Zipkin::ZipkinCoreConstants::HTTP_URL = "http.url"; -const std::string Zipkin::ZipkinCoreConstants::HTTP_STATUS_CODE = "http.status_code"; -const std::string Zipkin::ZipkinCoreConstants::HTTP_REQUEST_SIZE = "http.request.size"; -const std::string Zipkin::ZipkinCoreConstants::HTTP_RESPONSE_SIZE = "http.response.size"; +const std::string ZipkinCoreConstants::CLIENT_SEND = "cs"; +const std::string ZipkinCoreConstants::CLIENT_RECV = "cr"; +const std::string ZipkinCoreConstants::SERVER_SEND = "ss"; +const std::string ZipkinCoreConstants::SERVER_RECV = "sr"; +const std::string ZipkinCoreConstants::WIRE_SEND = "ws"; +const std::string ZipkinCoreConstants::WIRE_RECV = "wr"; +const std::string ZipkinCoreConstants::CLIENT_SEND_FRAGMENT = "csf"; +const std::string ZipkinCoreConstants::CLIENT_RECV_FRAGMENT = "crf"; +const std::string ZipkinCoreConstants::SERVER_SEND_FRAGMENT = "ssf"; +const std::string ZipkinCoreConstants::SERVER_RECV_FRAGMENT = "srf"; -const std::string Zipkin::ZipkinCoreConstants::LOCAL_COMPONENT = "lc"; -const std::string Zipkin::ZipkinCoreConstants::ERROR = "error"; -const std::string Zipkin::ZipkinCoreConstants::CLIENT_ADDR = "ca"; -const std::string Zipkin::ZipkinCoreConstants::SERVER_ADDR = "sa"; +const std::string ZipkinCoreConstants::HTTP_HOST = "http.host"; +const std::string ZipkinCoreConstants::HTTP_METHOD = "http.method"; +const std::string ZipkinCoreConstants::HTTP_PATH = "http.path"; +const std::string ZipkinCoreConstants::HTTP_URL = "http.url"; +const std::string ZipkinCoreConstants::HTTP_STATUS_CODE = "http.status_code"; +const std::string ZipkinCoreConstants::HTTP_REQUEST_SIZE = "http.request.size"; +const std::string ZipkinCoreConstants::HTTP_RESPONSE_SIZE = "http.response.size"; + +const std::string ZipkinCoreConstants::LOCAL_COMPONENT = "lc"; +const std::string ZipkinCoreConstants::ERROR = "error"; +const std::string ZipkinCoreConstants::CLIENT_ADDR = "ca"; +const std::string ZipkinCoreConstants::SERVER_ADDR = "sa"; // Zipkin B3 headers -const std::string Zipkin::ZipkinCoreConstants::X_B3_TRACE_ID = "X-B3-TraceId"; -const std::string Zipkin::ZipkinCoreConstants::X_B3_SPAN_ID = "X-B3-SpanId"; -const std::string Zipkin::ZipkinCoreConstants::X_B3_PARENT_SPAN_ID = "X-B3-ParentSpanId"; -const std::string Zipkin::ZipkinCoreConstants::X_B3_SAMPLED = "X-B3-Sampled"; -const std::string Zipkin::ZipkinCoreConstants::X_B3_FLAGS = "X-B3-Flags"; +const std::string ZipkinCoreConstants::X_B3_TRACE_ID = "X-B3-TraceId"; +const std::string ZipkinCoreConstants::X_B3_SPAN_ID = "X-B3-SpanId"; +const std::string ZipkinCoreConstants::X_B3_PARENT_SPAN_ID = "X-B3-ParentSpanId"; +const std::string ZipkinCoreConstants::X_B3_SAMPLED = "X-B3-Sampled"; +const std::string ZipkinCoreConstants::X_B3_FLAGS = "X-B3-Flags"; +} // Zipkin diff --git a/source/common/tracing/zipkin/zipkin_json_field_names.cc b/source/common/tracing/zipkin/zipkin_json_field_names.cc index 1e7af66c86a81..ad8a852a3a293 100644 --- a/source/common/tracing/zipkin/zipkin_json_field_names.cc +++ b/source/common/tracing/zipkin/zipkin_json_field_names.cc @@ -1,23 +1,26 @@ #include "common/tracing/zipkin/zipkin_json_field_names.h" -const std::string Zipkin::ZipkinJsonFieldNames::SPAN_TRACE_ID = "traceId"; -const std::string Zipkin::ZipkinJsonFieldNames::SPAN_PARENT_ID = "parentId"; -const std::string Zipkin::ZipkinJsonFieldNames::SPAN_NAME = "name"; -const std::string Zipkin::ZipkinJsonFieldNames::SPAN_ID = "id"; -const std::string Zipkin::ZipkinJsonFieldNames::SPAN_TIMESTAMP = "timestamp"; -const std::string Zipkin::ZipkinJsonFieldNames::SPAN_DURATION = "duration"; -const std::string Zipkin::ZipkinJsonFieldNames::SPAN_ANNOTATIONS = "annotations"; -const std::string Zipkin::ZipkinJsonFieldNames::SPAN_BINARY_ANNOTATIONS = "binaryAnnotations"; +namespace Zipkin { -const std::string Zipkin::ZipkinJsonFieldNames::ANNOTATION_ENDPOINT = "endpoint"; -const std::string Zipkin::ZipkinJsonFieldNames::ANNOTATION_TIMESTAMP = "timestamp"; -const std::string Zipkin::ZipkinJsonFieldNames::ANNOTATION_VALUE = "value"; +const std::string ZipkinJsonFieldNames::SPAN_TRACE_ID = "traceId"; +const std::string ZipkinJsonFieldNames::SPAN_PARENT_ID = "parentId"; +const std::string ZipkinJsonFieldNames::SPAN_NAME = "name"; +const std::string ZipkinJsonFieldNames::SPAN_ID = "id"; +const std::string ZipkinJsonFieldNames::SPAN_TIMESTAMP = "timestamp"; +const std::string ZipkinJsonFieldNames::SPAN_DURATION = "duration"; +const std::string ZipkinJsonFieldNames::SPAN_ANNOTATIONS = "annotations"; +const std::string ZipkinJsonFieldNames::SPAN_BINARY_ANNOTATIONS = "binaryAnnotations"; -const std::string Zipkin::ZipkinJsonFieldNames::BINARY_ANNOTATION_ENDPOINT = "endpoint"; -const std::string Zipkin::ZipkinJsonFieldNames::BINARY_ANNOTATION_KEY = "key"; -const std::string Zipkin::ZipkinJsonFieldNames::BINARY_ANNOTATION_VALUE = "value"; +const std::string ZipkinJsonFieldNames::ANNOTATION_ENDPOINT = "endpoint"; +const std::string ZipkinJsonFieldNames::ANNOTATION_TIMESTAMP = "timestamp"; +const std::string ZipkinJsonFieldNames::ANNOTATION_VALUE = "value"; -const std::string Zipkin::ZipkinJsonFieldNames::ENDPOINT_SERVICE_NAME = "serviceName"; -const std::string Zipkin::ZipkinJsonFieldNames::ENDPOINT_PORT = "port"; -const std::string Zipkin::ZipkinJsonFieldNames::ENDPOINT_IPV4 = "ipv4"; -const std::string Zipkin::ZipkinJsonFieldNames::ENDPOINT_IPV6 = "ipv6"; +const std::string ZipkinJsonFieldNames::BINARY_ANNOTATION_ENDPOINT = "endpoint"; +const std::string ZipkinJsonFieldNames::BINARY_ANNOTATION_KEY = "key"; +const std::string ZipkinJsonFieldNames::BINARY_ANNOTATION_VALUE = "value"; + +const std::string ZipkinJsonFieldNames::ENDPOINT_SERVICE_NAME = "serviceName"; +const std::string ZipkinJsonFieldNames::ENDPOINT_PORT = "port"; +const std::string ZipkinJsonFieldNames::ENDPOINT_IPV4 = "ipv4"; +const std::string ZipkinJsonFieldNames::ENDPOINT_IPV6 = "ipv6"; +} // Zipkin From 7bb9fe28895a36b8e3c2586eed18027104e23c53 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Wed, 12 Apr 2017 18:44:20 -0400 Subject: [PATCH 26/55] Added comments to all classes, structs, enums, and methods. Made several code-style changes. (#430) --- source/common/tracing/zipkin/span_buffer.h | 39 ++ source/common/tracing/zipkin/span_context.cc | 28 +- source/common/tracing/zipkin/span_context.h | 83 ++++- source/common/tracing/zipkin/tracer.cc | 24 +- source/common/tracing/zipkin/tracer.h | 53 ++- .../common/tracing/zipkin/tracer_interface.h | 19 +- source/common/tracing/zipkin/util.cc | 11 + source/common/tracing/zipkin/util.h | 54 ++- .../tracing/zipkin/zipkin_core_types.cc | 10 +- .../common/tracing/zipkin/zipkin_core_types.h | 348 +++++++++++++++++- .../tracing/zipkin/span_context_test.cc | 76 ++-- test/common/tracing/zipkin/tracer_test.cc | 14 +- .../tracing/zipkin/zipkin_core_types_test.cc | 40 +- 13 files changed, 664 insertions(+), 135 deletions(-) diff --git a/source/common/tracing/zipkin/span_buffer.h b/source/common/tracing/zipkin/span_buffer.h index 03e6cd8e0396a..de0a93952064d 100644 --- a/source/common/tracing/zipkin/span_buffer.h +++ b/source/common/tracing/zipkin/span_buffer.h @@ -6,22 +6,61 @@ namespace Zipkin { typedef std::shared_ptr SpanSharedPtr; +/** + * This class implements a simple buffer to store Zipkin tracing spans + * prior to flushing them. + */ class SpanBuffer { public: + /** + * Constructor that creates an empty buffer. Space needs to be allocated by invoking + * the method allocateBuffer(size). + */ SpanBuffer() : size_(0), next_position_(0) {} + /** + * Constructor that initializes a buffer with the given size + * + * @param size The desired buffer size + */ SpanBuffer(uint64_t size) : span_buffer_(size, nullptr), size_(size), next_position_(0) {} + /** + * Destructor + */ virtual ~SpanBuffer() {} + /** + * Allocates space for an empty buffer or resizes a previously-allocated one. + * + * @param size The desired buffer size + */ void allocateBuffer(uint64_t size); + /** + * Adds the given Zipkin span to the buffer. + * + * @param span The span to be added to the buffer + * + * @return true if the span was successfully added, or false if the buffer was full + */ bool addSpan(Span&& span); + /** + * Empties the buffer. This method is supposed to be called when all buffered spans + * have been sent to to the Zipkin service. + */ void flush(); + /** + * @return the number of spans currently buffered + */ uint64_t pendingSpans() { return next_position_; } + /** + * @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(); private: diff --git a/source/common/tracing/zipkin/span_context.cc b/source/common/tracing/zipkin/span_context.cc index 6c621228e896e..bd0e2b44a31df 100644 --- a/source/common/tracing/zipkin/span_context.cc +++ b/source/common/tracing/zipkin/span_context.cc @@ -12,16 +12,16 @@ SpanContext::SpanContext(const Span& span) { for (Annotation ann : span.annotations()) { if (ann.value() == ZipkinCoreConstants::CLIENT_RECV) { - annotation_values_.cr = true; + annotation_values_.cr_ = true; } if (ann.value() == ZipkinCoreConstants::CLIENT_SEND) { - annotation_values_.cs = true; + annotation_values_.cs_ = true; } if (ann.value() == ZipkinCoreConstants::SERVER_RECV) { - annotation_values_.sr = true; + annotation_values_.sr_ = true; } if (ann.value() == ZipkinCoreConstants::SERVER_SEND) { - annotation_values_.ss = true; + annotation_values_.ss_ = true; } } @@ -37,16 +37,16 @@ const std::string SpanContext::serializeToString() { s = traceIdAsHexString() + FIELD_SEPARATOR_ + idAsHexString() + FIELD_SEPARATOR_ + parentIdAsHexString(); - if (annotation_values_.cr) { + if (annotation_values_.cr_) { s += FIELD_SEPARATOR_ + ZipkinCoreConstants::CLIENT_RECV; } - if (annotation_values_.cs) { + if (annotation_values_.cs_) { s += FIELD_SEPARATOR_ + ZipkinCoreConstants::CLIENT_SEND; } - if (annotation_values_.sr) { + if (annotation_values_.sr_) { s += FIELD_SEPARATOR_ + ZipkinCoreConstants::SERVER_RECV; } - if (annotation_values_.ss) { + if (annotation_values_.ss_) { s += FIELD_SEPARATOR_ + ZipkinCoreConstants::SERVER_SEND; } @@ -66,8 +66,8 @@ void SpanContext::populateFromString(std::string s) { std::smatch match; trace_id_ = parent_id_ = id_ = 0; - annotation_values_.cs = annotation_values_.cr = annotation_values_.ss = annotation_values_.sr = - false; + annotation_values_.cs_ = annotation_values_.cr_ = annotation_values_.ss_ = + annotation_values_.sr_ = false; if (std::regex_search(s, match, re)) { // This is a valid string encoding of the context @@ -82,13 +82,13 @@ void SpanContext::populateFromString(std::string s) { while (annotation_value) { if (!std::strcmp(annotation_value, ZipkinCoreConstants::CLIENT_RECV.c_str())) { - annotation_values_.cr = true; + annotation_values_.cr_ = true; } else if (!std::strcmp(annotation_value, ZipkinCoreConstants::CLIENT_SEND.c_str())) { - annotation_values_.cs = true; + annotation_values_.cs_ = true; } else if (!std::strcmp(annotation_value, ZipkinCoreConstants::SERVER_RECV.c_str())) { - annotation_values_.sr = true; + annotation_values_.sr_ = true; } else if (!std::strcmp(annotation_value, ZipkinCoreConstants::SERVER_SEND.c_str())) { - annotation_values_.ss = true; + annotation_values_.ss_ = true; } annotation_value = std::strtok(NULL, FIELD_SEPARATOR_.c_str()); diff --git a/source/common/tracing/zipkin/span_context.h b/source/common/tracing/zipkin/span_context.h index e0a70b945dc25..998028752b89a 100644 --- a/source/common/tracing/zipkin/span_context.h +++ b/source/common/tracing/zipkin/span_context.h @@ -5,38 +5,111 @@ namespace Zipkin { +/** + * This struct identifies which Zipkin annotations are present in the + * span context (see SpanContext) + * Each member is a one-bit boolean indicating whether or not the + * corresponding annotation is present. + * + * In particular, the following annotations are tracked by this struct: + * CS: "Client Send" + * CR: "Client Receive" + * SS: "Server Send" + * SR: "Server Receive" + */ typedef struct Annotation_isset_ { - Annotation_isset_() : cs(false), cr(false), ss(false), sr(false) {} - bool cs : 1; - bool cr : 1; - bool ss : 1; - bool sr : 1; + Annotation_isset_() : cs_(false), cr_(false), ss_(false), sr_(false) {} + bool cs_ : 1; + bool cr_ : 1; + bool ss_ : 1; + bool sr_ : 1; } Annotation_isset_; +/** + * This class represents the context of a Zipkin span. It embodies the following + * span characteristics: trace id, span id, parent id, and basic annotations. + */ class SpanContext { public: + /** + * Default constructor. Creates an empty context. + */ SpanContext() : trace_id_(0), id_(0), parent_id_(0), is_populated_(false) {} + /** + * Constructor that creates a context object from the given Zipkin span object. + * + * @param span The Zipkin span used to initialize a SpanContext object + */ SpanContext(const Span& span); + /** + * Destructor + */ virtual ~SpanContext() {} + /** + * Serializes the SpanContext object as a string. This encoding of a SpanContext is used + * as the contents of the x-ot-span-context HTTP header, and allows Envoy to track the + * relationships among related Zipkin spans. + * + * @return a string-encoded SpanContext in the following format: + * + * "<16-hex-string trace id>;<16-hex-string span id>;<16-hex-string parent id>; + * + * The annotation list, if present, can contain the strings "cs", "cr", "ss", "sr", depending on + * which annotations are present. The semi-colon character is used as the separator for the + * annotation list. + * + * Example of a returned string corresponding to a span with the SR annotation: + * "25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;sr" + * + * Example of a returned string corresponding to a span with no annotations: + * "25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c" + */ const std::string serializeToString(); + /** + * Initializes a SpanContext object based on the given string. + * + * @param s The string-encoding of a SpanContext in the same format produced by the + * method serializeToString() + */ void populateFromString(std::string s); + /** + * @return the span id as an integer + */ uint64_t id() const { return id_; } + /** + * @return the span id as a 16-character hexadecimal string + */ std::string idAsHexString() const { return Util::uint64ToBase16(id_); } + /** + * @return the span's parent id as an integer + */ uint64_t parent_id() const { return parent_id_; } + /** + * @return the parent id as a 16-character hexadecimal string + */ std::string parentIdAsHexString() const { return Util::uint64ToBase16(parent_id_); } + /** + * @return the trace id as an integer + */ uint64_t trace_id() const { return trace_id_; } + /** + * @return the trace id as a 16-character hexadecimal string + */ std::string traceIdAsHexString() const { return Util::uint64ToBase16(trace_id_); } + /** + * @return a struct indicating which annotations are present in the span + */ Annotation_isset_ isSetAnnotation() const { return annotation_values_; } private: diff --git a/source/common/tracing/zipkin/tracer.cc b/source/common/tracing/zipkin/tracer.cc index ef62a001af59c..2773a947fbdfc 100644 --- a/source/common/tracing/zipkin/tracer.cc +++ b/source/common/tracing/zipkin/tracer.cc @@ -13,7 +13,7 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time) { uint64_t timestamp_micro; // Build the endpoint - getIPAndPort(address_, ip, port); + Util::getIPAndPort(address_, ip, port); ep.setIpv4(ip); ep.setPort(port); ep.setServiceName(service_name_); @@ -51,11 +51,11 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time, Endpoint ep; uint64_t timestamp_micro; - // TODO We currently ignore the start_time to set the span/annotation timestamps + // TODO(fabolive) We currently ignore the start_time to set the span/annotation timestamps // Is start_time really needed? timestamp_micro = Util::timeSinceEpochMicro(); - if ((previous_context.isSetAnnotation().sr) && (!previous_context.isSetAnnotation().cs)) { + if ((previous_context.isSetAnnotation().sr_) && (!previous_context.isSetAnnotation().cs_)) { // We need to create a new span that is a child of the previous span; no shared context // Create a new span id @@ -72,7 +72,8 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time, // Set the timestamp globally for the span span.setTimestamp(timestamp_micro); - } else if ((previous_context.isSetAnnotation().cs) && (!previous_context.isSetAnnotation().sr)) { + } else if ((previous_context.isSetAnnotation().cs_) && + (!previous_context.isSetAnnotation().sr_)) { // We need to create a new span that will share context with the previous span // Initialize the shared context for the new span @@ -86,12 +87,12 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time, } else { // Unexpected condition - // TODO Log an error + // TODO(fabolive) Log an error return span; // return an empty span } // Build the endpoint - getIPAndPort(address_, ip, port); + Util::getIPAndPort(address_, ip, port); ep.setIpv4(ip); ep.setPort(port); ep.setServiceName(service_name_); @@ -121,15 +122,4 @@ void Tracer::reportSpan(Span&& span) { void Tracer::setReporter(ReporterUniquePtr reporter) { reporter_ = ReporterSharedPtr(std::move(reporter)); } - -void Tracer::getIPAndPort(const std::string& address, std::string& ip, uint16_t& port) { - std::regex re("^(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})(:(\\d+))?$"); - std::smatch match; - if (std::regex_search(address, match, re)) { - ip = match.str(1); - if (match.str(3).size() > 0) { - port = std::stoi(match.str(3)); - } - } -} } // Zipkin diff --git a/source/common/tracing/zipkin/tracer.h b/source/common/tracing/zipkin/tracer.h index a1742756ec9b3..fa629b622adc0 100644 --- a/source/common/tracing/zipkin/tracer.h +++ b/source/common/tracing/zipkin/tracer.h @@ -1,5 +1,7 @@ #pragma once +#include "envoy/common/pure.h" + #include "common/tracing/zipkin/span_context.h" #include "common/tracing/zipkin/tracer_interface.h" #include "common/tracing/zipkin/zipkin_core_types.h" @@ -7,51 +9,86 @@ namespace Zipkin { /** - * Abstract class for Tracer users to implement their own span-buffer management policies + * Abstract class that delegates to users of the Tracer class the responsibility + * of "reporting" a Zipkin span that has ended its life cycle. "Reporting" can mean that the + * span will be sent to out to Zipkin, or buffered so that it can be sent out later. */ class Reporter { public: + /** + * Destructor + */ virtual ~Reporter() {} /** - * Span-buffer management policy to be implemented by users of the Trace class + * Method that a concrete Reporter class must implement to handle finished spans. + * For example, a span-buffer management policy could be implemented. + * + * @param span The span that needs action */ - virtual void reportSpan(Span&& span) = 0; + virtual void reportSpan(Span&& span) PURE; }; typedef std::shared_ptr ReporterSharedPtr; typedef std::unique_ptr ReporterUniquePtr; +/** + * This class implements the Zipkin tracer. It has methods to create the appropriate Zipkin span + * type, i.e., root span, child span, or shared-context span. + * + * This class allows its users to supply a concrete Reporter class whose reportSpan method + * is called by its own reportSpan method. By doing so, we have cleanly separated the logic + * of dealing with finished spans from the span-creation and tracing logic. + */ class Tracer : public TracerInterface { public: + /** + * Constructor + * + * @param service_name The name of the service where the Tracer is running. This name is + * used in all annotations' endpoints of the spans created by the Tracer. + * @param address A string in the format :. The IP address and port are used + * in all annotations' endpoints of the spans created by the Tracer. + */ Tracer(const std::string& service_name, const std::string& address) : service_name_(service_name), address_(address) {} virtual ~Tracer() {} /** - * Creates a "root" span + * Creates a "root" Zipkin span. + * + * @param span_name Name of the new span + * @param start_time The time indicating the beginning of the span */ Span startSpan(const std::string& span_name, uint64_t start_time); /** - * Based on the given context, creates either a "child" or a "shared-context" span + * Depending on the given context, creates either a "child" or a "shared-context" Zipkin span. + * + * @param span_name Name of the new span + * @param start_time The time indicating the beginning of the span + * @param previous_context The context of the span preceding the one to be created */ Span startSpan(const std::string& span_name, uint64_t start_time, SpanContext& previous_context); /** - * Called when the Span is finished + * TracerInterface::reportSpan */ void reportSpan(Span&& span) override; + /** + * @return the Reporter associated with the Tracer object + */ ReporterSharedPtr reporter() { return reporter_; } + /** + * Associates a Reporter object with this Tracer + */ void setReporter(ReporterUniquePtr reporter); private: - void getIPAndPort(const std::string& address, std::string& ip, uint16_t& port); - std::string service_name_; std::string address_; diff --git a/source/common/tracing/zipkin/tracer_interface.h b/source/common/tracing/zipkin/tracer_interface.h index e812d7b852fd6..90910573f3085 100644 --- a/source/common/tracing/zipkin/tracer_interface.h +++ b/source/common/tracing/zipkin/tracer_interface.h @@ -1,14 +1,31 @@ #pragma once +#include "envoy/common/pure.h" + namespace Zipkin { class Span; +/** + * This interface must be observed by a Zipkin tracer + */ class TracerInterface { public: + /** + * Destructor + */ virtual ~TracerInterface() {} - virtual void reportSpan(Span&& span) = 0; + /** + * A Zipkin tracer must implement this method. Its implementation must perform whatever + * actions are required when the given span is considered finished. An implementation + * will typically buffer the given span so that it can be flushed later. + * + * This method is invoked by the Span object when its finish() method is called. + * + * @param span The span that needs action + */ + virtual void reportSpan(Span&& span) PURE; }; typedef TracerInterface* TracerRawPtr; diff --git a/source/common/tracing/zipkin/util.cc b/source/common/tracing/zipkin/util.cc index 9af3d79e88869..9222c7ab16e14 100644 --- a/source/common/tracing/zipkin/util.cc +++ b/source/common/tracing/zipkin/util.cc @@ -60,4 +60,15 @@ std::string Util::uint64ToBase16(uint64_t value) { stream << std::setfill('0') << std::setw(16) << std::hex << value; return stream.str(); } + +void Util::getIPAndPort(const std::string& address, std::string& ip, uint16_t& port) { + std::regex re("^(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})(:(\\d+))?$"); + std::smatch match; + if (std::regex_search(address, match, re)) { + ip = match.str(1); + if (match.str(3).size() > 0) { + port = std::stoi(match.str(3)); + } + } +} } // Zipkin diff --git a/source/common/tracing/zipkin/util.h b/source/common/tracing/zipkin/util.h index 9090695b85b45..b6014c9cf0bfa 100644 --- a/source/common/tracing/zipkin/util.h +++ b/source/common/tracing/zipkin/util.h @@ -2,22 +2,72 @@ namespace Zipkin { -typedef std::chrono::system_clock Duration; - +/** + * Utility class with a few convenient methods + */ class Util { public: + // ==== + // Stringified-JSON manipulation + // ==== + + /** + * Merges the stringified JSONs given in target and source. + * + * @param target It will contain the resulting stringified JSON + * @param source The stringified JSON that will be added to target + * @param field_name The key name (added to target's JSON) whose value will be the JSON in source + */ static void mergeJsons(std::string& target, const std::string& source, const std::string& field_name); + /** + * Merges a stringified JSON and a vector of stringified JSONs. + * + * @param target It will contain the resulting stringified JSON + * @param json_array Vector of string pointers, where each element references a stringified JSON + * @param field_name The key name (added to target's JSON) whose value will be a stringified + * JSON array derived from json_array + */ static void addArrayToJson(std::string& target, const std::vector& json_array, const std::string& field_name); + // ==== + // Time manipulation + // ==== + /** + * Returns the time since epoch in microseconds + */ static uint64_t timeSinceEpochMicro(); + /** + * Returns the time since epoch in nanoseconds + */ static uint64_t timeSinceEpochNano(); + // ==== + // Miscellaneous + // ==== + + /** + * Returns a randomly-generated 64-bit integer number + */ static uint64_t generateRandom64(); + /** + * Converts the given 64-bit integer into a hexadecimal string + * + * @param value The integer to be converted + */ static std::string uint64ToBase16(uint64_t value); + + /** + * Extracts the IP address and port from a string of the form ":" + * + * @param address String of the form ":" + * @param ip It will be assigned the IP part of the address string + * @param port It will be assigned the port part of the address string + */ + static void getIPAndPort(const std::string& address, std::string& ip, uint16_t& port); }; } // Zipkin diff --git a/source/common/tracing/zipkin/zipkin_core_types.cc b/source/common/tracing/zipkin/zipkin_core_types.cc index d9d862efef142..7b05c7e8020c2 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.cc +++ b/source/common/tracing/zipkin/zipkin_core_types.cc @@ -164,17 +164,17 @@ const std::string& Span::toJson() { writer.Key(ZipkinJsonFieldNames::SPAN_ID.c_str()); writer.String(Util::uint64ToBase16(id_).c_str()); - if (isset_.parent_id && parent_id_) { + if (isset_.parent_id_ && parent_id_) { writer.Key(ZipkinJsonFieldNames::SPAN_PARENT_ID.c_str()); writer.String(Util::uint64ToBase16(parent_id_).c_str()); } - if (isset_.timestamp) { + if (isset_.timestamp_) { writer.Key(ZipkinJsonFieldNames::SPAN_TIMESTAMP.c_str()); writer.Int64(timestamp_); } - if (isset_.duration) { + if (isset_.duration_) { writer.Key(ZipkinJsonFieldNames::SPAN_DURATION.c_str()); writer.Int64(duration_); } @@ -204,14 +204,14 @@ const std::string& Span::toJson() { void Span::finish() { // Assumption: Span will have only one annotation when this method is called SpanContext context(*this); - if ((context.isSetAnnotation().sr) && (!context.isSetAnnotation().ss)) { + if ((context.isSetAnnotation().sr_) && (!context.isSetAnnotation().ss_)) { // Need to set the SS annotation Annotation ss; ss.setEndpoint(annotations_[0].endpoint()); ss.setTimestamp(Util::timeSinceEpochMicro()); ss.setValue(ZipkinCoreConstants::SERVER_SEND); annotations_.push_back(std::move(ss)); - } else if ((context.isSetAnnotation().cs) && (!context.isSetAnnotation().cr)) { + } else if ((context.isSetAnnotation().cs_) && (!context.isSetAnnotation().cr_)) { // Need to set the CR annotation Annotation cr; uint64_t stop_time = Util::timeSinceEpochMicro(); diff --git a/source/common/tracing/zipkin/zipkin_core_types.h b/source/common/tracing/zipkin/zipkin_core_types.h index 69a9df69bf8fa..669e653139102 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.h +++ b/source/common/tracing/zipkin/zipkin_core_types.h @@ -1,52 +1,117 @@ #pragma once +#include "envoy/common/pure.h" + #include "common/tracing/zipkin/tracer_interface.h" #include "common/tracing/zipkin/util.h" namespace Zipkin { +/** + * Base class to be inherited by all classes that represent Zipkin-related concepts, namely: + * endpoint, annotation, binary annotation, and span. + */ class ZipkinBase { public: + /** + * Destructor + */ virtual ~ZipkinBase() {} - virtual const std::string& toJson() = 0; + /** + * All classes defining Zipkin abstractions need to implement this method to convert + * the corresponding abstraction to a Zipkin-compliant JSON. + */ + virtual const std::string& toJson() PURE; protected: std::string json_string_; }; +/** + * Represents a Zipkin endpoint. This class is based on Zipkin's Thrift definition of an endpoint. + * Endpoints can be added to Zipkin annotations. + */ class Endpoint : public ZipkinBase { public: + /** + * Copy constructor + */ Endpoint(const Endpoint&); + /** + * Assignment operator + */ Endpoint& operator=(const Endpoint&); + /** + * Default constructor. Creates an empty Endpoint. + */ Endpoint() : ipv4_(), port_(0), service_name_(), isset_ipv6_(false) {} + /** + * Constructor that initializes an endpoint with the given attributes. + * + * @param ipv4 String representing the endpoint's ipv4 attribute + * @param port Integer representing the endpoint's port attribute + * @param service_name String representing the endpoint's + */ Endpoint(const std::string& ipv4, uint16_t port, const std::string& service_name) : ipv4_(ipv4), port_(port), service_name_(service_name), isset_ipv6_(false) {} + /** + * @return the endpoint's ipv4 as a string + */ const std::string& ipv4() const { return ipv4_; } + /** + * Sets the endpoint's ipv4 attribute + */ void setIpv4(const std::string& ipv4) { ipv4_ = ipv4; } + /** + * @return the endpoint's ipv6 as a string + */ const std::string& ipv6() const { return ipv6_; } + /** + * Sets the endpoint's ipv6 attribute + */ void setIpv6(const std::string& ipv6) { ipv6_ = ipv6; isset_ipv6_ = true; } + /** + * @return true if the ipv6 attribute is set, or false otherwise + */ bool isSetIpv6() const { return isset_ipv6_; } + /** + * @return the endpoint's port attribute + */ uint16_t port() const { return port_; } + /** + * Sets the endpoint's port attribute + */ void setPort(uint16_t port) { port_ = port; } + /** + * @return the endpoint's service name attribute + */ const std::string& serviceName() const { return service_name_; } + /** + * Sets the endpoint's service name attribute + */ void setServiceName(const std::string& service_name) { service_name_ = service_name; } + /** + * Serializes the endpoint as a Zipkin-compliant JSON representation as a string + * + * @return a stringified JSON + */ const std::string& toJson() override; private: @@ -58,39 +123,89 @@ class Endpoint : public ZipkinBase { bool isset_ipv6_; }; +/** + * Represents a Zipkin basic annotation. This class is based on Zipkin's Thrift definition of + * an annotation. + */ class Annotation : public ZipkinBase { public: + /** + * Copy constructor + */ Annotation(const Annotation&); + /** + * Assignment operator + */ Annotation& operator=(const Annotation&); + /** + * Default constructor. Creates an empty annotation + */ Annotation() : timestamp_(0), value_(), isset_endpoint_(false) {} + /** + * Constructor that creates an annotation based on the given parameters + * + * @param timestamp A 64-bit integer containing the annotation timestasmp attribute + * @param value A string containing the annotation's value attribute. Valid values + * appear on ZipkinCoreConstants. The most commonly used values are "cs", "cr", "ss" and "sr". + * @param endpoint The endpoint object representing the annotation's endpoint attribute + */ Annotation(uint64_t timestamp, const std::string value, Endpoint& endpoint) : timestamp_(timestamp), value_(value), endpoint_(endpoint), isset_endpoint_(true) {} + /** + * @return the annotation's endpoint attribute + */ const Endpoint& endpoint() const { return endpoint_; } + /** + * Sets the annotation's endpoint attribute (copy semantics) + */ void setEndpoint(const Endpoint& endpoint) { endpoint_ = endpoint; isset_endpoint_ = true; } + /** + * Sets the annotation's endpoint attribute (move semantics) + */ void setEndpoint(const Endpoint&& endpoint) { endpoint_ = endpoint; isset_endpoint_ = true; } + /** + * @return the annotation's timestamp attribute + */ uint64_t timestamp() const { return timestamp_; } + /** + * Sets the annotation's timestamp attribute + */ void setTimestamp(uint64_t timestamp) { timestamp_ = timestamp; } + /** + * return the annotation's value attribute + */ const std::string& value() const { return value_; } + /** + * Sets the annotation's value attribute + */ void setValue(const std::string& value) { value_ = value; } + /** + * @return true if the endpoint attribute is set, or false otherwise + */ bool isSetEndpoint() const { return isset_endpoint_; } + /** + * Serializes the annotation as a Zipkin-compliant JSON representation as a string + * + * @return a stringified JSON + */ const std::string& toJson() override; private: @@ -101,45 +216,103 @@ class Annotation : public ZipkinBase { bool isset_endpoint_; }; +/** + * Enum representing valid types of Zipkin binary annotations + */ enum AnnotationType { BOOL = 0, STRING = 1 }; +/** + * Represents a Zipkin binary annotation. This class is based on Zipkin's Thrift definition of + * a binary annotation. A binary annotation allows arbitrary key-value pairs to be associated + * with a Zipkin span. + */ class BinaryAnnotation : public ZipkinBase { public: + /** + * Copy constructor + */ BinaryAnnotation(const BinaryAnnotation&); + /** + * Assignment operator + */ BinaryAnnotation& operator=(const BinaryAnnotation&); + /** + * Default constructor. Creates an empty binary annotation + */ BinaryAnnotation() : key_(), value_(), annotation_type_(STRING), isset_endpoint_(false) {} + /** + * Constructor that creates a binary annotation based on the given parameters + * + * @param key The key name of the annotation + * @param value The value associated with the key + */ BinaryAnnotation(const std::string& key, const std::string& value) : key_(key), value_(value), annotation_type_(STRING), isset_endpoint_(false) {} + /** + * @return the type of the binary annotation + */ AnnotationType annotationType() const { return annotation_type_; } + /** + * Sets the binary's annotation type + */ void setAnnotationType(AnnotationType annotationType) { annotation_type_ = annotationType; } + /** + * @return the annotation's endpoint attribute + */ const Endpoint& endpoint() const { return endpoint_; } + /** + * Sets the annotation's endpoint attribute (copy semantics) + */ void setEndpoint(const Endpoint& endpoint) { endpoint_ = endpoint; isset_endpoint_ = true; } + /** + * Sets the annotation's endpoint attribute (move semantics) + */ void setEndpoint(const Endpoint&& endpoint) { endpoint_ = endpoint; isset_endpoint_ = true; } + /** + * @return true of the endpoint attribute has been set, or false otherwise + */ bool isSetEndpoint() const { return isset_endpoint_; } + /** + * @return the key attribute + */ const std::string& key() const { return key_; } + /** + * Sets the key attribute + */ void setKey(const std::string& key) { key_ = key; } + /** + * @return the value attribute + */ const std::string& value() const { return value_; } + /** + * Sets the value attribute + */ void setValue(const std::string& value) { value_ = value; } + /** + * Serializes the binary annotation as a Zipkin-compliant JSON representation as a string + * + * @return a stringified JSON + */ const std::string& toJson() override; private: @@ -152,106 +325,245 @@ class BinaryAnnotation : public ZipkinBase { bool isset_endpoint_; }; -typedef struct _Span__isset { - _Span__isset() - : parent_id(false), debug(false), timestamp(false), duration(false), trace_id_high(false) {} - bool parent_id : 1; - bool debug : 1; - bool timestamp : 1; - bool duration : 1; - bool trace_id_high : 1; -} _Span__isset; - +/** + * This struct identifies which of the optional attributes are set in a Span object. + * Each member is a one-bit boolean indicating whether or not the corresponding attribute is set. + */ +typedef struct Span_isset_ { + Span_isset_() + : parent_id_(false), debug_(false), timestamp_(false), duration_(false), + trace_id_high_(false) {} + bool parent_id_ : 1; + bool debug_ : 1; + bool timestamp_ : 1; + bool duration_ : 1; + bool trace_id_high_ : 1; +} Span_isset_; + +/** + * Represents a Zipkin span. This class is based on Zipkin's Thrift definition of a span. + */ class Span : public ZipkinBase { public: + /** + * Copy constructor + */ Span(const Span&); + /** + * Assignment operator + */ Span& operator=(const Span&); + /** + * Default constructor. Creates an empty span. + */ Span() : trace_id_(0), name_(), id_(0), parent_id_(0), timestamp_(0), duration_(0), trace_id_high_(0), start_time_(0), tracer_(nullptr) {} + /** + * Sets the span's trace id attribute + */ void setTraceId(const uint64_t val) { trace_id_ = val; } + /** + * Sets the span's name attribute + */ void setName(const std::string& val) { name_ = val; } + /** + * Sets the span's id + */ void setId(const uint64_t val) { id_ = val; } + /** + * Sets the span's parent id + */ void setParentId(const uint64_t val) { parent_id_ = val; - isset_.parent_id = true; + isset_.parent_id_ = true; } + /** + * @return a vector with all annotations added to the span + */ const std::vector& annotations() { return annotations_; } + /** + * Sets the span's annotations all at once + */ void setAannotations(const std::vector& val) { annotations_ = val; } + /** + * Adds an annotation to the span (copy semantics) + */ void addAnnotation(const Annotation& ann) { annotations_.push_back(ann); } + /** + * Adds an annotation to the span (move semantics) + */ void addAnnotation(const Annotation&& ann) { annotations_.push_back(ann); } + /** + * Sets the span's binary annotations all at once + */ void setBinaryAnnotations(const std::vector& val) { binary_annotations_ = val; } + /** + * Adds a binary annotation to the span (copy semantics) + */ void addBinaryAnnotation(const BinaryAnnotation& bann) { binary_annotations_.push_back(bann); } + /** + * Adds a binary annotation to the span (move semantics) + */ void addBinaryAnnotation(const BinaryAnnotation&& bann) { binary_annotations_.push_back(bann); } - void setDebug() { isset_.debug = true; } + /** + * Sets the span's debug attribute + */ + void setDebug() { isset_.debug_ = true; } + /** + * Sets the span's timestamp attribute + */ void setTimestamp(const int64_t val) { timestamp_ = val; - isset_.timestamp = true; + isset_.timestamp_ = true; } + /** + * Sets the span's duration attribute + */ void setDuration(const int64_t val) { duration_ = val; - isset_.duration = true; + isset_.duration_ = true; } + /** + * Sets the higher 64 bits of the span's 128-bit trace id + * Note that this is optional, since 64-bit trace ids are valid + */ void setTraceIdHigh(const uint64_t val) { trace_id_high_ = val; - isset_.trace_id_high = true; + isset_.trace_id_high_ = true; } + /** + * Sets the span start-time attribute + */ void setStartTime(const int64_t time) { start_time_ = time; } + /** + * @return the span's annotations + */ const std::vector& annotations() const { return annotations_; } + /** + * @return the span's binary annotations + */ const std::vector& binaryAnnotations() const { return binary_annotations_; } + /** + * @return the span's duration attribute + */ int64_t duration() const { return duration_; } + /** + * @return the span's id as an integer + */ uint64_t id() const { return id_; } + /** + * @return the span's id as a hexadecimal string + */ std::string idAsHexString() const { return Util::uint64ToBase16(id_); } - const _Span__isset& isSet() const { return isset_; } + /** + * @return a struct indicating which of the span's optional attributes are set + */ + const Span_isset_& isSet() const { return isset_; } + /** + * @return the span's name + */ const std::string& name() const { return name_; } + /** + * @return the span's parent id as an integer + */ uint64_t parentId() const { return parent_id_; } + /** + * @return the span's parent id as a hexadecimal string + */ std::string parentIdAsHexString() const { return Util::uint64ToBase16(parent_id_); } + /** + * @return the span's timestamp + */ int64_t timestamp() const { return timestamp_; } + /** + * @return the span's trace id as an integer + */ uint64_t traceId() const { return trace_id_; } + /** + * @return the span's trace id as a hexadecimal string + */ std::string traceIdAsHexString() const { return Util::uint64ToBase16(trace_id_); } + /** + * @return the higher 64 bits of a 128-bit trace id + */ uint64_t traceIdHigh() const { return trace_id_high_; } + /** + * @return the span's start time + */ int64_t startTime() const { return start_time_; } + /** + * Serializes the span as a Zipkin-compliant JSON representation as a string. + * The resulting JSON string can be used as part of an HTTP POST call to + * send the span to Zipkin + * + * @return a stringified JSON + */ const std::string& toJson() override; + /** + * 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 + * when it is finished. + * + * @param tracer Represents the Tracer object to be associated with the span + */ void setTracer(TracerRawPtr tracer) { tracer_ = tracer; } + /** + * @return the Tracer object associated with the span + */ TracerRawPtr tracer() const { return tracer_; } + /** + * Marks a successful end of the span. This method will: + * + * (1) determine if it needs to add more annotations to the span (e.g., a span containing a CS + * annotation will need to add a CR annotation) and add them; + * (2) compute and set the span's duration; + * (3) invoke the tracer's reportSpan() method if a tracer has been associated with the span. + */ void finish(); + /** + * Adds a binary annotation to the span + * + * @param name The binary annotation's key + * @param value The binary annotation's value + */ void setTag(const std::string& name, const std::string& value); private: @@ -269,6 +581,6 @@ class Span : public ZipkinBase { TracerRawPtr tracer_; - _Span__isset isset_; + Span_isset_ isset_; }; } // Zipkin diff --git a/test/common/tracing/zipkin/span_context_test.cc b/test/common/tracing/zipkin/span_context_test.cc index 4e087a0df7bdc..cad52b468c7d9 100644 --- a/test/common/tracing/zipkin/span_context_test.cc +++ b/test/common/tracing/zipkin/span_context_test.cc @@ -15,10 +15,10 @@ TEST(ZipkinSpanContextTest, populateFromString) { EXPECT_EQ("0000000000000000", span_context.idAsHexString()); EXPECT_EQ(0ULL, span_context.parent_id()); EXPECT_EQ("0000000000000000", span_context.parentIdAsHexString()); - EXPECT_FALSE(span_context.isSetAnnotation().cr); - EXPECT_FALSE(span_context.isSetAnnotation().cs); - EXPECT_FALSE(span_context.isSetAnnotation().sr); - EXPECT_FALSE(span_context.isSetAnnotation().ss); + EXPECT_FALSE(span_context.isSetAnnotation().cr_); + EXPECT_FALSE(span_context.isSetAnnotation().cs_); + EXPECT_FALSE(span_context.isSetAnnotation().sr_); + EXPECT_FALSE(span_context.isSetAnnotation().ss_); EXPECT_EQ("0000000000000000;0000000000000000;0000000000000000", span_context.serializeToString()); // Span context populated with trace id, id, parent id, and no annotations @@ -29,10 +29,10 @@ TEST(ZipkinSpanContextTest, populateFromString) { EXPECT_EQ("56707c7b3e1092af", span_context.idAsHexString()); EXPECT_EQ(14164264937399213340ULL, span_context.parent_id()); EXPECT_EQ("c49193ea42335d1c", span_context.parentIdAsHexString()); - EXPECT_FALSE(span_context.isSetAnnotation().cr); - EXPECT_FALSE(span_context.isSetAnnotation().cs); - EXPECT_FALSE(span_context.isSetAnnotation().sr); - EXPECT_FALSE(span_context.isSetAnnotation().ss); + EXPECT_FALSE(span_context.isSetAnnotation().cr_); + EXPECT_FALSE(span_context.isSetAnnotation().cs_); + EXPECT_FALSE(span_context.isSetAnnotation().sr_); + EXPECT_FALSE(span_context.isSetAnnotation().ss_); EXPECT_EQ("25c6f38dd0600e79;56707c7b3e1092af;c49193ea42335d1c", span_context.serializeToString()); // Span context populated with trace id, id, parent id, and one annotation @@ -43,10 +43,10 @@ TEST(ZipkinSpanContextTest, populateFromString) { EXPECT_EQ("56707c7b3e1092af", span_context.idAsHexString()); EXPECT_EQ(14164264937399213340ULL, span_context.parent_id()); EXPECT_EQ("c49193ea42335d1c", span_context.parentIdAsHexString()); - EXPECT_FALSE(span_context.isSetAnnotation().cr); - EXPECT_TRUE(span_context.isSetAnnotation().cs); - EXPECT_FALSE(span_context.isSetAnnotation().sr); - EXPECT_FALSE(span_context.isSetAnnotation().ss); + EXPECT_FALSE(span_context.isSetAnnotation().cr_); + EXPECT_TRUE(span_context.isSetAnnotation().cs_); + EXPECT_FALSE(span_context.isSetAnnotation().sr_); + EXPECT_FALSE(span_context.isSetAnnotation().ss_); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cs", span_context.serializeToString()); @@ -58,10 +58,10 @@ TEST(ZipkinSpanContextTest, populateFromString) { EXPECT_EQ("56707c7b3e1092af", span_context.idAsHexString()); EXPECT_EQ(14164264937399213340ULL, span_context.parent_id()); EXPECT_EQ("c49193ea42335d1c", span_context.parentIdAsHexString()); - EXPECT_TRUE(span_context.isSetAnnotation().cr); - EXPECT_TRUE(span_context.isSetAnnotation().cs); - EXPECT_FALSE(span_context.isSetAnnotation().sr); - EXPECT_FALSE(span_context.isSetAnnotation().ss); + EXPECT_TRUE(span_context.isSetAnnotation().cr_); + EXPECT_TRUE(span_context.isSetAnnotation().cs_); + EXPECT_FALSE(span_context.isSetAnnotation().sr_); + EXPECT_FALSE(span_context.isSetAnnotation().ss_); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cr;cs", span_context.serializeToString()); @@ -73,10 +73,10 @@ TEST(ZipkinSpanContextTest, populateFromString) { EXPECT_EQ("0000000000000000", span_context.idAsHexString()); EXPECT_EQ(0ULL, span_context.parent_id()); EXPECT_EQ("0000000000000000", span_context.parentIdAsHexString()); - EXPECT_FALSE(span_context.isSetAnnotation().cr); - EXPECT_FALSE(span_context.isSetAnnotation().cs); - EXPECT_FALSE(span_context.isSetAnnotation().sr); - EXPECT_FALSE(span_context.isSetAnnotation().ss); + EXPECT_FALSE(span_context.isSetAnnotation().cr_); + EXPECT_FALSE(span_context.isSetAnnotation().cs_); + EXPECT_FALSE(span_context.isSetAnnotation().sr_); + EXPECT_FALSE(span_context.isSetAnnotation().ss_); EXPECT_EQ("0000000000000000;0000000000000000;0000000000000000", span_context.serializeToString()); } @@ -91,10 +91,10 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { EXPECT_EQ("0000000000000000", span_context.idAsHexString()); EXPECT_EQ(0ULL, span_context.parent_id()); EXPECT_EQ("0000000000000000", span_context.parentIdAsHexString()); - EXPECT_FALSE(span_context.isSetAnnotation().cr); - EXPECT_FALSE(span_context.isSetAnnotation().cs); - EXPECT_FALSE(span_context.isSetAnnotation().sr); - EXPECT_FALSE(span_context.isSetAnnotation().ss); + EXPECT_FALSE(span_context.isSetAnnotation().cr_); + EXPECT_FALSE(span_context.isSetAnnotation().cs_); + EXPECT_FALSE(span_context.isSetAnnotation().sr_); + EXPECT_FALSE(span_context.isSetAnnotation().ss_); EXPECT_EQ("0000000000000000;0000000000000000;0000000000000000", span_context.serializeToString()); // Span context populated with trace id, id, parent id, and no annotations @@ -108,17 +108,17 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { EXPECT_EQ("56707c7b3e1092af", span_context_2.idAsHexString()); EXPECT_EQ(14164264937399213340ULL, span_context_2.parent_id()); EXPECT_EQ("c49193ea42335d1c", span_context_2.parentIdAsHexString()); - EXPECT_FALSE(span_context_2.isSetAnnotation().cr); - EXPECT_FALSE(span_context_2.isSetAnnotation().cs); - EXPECT_FALSE(span_context_2.isSetAnnotation().sr); - EXPECT_FALSE(span_context_2.isSetAnnotation().ss); + EXPECT_FALSE(span_context_2.isSetAnnotation().cr_); + EXPECT_FALSE(span_context_2.isSetAnnotation().cs_); + EXPECT_FALSE(span_context_2.isSetAnnotation().sr_); + EXPECT_FALSE(span_context_2.isSetAnnotation().ss_); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c", span_context_2.serializeToString()); // Test if we can handle 128-bit trace ids - EXPECT_FALSE(span.isSet().trace_id_high); + EXPECT_FALSE(span.isSet().trace_id_high_); span.setTraceIdHigh(9922130815203937912ULL); - EXPECT_TRUE(span.isSet().trace_id_high); + EXPECT_TRUE(span.isSet().trace_id_high_); SpanContext span_context_5(span); // We currently drop the high bits. So, we expect the same context as above EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c", @@ -135,10 +135,10 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { EXPECT_EQ("56707c7b3e1092af", span_context_3.idAsHexString()); EXPECT_EQ(14164264937399213340ULL, span_context_3.parent_id()); EXPECT_EQ("c49193ea42335d1c", span_context_3.parentIdAsHexString()); - EXPECT_FALSE(span_context_3.isSetAnnotation().cr); - EXPECT_FALSE(span_context_3.isSetAnnotation().cs); - EXPECT_TRUE(span_context_3.isSetAnnotation().sr); - EXPECT_FALSE(span_context_3.isSetAnnotation().ss); + EXPECT_FALSE(span_context_3.isSetAnnotation().cr_); + EXPECT_FALSE(span_context_3.isSetAnnotation().cs_); + EXPECT_TRUE(span_context_3.isSetAnnotation().sr_); + EXPECT_FALSE(span_context_3.isSetAnnotation().ss_); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;sr", span_context_3.serializeToString()); @@ -152,10 +152,10 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { EXPECT_EQ("56707c7b3e1092af", span_context_4.idAsHexString()); EXPECT_EQ(14164264937399213340ULL, span_context_4.parent_id()); EXPECT_EQ("c49193ea42335d1c", span_context_4.parentIdAsHexString()); - EXPECT_FALSE(span_context_4.isSetAnnotation().cr); - EXPECT_FALSE(span_context_4.isSetAnnotation().cs); - EXPECT_TRUE(span_context_4.isSetAnnotation().sr); - EXPECT_TRUE(span_context_4.isSetAnnotation().ss); + EXPECT_FALSE(span_context_4.isSetAnnotation().cr_); + EXPECT_FALSE(span_context_4.isSetAnnotation().cs_); + EXPECT_TRUE(span_context_4.isSetAnnotation().sr_); + EXPECT_TRUE(span_context_4.isSetAnnotation().ss_); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;sr;ss", span_context_4.serializeToString()); } diff --git a/test/common/tracing/zipkin/tracer_test.cc b/test/common/tracing/zipkin/tracer_test.cc index 9ef0610d8c7ed..b56a512c77692 100644 --- a/test/common/tracing/zipkin/tracer_test.cc +++ b/test/common/tracing/zipkin/tracer_test.cc @@ -21,7 +21,7 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_NE(0ULL, root_span.traceId()); // trace id must be set EXPECT_EQ(root_span.traceId(), root_span.id()); // span id and trace id must be the same - EXPECT_FALSE(root_span.isSet().parent_id); // no parent set + EXPECT_FALSE(root_span.isSet().parent_id_); // no parent set EXPECT_NE(0LL, root_span.timestamp()); // span's timestamp must be set // A CS annotation must have been added @@ -40,7 +40,7 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_EQ(dynamic_cast(&tracer), root_span.tracer()); // Duration is not set at span-creation time - EXPECT_FALSE(root_span.isSet().duration); + EXPECT_FALSE(root_span.isSet().duration_); // ============== // Test the creation of a shared-context span --> SR @@ -61,11 +61,11 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_EQ(root_span.id(), server_side_shared_context_span.id()); // The parent should be the same as in the CS side (none in this case) - EXPECT_FALSE(server_side_shared_context_span.isSet().parent_id); + EXPECT_FALSE(server_side_shared_context_span.isSet().parent_id_); // span timestamp should not be set (it was set in the CS side) EXPECT_EQ(0LL, server_side_shared_context_span.timestamp()); - EXPECT_FALSE(server_side_shared_context_span.isSet().timestamp); + EXPECT_FALSE(server_side_shared_context_span.isSet().timestamp_); // An SR annotation must have been added EXPECT_EQ(1ULL, server_side_shared_context_span.annotations().size()); @@ -83,7 +83,7 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_EQ(dynamic_cast(&tracer), server_side_shared_context_span.tracer()); // Duration is not set at span-creation time - EXPECT_FALSE(server_side_shared_context_span.isSet().duration); + EXPECT_FALSE(server_side_shared_context_span.isSet().duration_); // ============== // Test the creation of a child span --> CS @@ -103,7 +103,7 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_NE(child_span.traceId(), child_span.id()); // parent should be the previous span - EXPECT_TRUE(child_span.isSet().parent_id); + EXPECT_TRUE(child_span.isSet().parent_id_); EXPECT_EQ(server_side_shared_context_span.id(), child_span.parentId()); // span's timestamp must be set @@ -125,7 +125,7 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_EQ(dynamic_cast(&tracer), child_span.tracer()); // Duration is not set at span-creation time - EXPECT_FALSE(child_span.isSet().duration); + EXPECT_FALSE(child_span.isSet().duration_); } TEST(ZipkinTracerTest, finishSpan) { diff --git a/test/common/tracing/zipkin/zipkin_core_types_test.cc b/test/common/tracing/zipkin/zipkin_core_types_test.cc index c908c263c0cce..e3a6f45b2935b 100644 --- a/test/common/tracing/zipkin/zipkin_core_types_test.cc +++ b/test/common/tracing/zipkin/zipkin_core_types_test.cc @@ -293,11 +293,11 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { EXPECT_EQ(0LL, span.timestamp()); EXPECT_EQ(0LL, span.duration()); EXPECT_EQ(0LL, span.startTime()); - EXPECT_FALSE(span.isSet().debug); - EXPECT_FALSE(span.isSet().duration); - EXPECT_FALSE(span.isSet().parent_id); - EXPECT_FALSE(span.isSet().timestamp); - EXPECT_FALSE(span.isSet().trace_id_high); + EXPECT_FALSE(span.isSet().debug_); + EXPECT_FALSE(span.isSet().duration_); + EXPECT_FALSE(span.isSet().parent_id_); + EXPECT_FALSE(span.isSet().timestamp_); + EXPECT_FALSE(span.isSet().trace_id_high_); EXPECT_EQ("{\"traceId\":\"0000000000000000\",\"name\":\"\",\"id\":\"0000000000000000\"," "\"annotations\":[],\"binaryAnnotations\":[]}", span.toJson()); @@ -313,7 +313,7 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { span.setParentId(id); EXPECT_EQ(id, span.parentId()); EXPECT_EQ(id_hex, span.parentIdAsHexString()); - EXPECT_TRUE(span.isSet().parent_id); + EXPECT_TRUE(span.isSet().parent_id_); id = Util::generateRandom64(); id_hex = Util::uint64ToBase16(id); @@ -325,25 +325,25 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { id_hex = Util::uint64ToBase16(id); span.setTraceIdHigh(id); EXPECT_EQ(id, span.traceIdHigh()); - EXPECT_TRUE(span.isSet().trace_id_high); + EXPECT_TRUE(span.isSet().trace_id_high_); int64_t timestamp = Util::timeSinceEpochMicro(); span.setTimestamp(timestamp); EXPECT_EQ(timestamp, span.timestamp()); - EXPECT_TRUE(span.isSet().timestamp); + EXPECT_TRUE(span.isSet().timestamp_); span.setStartTime(timestamp); EXPECT_EQ(timestamp, span.startTime()); span.setDuration(3000LL); EXPECT_EQ(3000LL, span.duration()); - EXPECT_TRUE(span.isSet().duration); + EXPECT_TRUE(span.isSet().duration_); span.setName("span_name"); EXPECT_EQ("span_name", span.name()); span.setDebug(); - EXPECT_TRUE(span.isSet().debug); + EXPECT_TRUE(span.isSet().debug_); Endpoint endpoint; Annotation ann; @@ -464,11 +464,11 @@ TEST(ZipkinCoreTypesSpanTest, copyConstructor) { EXPECT_EQ(span.timestamp(), span2.timestamp()); EXPECT_EQ(span.duration(), span2.duration()); EXPECT_EQ(span.startTime(), span2.startTime()); - EXPECT_EQ(span.isSet().debug, span2.isSet().debug); - EXPECT_EQ(span.isSet().duration, span2.isSet().duration); - EXPECT_EQ(span.isSet().parent_id, span2.isSet().parent_id); - EXPECT_EQ(span.isSet().timestamp, span2.isSet().timestamp); - EXPECT_EQ(span.isSet().trace_id_high, span2.isSet().trace_id_high); + EXPECT_EQ(span.isSet().debug_, span2.isSet().debug_); + EXPECT_EQ(span.isSet().duration_, span2.isSet().duration_); + EXPECT_EQ(span.isSet().parent_id_, span2.isSet().parent_id_); + EXPECT_EQ(span.isSet().timestamp_, span2.isSet().timestamp_); + EXPECT_EQ(span.isSet().trace_id_high_, span2.isSet().trace_id_high_); } TEST(ZipkinCoreTypesSpanTest, assignmentOperator) { @@ -499,11 +499,11 @@ TEST(ZipkinCoreTypesSpanTest, assignmentOperator) { EXPECT_EQ(span.timestamp(), span2.timestamp()); EXPECT_EQ(span.duration(), span2.duration()); EXPECT_EQ(span.startTime(), span2.startTime()); - EXPECT_EQ(span.isSet().debug, span2.isSet().debug); - EXPECT_EQ(span.isSet().duration, span2.isSet().duration); - EXPECT_EQ(span.isSet().parent_id, span2.isSet().parent_id); - EXPECT_EQ(span.isSet().timestamp, span2.isSet().timestamp); - EXPECT_EQ(span.isSet().trace_id_high, span2.isSet().trace_id_high); + EXPECT_EQ(span.isSet().debug_, span2.isSet().debug_); + EXPECT_EQ(span.isSet().duration_, span2.isSet().duration_); + EXPECT_EQ(span.isSet().parent_id_, span2.isSet().parent_id_); + EXPECT_EQ(span.isSet().timestamp_, span2.isSet().timestamp_); + EXPECT_EQ(span.isSet().trace_id_high_, span2.isSet().trace_id_high_); } TEST(ZipkinCoreTypesSpanTest, setTag) { From b994a8cc33d27a9f4a6a6409a40ed3fa483e90cf Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Wed, 12 Apr 2017 19:43:16 -0400 Subject: [PATCH 27/55] Optimized the conversion from uint64_t into a 16-char hex string (#430) --- source/common/tracing/zipkin/util.cc | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/source/common/tracing/zipkin/util.cc b/source/common/tracing/zipkin/util.cc index 9222c7ab16e14..0122a919b295f 100644 --- a/source/common/tracing/zipkin/util.cc +++ b/source/common/tracing/zipkin/util.cc @@ -4,6 +4,7 @@ #include "rapidjson/stringbuffer.h" #include "rapidjson/document.h" +#include "common/common/hex.h" #include "common/tracing/zipkin/util.h" namespace Zipkin { @@ -56,9 +57,18 @@ uint64_t Util::generateRandom64() { } std::string Util::uint64ToBase16(uint64_t value) { - std::stringstream stream; - stream << std::setfill('0') << std::setw(16) << std::hex << value; - return stream.str(); + std::vector data(8); + + data[7] = (value & 0x00000000000000FF); + data[6] = (value & 0x000000000000FF00) >> 8; + data[5] = (value & 0x0000000000FF0000) >> 16; + data[4] = (value & 0x00000000FF000000) >> 24; + data[3] = (value & 0x000000FF00000000) >> 32; + data[2] = (value & 0x0000FF0000000000) >> 40; + data[1] = (value & 0x00FF000000000000) >> 48; + data[0] = (value & 0xFF00000000000000) >> 56; + + return Hex::encode(&data[0], data.size()); } void Util::getIPAndPort(const std::string& address, std::string& ip, uint16_t& port) { From 9b001e5661ad5393f1971c971d8a5dbdf4f1ded2 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Wed, 12 Apr 2017 20:02:25 -0400 Subject: [PATCH 28/55] Minor fixes, optimizations, and stylistic changes (#430) --- source/common/tracing/zipkin/span_context.cc | 28 ++++++++++---------- source/common/tracing/zipkin/span_context.h | 2 +- source/common/tracing/zipkin/tracer.cc | 3 --- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/source/common/tracing/zipkin/span_context.cc b/source/common/tracing/zipkin/span_context.cc index bd0e2b44a31df..b172b84f8bccf 100644 --- a/source/common/tracing/zipkin/span_context.cc +++ b/source/common/tracing/zipkin/span_context.cc @@ -10,17 +10,17 @@ SpanContext::SpanContext(const Span& span) { id_ = span.id(); parent_id_ = span.parentId(); - for (Annotation ann : span.annotations()) { - if (ann.value() == ZipkinCoreConstants::CLIENT_RECV) { + for (const Annotation& annotation : span.annotations()) { + if (annotation.value() == ZipkinCoreConstants::CLIENT_RECV) { annotation_values_.cr_ = true; } - if (ann.value() == ZipkinCoreConstants::CLIENT_SEND) { + if (annotation.value() == ZipkinCoreConstants::CLIENT_SEND) { annotation_values_.cs_ = true; } - if (ann.value() == ZipkinCoreConstants::SERVER_RECV) { + if (annotation.value() == ZipkinCoreConstants::SERVER_RECV) { annotation_values_.sr_ = true; } - if (ann.value() == ZipkinCoreConstants::SERVER_SEND) { + if (annotation.value() == ZipkinCoreConstants::SERVER_SEND) { annotation_values_.ss_ = true; } } @@ -33,27 +33,27 @@ const std::string SpanContext::serializeToString() { return "0000000000000000;0000000000000000;0000000000000000"; } - std::string s; - s = traceIdAsHexString() + FIELD_SEPARATOR_ + idAsHexString() + FIELD_SEPARATOR_ + - parentIdAsHexString(); + std::string result; + result = traceIdAsHexString() + FIELD_SEPARATOR_ + idAsHexString() + FIELD_SEPARATOR_ + + parentIdAsHexString(); if (annotation_values_.cr_) { - s += FIELD_SEPARATOR_ + ZipkinCoreConstants::CLIENT_RECV; + result += FIELD_SEPARATOR_ + ZipkinCoreConstants::CLIENT_RECV; } if (annotation_values_.cs_) { - s += FIELD_SEPARATOR_ + ZipkinCoreConstants::CLIENT_SEND; + result += FIELD_SEPARATOR_ + ZipkinCoreConstants::CLIENT_SEND; } if (annotation_values_.sr_) { - s += FIELD_SEPARATOR_ + ZipkinCoreConstants::SERVER_RECV; + result += FIELD_SEPARATOR_ + ZipkinCoreConstants::SERVER_RECV; } if (annotation_values_.ss_) { - s += FIELD_SEPARATOR_ + ZipkinCoreConstants::SERVER_SEND; + result += FIELD_SEPARATOR_ + ZipkinCoreConstants::SERVER_SEND; } - return s; + return result; } -void SpanContext::populateFromString(std::string s) { +void SpanContext::populateFromString(const std::string& s) { // ^([0-9,a-z]{16});([0-9,a-z]{16});([0-9,a-z]{16})((;(cs|sr|cr|ss))*)$ std::string hex_digit_group = "([0-9,a-z]{16})"; std::string reg_ex_str = diff --git a/source/common/tracing/zipkin/span_context.h b/source/common/tracing/zipkin/span_context.h index 998028752b89a..7ede37b8be8e6 100644 --- a/source/common/tracing/zipkin/span_context.h +++ b/source/common/tracing/zipkin/span_context.h @@ -75,7 +75,7 @@ class SpanContext { * @param s The string-encoding of a SpanContext in the same format produced by the * method serializeToString() */ - void populateFromString(std::string s); + void populateFromString(const std::string& s); /** * @return the span id as an integer diff --git a/source/common/tracing/zipkin/tracer.cc b/source/common/tracing/zipkin/tracer.cc index 2773a947fbdfc..c3784f2d2e259 100644 --- a/source/common/tracing/zipkin/tracer.cc +++ b/source/common/tracing/zipkin/tracer.cc @@ -85,9 +85,6 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time, // Set the SR annotation value annotation.setValue(ZipkinCoreConstants::SERVER_RECV); } else { - // Unexpected condition - - // TODO(fabolive) Log an error return span; // return an empty span } From 04e373e48c31ce6b238bd0c3aa5475bb641a810a Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Thu, 13 Apr 2017 09:56:13 -0400 Subject: [PATCH 29/55] Minor improvements and clean-up to SpanBuffer (#430) --- source/common/tracing/zipkin/span_buffer.cc | 9 +++----- source/common/tracing/zipkin/span_buffer.h | 23 +++++++++---------- .../common/tracing/zipkin/span_buffer_test.cc | 8 +++---- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/source/common/tracing/zipkin/span_buffer.cc b/source/common/tracing/zipkin/span_buffer.cc index b9c6182e457b8..1753d2fa308dd 100644 --- a/source/common/tracing/zipkin/span_buffer.cc +++ b/source/common/tracing/zipkin/span_buffer.cc @@ -2,13 +2,10 @@ namespace Zipkin { -void SpanBuffer::allocateBuffer(uint64_t size) { - span_buffer_.resize(size); - size_ = size; -} +void SpanBuffer::allocateBuffer(uint64_t size) { span_buffer_.resize(size); } bool SpanBuffer::addSpan(Span&& span) { - if (next_position_ == size_) { + if (next_position_ == span_buffer_.size()) { // Buffer full return false; } @@ -17,7 +14,7 @@ bool SpanBuffer::addSpan(Span&& span) { return true; } -void SpanBuffer::flush() { +void SpanBuffer::clear() { next_position_ = 0; std::fill(span_buffer_.begin(), span_buffer_.end(), nullptr); } diff --git a/source/common/tracing/zipkin/span_buffer.h b/source/common/tracing/zipkin/span_buffer.h index de0a93952064d..9f542a98d0b38 100644 --- a/source/common/tracing/zipkin/span_buffer.h +++ b/source/common/tracing/zipkin/span_buffer.h @@ -16,33 +16,33 @@ class SpanBuffer { * Constructor that creates an empty buffer. Space needs to be allocated by invoking * the method allocateBuffer(size). */ - SpanBuffer() : size_(0), next_position_(0) {} + SpanBuffer() : next_position_(0) {} /** - * Constructor that initializes a buffer with the given size + * Constructor that initializes a buffer with the given size. * - * @param size The desired buffer size + * @param size The desired buffer size. */ - SpanBuffer(uint64_t size) : span_buffer_(size, nullptr), size_(size), next_position_(0) {} + SpanBuffer(uint64_t size) : span_buffer_(size, nullptr), next_position_(0) {} /** - * Destructor + * Destructor. */ virtual ~SpanBuffer() {} /** * Allocates space for an empty buffer or resizes a previously-allocated one. * - * @param size The desired buffer size + * @param size The desired buffer size. */ void allocateBuffer(uint64_t size); /** * Adds the given Zipkin span to the buffer. * - * @param span The span to be added to the buffer + * @param span The span to be added to the buffer. * - * @return true if the span was successfully added, or false if the buffer was full + * @return true if the span was successfully added, or false if the buffer was full. */ bool addSpan(Span&& span); @@ -50,23 +50,22 @@ class SpanBuffer { * Empties the buffer. This method is supposed to be called when all buffered spans * have been sent to to the Zipkin service. */ - void flush(); + void clear(); /** - * @return the number of spans currently buffered + * @return the number of spans currently buffered. */ uint64_t pendingSpans() { return next_position_; } /** * @return the contents of the buffer as a stringified array of JSONs, where - * each JSON in the array corresponds to one Zipkin span + * each JSON in the array corresponds to one Zipkin span. */ std::string toStringifiedJsonArray(); private: // We use a pre-allocated vector to improve performance std::vector span_buffer_; - uint64_t size_; uint64_t next_position_; }; } // Zipkin diff --git a/test/common/tracing/zipkin/span_buffer_test.cc b/test/common/tracing/zipkin/span_buffer_test.cc index 27fbbd1b5d5db..33ce69d7538aa 100644 --- a/test/common/tracing/zipkin/span_buffer_test.cc +++ b/test/common/tracing/zipkin/span_buffer_test.cc @@ -26,7 +26,7 @@ TEST(ZipkinSpanBufferTest, defaultConstructorEndToEnd) { "]"; EXPECT_EQ(expected_json_array_string, buffer.toStringifiedJsonArray()); - buffer.flush(); + buffer.clear(); EXPECT_EQ(0ULL, buffer.pendingSpans()); EXPECT_EQ("[]", buffer.toStringifiedJsonArray()); @@ -48,7 +48,7 @@ TEST(ZipkinSpanBufferTest, defaultConstructorEndToEnd) { EXPECT_EQ(2ULL, buffer.pendingSpans()); EXPECT_EQ(expected_json_array_string, buffer.toStringifiedJsonArray()); - buffer.flush(); + buffer.clear(); EXPECT_EQ(0ULL, buffer.pendingSpans()); EXPECT_EQ("[]", buffer.toStringifiedJsonArray()); } @@ -71,7 +71,7 @@ TEST(ZipkinSpanBufferTest, sizeConstructorEndtoEnd) { "]"; EXPECT_EQ(expected_json_array_string, buffer.toStringifiedJsonArray()); - buffer.flush(); + buffer.clear(); EXPECT_EQ(0ULL, buffer.pendingSpans()); EXPECT_EQ("[]", buffer.toStringifiedJsonArray()); @@ -93,7 +93,7 @@ TEST(ZipkinSpanBufferTest, sizeConstructorEndtoEnd) { EXPECT_EQ(2ULL, buffer.pendingSpans()); EXPECT_EQ(expected_json_array_string, buffer.toStringifiedJsonArray()); - buffer.flush(); + buffer.clear(); EXPECT_EQ(0ULL, buffer.pendingSpans()); EXPECT_EQ("[]", buffer.toStringifiedJsonArray()); } From dfda5be95c8536c1e0e10db0fc99ca55f4954f32 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Thu, 13 Apr 2017 15:10:55 -0400 Subject: [PATCH 30/55] Cleaning up SpanContext (#430) --- source/common/tracing/zipkin/span_context.cc | 72 +++++++++++++------- source/common/tracing/zipkin/span_context.h | 56 ++++++++++----- 2 files changed, 84 insertions(+), 44 deletions(-) diff --git a/source/common/tracing/zipkin/span_context.cc b/source/common/tracing/zipkin/span_context.cc index b172b84f8bccf..83e154d157703 100644 --- a/source/common/tracing/zipkin/span_context.cc +++ b/source/common/tracing/zipkin/span_context.cc @@ -1,10 +1,41 @@ +#include "common/common/utility.h" + #include "common/tracing/zipkin/zipkin_core_constants.h" #include "common/tracing/zipkin/span_context.h" namespace Zipkin { +// String that separates the span-context fields in its string-serialized form. const std::string SpanContext::FIELD_SEPARATOR_ = ";"; +// String value corresponding to an empty span context. +const std::string SpanContext::UNITIALIZED_SPAN_CONTEXT_ = "0000000000000000" + FIELD_SEPARATOR_ + + "0000000000000000" + FIELD_SEPARATOR_ + + "0000000000000000"; + +// String with regular expression to match a 16-digit hexadecimal number. +const std::string SpanContext::HEX_DIGIT_GROUP_REGEX_STR_ = "([0-9,a-z]{16})"; + +// The static functions below are needed due to C++ inability to safely concatenate static strings +// belonging to different compilation units (the initialization order is not guaranteed). + +const std::string& SpanContext::SPAN_CONTEXT_REGEX_STR() { + // ^([0-9,a-z]{16});([0-9,a-z]{16});([0-9,a-z]{16})((;(cs|sr|cr|ss))*)$ + static std::string span_context_regex_str = + "^" + HEX_DIGIT_GROUP_REGEX_STR_ + FIELD_SEPARATOR_ + HEX_DIGIT_GROUP_REGEX_STR_ + + FIELD_SEPARATOR_ + HEX_DIGIT_GROUP_REGEX_STR_ + "((" + FIELD_SEPARATOR_ + "(" + + ZipkinCoreConstants::CLIENT_SEND + "|" + ZipkinCoreConstants::SERVER_RECV + "|" + + ZipkinCoreConstants::CLIENT_RECV + "|" + ZipkinCoreConstants::SERVER_SEND + "))*)$"; + + return span_context_regex_str; +} + +const std::regex& SpanContext::SPAN_CONTEXT_REGEX() { + static std::regex span_context_regex(SPAN_CONTEXT_REGEX_STR()); + + return span_context_regex; +} + SpanContext::SpanContext(const Span& span) { trace_id_ = span.traceId(); id_ = span.id(); @@ -25,12 +56,12 @@ SpanContext::SpanContext(const Span& span) { } } - is_populated_ = true; + is_initialized_ = true; } const std::string SpanContext::serializeToString() { - if (!is_populated_) { - return "0000000000000000;0000000000000000;0000000000000000"; + if (!is_initialized_) { + return UNITIALIZED_SPAN_CONTEXT_; } std::string result; @@ -53,23 +84,14 @@ const std::string SpanContext::serializeToString() { return result; } -void SpanContext::populateFromString(const std::string& s) { - // ^([0-9,a-z]{16});([0-9,a-z]{16});([0-9,a-z]{16})((;(cs|sr|cr|ss))*)$ - std::string hex_digit_group = "([0-9,a-z]{16})"; - std::string reg_ex_str = - "^" + hex_digit_group + FIELD_SEPARATOR_ + hex_digit_group + FIELD_SEPARATOR_ + - hex_digit_group + "((" + FIELD_SEPARATOR_ + "(" + ZipkinCoreConstants::CLIENT_SEND + "|" + - ZipkinCoreConstants::SERVER_RECV + "|" + ZipkinCoreConstants::CLIENT_RECV + "|" + - ZipkinCoreConstants::SERVER_SEND + "))*)$"; - - std::regex re(reg_ex_str); +void SpanContext::populateFromString(const std::string& span_context_str) { std::smatch match; trace_id_ = parent_id_ = id_ = 0; annotation_values_.cs_ = annotation_values_.cr_ = annotation_values_.ss_ = annotation_values_.sr_ = false; - if (std::regex_search(s, match, re)) { + if (std::regex_search(span_context_str, match, SPAN_CONTEXT_REGEX())) { // This is a valid string encoding of the context trace_id_ = stoull(match.str(1), nullptr, 16); id_ = stoull(match.str(2), nullptr, 16); @@ -77,27 +99,25 @@ void SpanContext::populateFromString(const std::string& s) { std::string matched_annotations = match.str(4); if (matched_annotations.size() > 0) { - char* annotation_value_strings = const_cast(matched_annotations.c_str()); - char* annotation_value = std::strtok(annotation_value_strings, FIELD_SEPARATOR_.c_str()); - - while (annotation_value) { - if (!std::strcmp(annotation_value, ZipkinCoreConstants::CLIENT_RECV.c_str())) { + std::vector annotation_value_strings = + StringUtil::split(matched_annotations, FIELD_SEPARATOR_); + for (const std::string& annotation_value : annotation_value_strings) { + // while (annotation_value) { + if (annotation_value == ZipkinCoreConstants::CLIENT_RECV) { annotation_values_.cr_ = true; - } else if (!std::strcmp(annotation_value, ZipkinCoreConstants::CLIENT_SEND.c_str())) { + } else if (annotation_value == ZipkinCoreConstants::CLIENT_SEND) { annotation_values_.cs_ = true; - } else if (!std::strcmp(annotation_value, ZipkinCoreConstants::SERVER_RECV.c_str())) { + } else if (annotation_value == ZipkinCoreConstants::SERVER_RECV) { annotation_values_.sr_ = true; - } else if (!std::strcmp(annotation_value, ZipkinCoreConstants::SERVER_SEND.c_str())) { + } else if (annotation_value == ZipkinCoreConstants::SERVER_SEND) { annotation_values_.ss_ = true; } - - annotation_value = std::strtok(NULL, FIELD_SEPARATOR_.c_str()); } } - is_populated_ = true; + is_initialized_ = true; } else { - is_populated_ = false; + is_initialized_ = false; } } } // Zipkin diff --git a/source/common/tracing/zipkin/span_context.h b/source/common/tracing/zipkin/span_context.h index 7ede37b8be8e6..adbc5c64a1ca4 100644 --- a/source/common/tracing/zipkin/span_context.h +++ b/source/common/tracing/zipkin/span_context.h @@ -17,13 +17,13 @@ namespace Zipkin { * SS: "Server Send" * SR: "Server Receive" */ -typedef struct Annotation_isset_ { - Annotation_isset_() : cs_(false), cr_(false), ss_(false), sr_(false) {} +typedef struct AnnotationIsSet { + AnnotationIsSet() : cs_(false), cr_(false), ss_(false), sr_(false) {} bool cs_ : 1; bool cr_ : 1; bool ss_ : 1; bool sr_ : 1; -} Annotation_isset_; +} AnnotationIsSet; /** * This class represents the context of a Zipkin span. It embodies the following @@ -34,12 +34,12 @@ class SpanContext { /** * Default constructor. Creates an empty context. */ - SpanContext() : trace_id_(0), id_(0), parent_id_(0), is_populated_(false) {} + SpanContext() : trace_id_(0), id_(0), parent_id_(0), is_initialized_(false) {} /** * Constructor that creates a context object from the given Zipkin span object. * - * @param span The Zipkin span used to initialize a SpanContext object + * @param span The Zipkin span used to initialize a SpanContext object. */ SpanContext(const Span& span); @@ -72,10 +72,10 @@ class SpanContext { /** * Initializes a SpanContext object based on the given string. * - * @param s The string-encoding of a SpanContext in the same format produced by the - * method serializeToString() + * @param span_context_str The string-encoding of a SpanContext in the same format produced by the + * method serializeToString(). */ - void populateFromString(const std::string& s); + void populateFromString(const std::string& span_context_str); /** * @return the span id as an integer @@ -83,41 +83,61 @@ class SpanContext { uint64_t id() const { return id_; } /** - * @return the span id as a 16-character hexadecimal string + * @return the span id as a 16-character hexadecimal string. */ std::string idAsHexString() const { return Util::uint64ToBase16(id_); } /** - * @return the span's parent id as an integer + * @return the span's parent id as an integer. */ uint64_t parent_id() const { return parent_id_; } /** - * @return the parent id as a 16-character hexadecimal string + * @return the parent id as a 16-character hexadecimal string. */ std::string parentIdAsHexString() const { return Util::uint64ToBase16(parent_id_); } /** - * @return the trace id as an integer + * @return the trace id as an integer. */ uint64_t trace_id() const { return trace_id_; } /** - * @return the trace id as a 16-character hexadecimal string + * @return the trace id as a 16-character hexadecimal string. */ std::string traceIdAsHexString() const { return Util::uint64ToBase16(trace_id_); } /** - * @return a struct indicating which annotations are present in the span + * @return a struct indicating which annotations are present in the span. */ - Annotation_isset_ isSetAnnotation() const { return annotation_values_; } + AnnotationIsSet isSetAnnotation() const { return annotation_values_; } private: - const static std::string FIELD_SEPARATOR_; + static const std::string FIELD_SEPARATOR_; + static const std::string UNITIALIZED_SPAN_CONTEXT_; + static const std::string HEX_DIGIT_GROUP_REGEX_STR_; + + /** + * @return a string with a regular expression to match a valid string-serialized span context. + * + * Note that a function is needed because we cannot concatenate static strings from + * different compilation units at initialization time (the initialization order is not + * guaranteed). In this case, the compilation units are ZipkinCoreConstants and SpanContext. + */ + static const std::string& SPAN_CONTEXT_REGEX_STR(); + + /** + * @return a regex to match a valid string-serialization of a span context. + * + * Note that a function is needed because the string used to build the regex + * cannot be initialized statically. + */ + static const std::regex& SPAN_CONTEXT_REGEX(); + uint64_t trace_id_; uint64_t id_; uint64_t parent_id_; - Annotation_isset_ annotation_values_; - bool is_populated_; + AnnotationIsSet annotation_values_; + bool is_initialized_; }; } // Zipkin From 4cd118ab19239037c812d9f3d1ba600b94d217c1 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Thu, 13 Apr 2017 15:22:18 -0400 Subject: [PATCH 31/55] Minor clean-ups on Tracer (#430) --- source/common/tracing/zipkin/tracer.cc | 17 +++++++------- source/common/tracing/zipkin/tracer.h | 22 +++++++++---------- .../common/tracing/zipkin/tracer_interface.h | 6 ++--- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/source/common/tracing/zipkin/tracer.cc b/source/common/tracing/zipkin/tracer.cc index c3784f2d2e259..a2fcec9fca908 100644 --- a/source/common/tracing/zipkin/tracer.cc +++ b/source/common/tracing/zipkin/tracer.cc @@ -5,24 +5,22 @@ namespace Zipkin { Span Tracer::startSpan(const std::string& span_name, uint64_t start_time) { - Span span; - Annotation cs; + // Build the endpoint std::string ip; uint16_t port; - Endpoint ep; - uint64_t timestamp_micro; - - // Build the endpoint Util::getIPAndPort(address_, ip, port); + Endpoint ep; ep.setIpv4(ip); ep.setPort(port); ep.setServiceName(service_name_); // Build the CS annotation + Annotation cs; cs.setEndpoint(std::move(ep)); cs.setValue(ZipkinCoreConstants::CLIENT_SEND); // Create an all-new span, with no parent id + Span span; span.setName(span_name); uint64_t randon_number = Util::generateRandom64(); span.setId(randon_number); @@ -30,6 +28,7 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time) { span.setStartTime(start_time); // Set the timestamp globally for the span and also for the CS annotation + uint64_t timestamp_micro; timestamp_micro = Util::timeSinceEpochMicro(); cs.setTimestamp(timestamp_micro); span.setTimestamp(timestamp_micro); @@ -46,9 +45,6 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time, SpanContext& previous_context) { Span span; Annotation annotation; - std::string ip; - uint16_t port; - Endpoint ep; uint64_t timestamp_micro; // TODO(fabolive) We currently ignore the start_time to set the span/annotation timestamps @@ -89,7 +85,10 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time, } // Build the endpoint + std::string ip; + uint16_t port; Util::getIPAndPort(address_, ip, port); + Endpoint ep; ep.setIpv4(ip); ep.setPort(port); ep.setServiceName(service_name_); diff --git a/source/common/tracing/zipkin/tracer.h b/source/common/tracing/zipkin/tracer.h index fa629b622adc0..3c93c411471b4 100644 --- a/source/common/tracing/zipkin/tracer.h +++ b/source/common/tracing/zipkin/tracer.h @@ -16,7 +16,7 @@ namespace Zipkin { class Reporter { public: /** - * Destructor + * Destructor. */ virtual ~Reporter() {} @@ -24,7 +24,7 @@ class Reporter { * Method that a concrete Reporter class must implement to handle finished spans. * For example, a span-buffer management policy could be implemented. * - * @param span The span that needs action + * @param span The span that needs action. */ virtual void reportSpan(Span&& span) PURE; }; @@ -44,7 +44,7 @@ typedef std::unique_ptr ReporterUniquePtr; class Tracer : public TracerInterface { public: /** - * Constructor + * Constructor. * * @param service_name The name of the service where the Tracer is running. This name is * used in all annotations' endpoints of the spans created by the Tracer. @@ -59,32 +59,32 @@ class Tracer : public TracerInterface { /** * Creates a "root" Zipkin span. * - * @param span_name Name of the new span - * @param start_time The time indicating the beginning of the span + * @param span_name Name of the new span. + * @param start_time The time indicating the beginning of the span. */ Span startSpan(const std::string& span_name, uint64_t start_time); /** * Depending on the given context, creates either a "child" or a "shared-context" Zipkin span. * - * @param span_name Name of the new span - * @param start_time The time indicating the beginning of the span - * @param previous_context The context of the span preceding the one to be created + * @param span_name Name of the new span. + * @param start_time The time indicating the beginning of the span. + * @param previous_context The context of the span preceding the one to be created. */ Span startSpan(const std::string& span_name, uint64_t start_time, SpanContext& previous_context); /** - * TracerInterface::reportSpan + * TracerInterface::reportSpan. */ void reportSpan(Span&& span) override; /** - * @return the Reporter associated with the Tracer object + * @return the Reporter associated with the Tracer object. */ ReporterSharedPtr reporter() { return reporter_; } /** - * Associates a Reporter object with this Tracer + * Associates a Reporter object with this Tracer. */ void setReporter(ReporterUniquePtr reporter); diff --git a/source/common/tracing/zipkin/tracer_interface.h b/source/common/tracing/zipkin/tracer_interface.h index 90910573f3085..1c514362d6712 100644 --- a/source/common/tracing/zipkin/tracer_interface.h +++ b/source/common/tracing/zipkin/tracer_interface.h @@ -7,12 +7,12 @@ namespace Zipkin { class Span; /** - * This interface must be observed by a Zipkin tracer + * This interface must be observed by a Zipkin tracer. */ class TracerInterface { public: /** - * Destructor + * Destructor. */ virtual ~TracerInterface() {} @@ -23,7 +23,7 @@ class TracerInterface { * * This method is invoked by the Span object when its finish() method is called. * - * @param span The span that needs action + * @param span The span that needs action. */ virtual void reportSpan(Span&& span) PURE; }; From 41c0f572c697988217007b775a9fecb1cf27df78 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Thu, 13 Apr 2017 16:28:18 -0400 Subject: [PATCH 32/55] Renamed utility method name, removed unused header file, and made minor changes to comments (#430) --- source/common/tracing/zipkin/span_context.h | 6 ++-- source/common/tracing/zipkin/util.cc | 4 +-- source/common/tracing/zipkin/util.h | 34 +++++++++---------- .../tracing/zipkin/zipkin_core_types.cc | 6 ++-- .../common/tracing/zipkin/zipkin_core_types.h | 6 ++-- test/common/tracing/zipkin/util_test.cc | 2 +- .../tracing/zipkin/zipkin_core_types_test.cc | 12 +++---- 7 files changed, 34 insertions(+), 36 deletions(-) diff --git a/source/common/tracing/zipkin/span_context.h b/source/common/tracing/zipkin/span_context.h index adbc5c64a1ca4..42d6c80c37ce4 100644 --- a/source/common/tracing/zipkin/span_context.h +++ b/source/common/tracing/zipkin/span_context.h @@ -85,7 +85,7 @@ class SpanContext { /** * @return the span id as a 16-character hexadecimal string. */ - std::string idAsHexString() const { return Util::uint64ToBase16(id_); } + std::string idAsHexString() const { return Util::uint64ToHex(id_); } /** * @return the span's parent id as an integer. @@ -95,7 +95,7 @@ class SpanContext { /** * @return the parent id as a 16-character hexadecimal string. */ - std::string parentIdAsHexString() const { return Util::uint64ToBase16(parent_id_); } + std::string parentIdAsHexString() const { return Util::uint64ToHex(parent_id_); } /** * @return the trace id as an integer. @@ -105,7 +105,7 @@ class SpanContext { /** * @return the trace id as a 16-character hexadecimal string. */ - std::string traceIdAsHexString() const { return Util::uint64ToBase16(trace_id_); } + std::string traceIdAsHexString() const { return Util::uint64ToHex(trace_id_); } /** * @return a struct indicating which annotations are present in the span. diff --git a/source/common/tracing/zipkin/util.cc b/source/common/tracing/zipkin/util.cc index 0122a919b295f..ff02df2a06b81 100644 --- a/source/common/tracing/zipkin/util.cc +++ b/source/common/tracing/zipkin/util.cc @@ -1,5 +1,3 @@ -#include - #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" #include "rapidjson/document.h" @@ -56,7 +54,7 @@ uint64_t Util::generateRandom64() { return rand_64(); } -std::string Util::uint64ToBase16(uint64_t value) { +std::string Util::uint64ToHex(uint64_t value) { std::vector data(8); data[7] = (value & 0x00000000000000FF); diff --git a/source/common/tracing/zipkin/util.h b/source/common/tracing/zipkin/util.h index b6014c9cf0bfa..6a1f5af170285 100644 --- a/source/common/tracing/zipkin/util.h +++ b/source/common/tracing/zipkin/util.h @@ -14,9 +14,9 @@ class Util { /** * Merges the stringified JSONs given in target and source. * - * @param target It will contain the resulting stringified JSON - * @param source The stringified JSON that will be added to target - * @param field_name The key name (added to target's JSON) whose value will be the JSON in source + * @param target It will contain the resulting stringified JSON. + * @param source The stringified JSON that will be added to target. + * @param field_name The key name (added to target's JSON) whose value will be the JSON in source. */ static void mergeJsons(std::string& target, const std::string& source, const std::string& field_name); @@ -24,10 +24,10 @@ class Util { /** * Merges a stringified JSON and a vector of stringified JSONs. * - * @param target It will contain the resulting stringified JSON - * @param json_array Vector of string pointers, where each element references a stringified JSON - * @param field_name The key name (added to target's JSON) whose value will be a stringified - * JSON array derived from json_array + * @param target It will contain the resulting stringified JSON. + * @param json_array Vector of string pointers, where each element references a stringified JSON. + * @param field_name The key name (added to target's JSON) whose value will be a stringified. + * JSON array derived from json_array. */ static void addArrayToJson(std::string& target, const std::vector& json_array, const std::string& field_name); @@ -36,12 +36,12 @@ class Util { // ==== /** - * Returns the time since epoch in microseconds + * Returns the time since epoch in microseconds. */ static uint64_t timeSinceEpochMicro(); /** - * Returns the time since epoch in nanoseconds + * Returns the time since epoch in nanoseconds. */ static uint64_t timeSinceEpochNano(); @@ -50,23 +50,23 @@ class Util { // ==== /** - * Returns a randomly-generated 64-bit integer number + * Returns a randomly-generated 64-bit integer number. */ static uint64_t generateRandom64(); /** - * Converts the given 64-bit integer into a hexadecimal string + * Converts the given 64-bit integer into a hexadecimal string. * - * @param value The integer to be converted + * @param value The integer to be converted. */ - static std::string uint64ToBase16(uint64_t value); + static std::string uint64ToHex(uint64_t value); /** - * Extracts the IP address and port from a string of the form ":" + * Extracts the IP address and port from a string of the form ":". * - * @param address String of the form ":" - * @param ip It will be assigned the IP part of the address string - * @param port It will be assigned the port part of the address string + * @param address String of the form ":". + * @param ip It will be assigned the IP part of the address string. + * @param port It will be assigned the port part of the address string. */ static void getIPAndPort(const std::string& address, std::string& ip, uint16_t& port); }; diff --git a/source/common/tracing/zipkin/zipkin_core_types.cc b/source/common/tracing/zipkin/zipkin_core_types.cc index 7b05c7e8020c2..dcc63d0b5a27a 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.cc +++ b/source/common/tracing/zipkin/zipkin_core_types.cc @@ -158,15 +158,15 @@ const std::string& Span::toJson() { rapidjson::Writer writer(s); writer.StartObject(); writer.Key(ZipkinJsonFieldNames::SPAN_TRACE_ID.c_str()); - writer.String(Util::uint64ToBase16(trace_id_).c_str()); + writer.String(Util::uint64ToHex(trace_id_).c_str()); writer.Key(ZipkinJsonFieldNames::SPAN_NAME.c_str()); writer.String(name_.c_str()); writer.Key(ZipkinJsonFieldNames::SPAN_ID.c_str()); - writer.String(Util::uint64ToBase16(id_).c_str()); + writer.String(Util::uint64ToHex(id_).c_str()); if (isset_.parent_id_ && parent_id_) { writer.Key(ZipkinJsonFieldNames::SPAN_PARENT_ID.c_str()); - writer.String(Util::uint64ToBase16(parent_id_).c_str()); + writer.String(Util::uint64ToHex(parent_id_).c_str()); } if (isset_.timestamp_) { diff --git a/source/common/tracing/zipkin/zipkin_core_types.h b/source/common/tracing/zipkin/zipkin_core_types.h index 669e653139102..72676fef01bd2 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.h +++ b/source/common/tracing/zipkin/zipkin_core_types.h @@ -478,7 +478,7 @@ class Span : public ZipkinBase { /** * @return the span's id as a hexadecimal string */ - std::string idAsHexString() const { return Util::uint64ToBase16(id_); } + std::string idAsHexString() const { return Util::uint64ToHex(id_); } /** * @return a struct indicating which of the span's optional attributes are set @@ -498,7 +498,7 @@ class Span : public ZipkinBase { /** * @return the span's parent id as a hexadecimal string */ - std::string parentIdAsHexString() const { return Util::uint64ToBase16(parent_id_); } + std::string parentIdAsHexString() const { return Util::uint64ToHex(parent_id_); } /** * @return the span's timestamp @@ -513,7 +513,7 @@ class Span : public ZipkinBase { /** * @return the span's trace id as a hexadecimal string */ - std::string traceIdAsHexString() const { return Util::uint64ToBase16(trace_id_); } + std::string traceIdAsHexString() const { return Util::uint64ToHex(trace_id_); } /** * @return the higher 64 bits of a 128-bit trace id diff --git a/test/common/tracing/zipkin/util_test.cc b/test/common/tracing/zipkin/util_test.cc index 9a9942fd5f18d..7c8c823676424 100644 --- a/test/common/tracing/zipkin/util_test.cc +++ b/test/common/tracing/zipkin/util_test.cc @@ -9,7 +9,7 @@ TEST(ZipkinUtilTest, utilTests) { EXPECT_EQ(typeid(uint64_t).name(), typeid(Util::timeSinceEpochMicro()).name()); EXPECT_EQ(typeid(uint64_t).name(), typeid(Util::timeSinceEpochNano()).name()); - std::string base16_string = Util::uint64ToBase16(2722130815203937912ULL); + std::string base16_string = Util::uint64ToHex(2722130815203937912ULL); EXPECT_EQ(typeid(std::string).name(), typeid(base16_string).name()); EXPECT_EQ("25c6f38dd0600e78", base16_string); diff --git a/test/common/tracing/zipkin/zipkin_core_types_test.cc b/test/common/tracing/zipkin/zipkin_core_types_test.cc index e3a6f45b2935b..e790aeb0f8e02 100644 --- a/test/common/tracing/zipkin/zipkin_core_types_test.cc +++ b/test/common/tracing/zipkin/zipkin_core_types_test.cc @@ -303,26 +303,26 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { span.toJson()); uint64_t id = Util::generateRandom64(); - std::string id_hex = Util::uint64ToBase16(id); + std::string id_hex = Util::uint64ToHex(id); span.setId(id); EXPECT_EQ(id, span.id()); EXPECT_EQ(id_hex, span.idAsHexString()); id = Util::generateRandom64(); - id_hex = Util::uint64ToBase16(id); + id_hex = Util::uint64ToHex(id); span.setParentId(id); EXPECT_EQ(id, span.parentId()); EXPECT_EQ(id_hex, span.parentIdAsHexString()); EXPECT_TRUE(span.isSet().parent_id_); id = Util::generateRandom64(); - id_hex = Util::uint64ToBase16(id); + id_hex = Util::uint64ToHex(id); span.setTraceId(id); EXPECT_EQ(id, span.traceId()); EXPECT_EQ(id_hex, span.traceIdAsHexString()); id = Util::generateRandom64(); - id_hex = Util::uint64ToBase16(id); + id_hex = Util::uint64ToHex(id); span.setTraceIdHigh(id); EXPECT_EQ(id, span.traceIdHigh()); EXPECT_TRUE(span.isSet().trace_id_high_); @@ -440,7 +440,7 @@ TEST(ZipkinCoreTypesSpanTest, copyConstructor) { Span span; uint64_t id = Util::generateRandom64(); - std::string id_hex = Util::uint64ToBase16(id); + std::string id_hex = Util::uint64ToHex(id); span.setId(id); span.setParentId(id); span.setTraceId(id); @@ -475,7 +475,7 @@ TEST(ZipkinCoreTypesSpanTest, assignmentOperator) { Span span; uint64_t id = Util::generateRandom64(); - std::string id_hex = Util::uint64ToBase16(id); + std::string id_hex = Util::uint64ToHex(id); span.setId(id); span.setParentId(id); span.setTraceId(id); From a54f4338921ba756f42dec01f5d3b4766cd7d1ad Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Thu, 13 Apr 2017 16:37:06 -0400 Subject: [PATCH 33/55] Fixed struct name. Minor changes to comments (#430) --- .../common/tracing/zipkin/zipkin_core_types.h | 196 +++++++++--------- 1 file changed, 98 insertions(+), 98 deletions(-) diff --git a/source/common/tracing/zipkin/zipkin_core_types.h b/source/common/tracing/zipkin/zipkin_core_types.h index 72676fef01bd2..c9880bc8c77c9 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.h +++ b/source/common/tracing/zipkin/zipkin_core_types.h @@ -14,7 +14,7 @@ namespace Zipkin { class ZipkinBase { public: /** - * Destructor + * Destructor. */ virtual ~ZipkinBase() {} @@ -35,12 +35,12 @@ class ZipkinBase { class Endpoint : public ZipkinBase { public: /** - * Copy constructor + * Copy constructor. */ Endpoint(const Endpoint&); /** - * Assignment operator + * Assignment operator. */ Endpoint& operator=(const Endpoint&); @@ -52,30 +52,30 @@ class Endpoint : public ZipkinBase { /** * Constructor that initializes an endpoint with the given attributes. * - * @param ipv4 String representing the endpoint's ipv4 attribute - * @param port Integer representing the endpoint's port attribute - * @param service_name String representing the endpoint's + * @param ipv4 String representing the endpoint's ipv4 attribute. + * @param port Integer representing the endpoint's port attribute. + * @param service_name String representing the endpoint's. */ Endpoint(const std::string& ipv4, uint16_t port, const std::string& service_name) : ipv4_(ipv4), port_(port), service_name_(service_name), isset_ipv6_(false) {} /** - * @return the endpoint's ipv4 as a string + * @return the endpoint's ipv4 as a string. */ const std::string& ipv4() const { return ipv4_; } /** - * Sets the endpoint's ipv4 attribute + * Sets the endpoint's ipv4 attribute. */ void setIpv4(const std::string& ipv4) { ipv4_ = ipv4; } /** - * @return the endpoint's ipv6 as a string + * @return the endpoint's ipv6 as a string. */ const std::string& ipv6() const { return ipv6_; } /** - * Sets the endpoint's ipv6 attribute + * Sets the endpoint's ipv6 attribute. */ void setIpv6(const std::string& ipv6) { ipv6_ = ipv6; @@ -83,34 +83,34 @@ class Endpoint : public ZipkinBase { } /** - * @return true if the ipv6 attribute is set, or false otherwise + * @return true if the ipv6 attribute is set, or false otherwise. */ bool isSetIpv6() const { return isset_ipv6_; } /** - * @return the endpoint's port attribute + * @return the endpoint's port attribute. */ uint16_t port() const { return port_; } /** - * Sets the endpoint's port attribute + * Sets the endpoint's port attribute. */ void setPort(uint16_t port) { port_ = port; } /** - * @return the endpoint's service name attribute + * @return the endpoint's service name attribute. */ const std::string& serviceName() const { return service_name_; } /** - * Sets the endpoint's service name attribute + * Sets the endpoint's service name attribute. */ void setServiceName(const std::string& service_name) { service_name_ = service_name; } /** - * Serializes the endpoint as a Zipkin-compliant JSON representation as a string + * Serializes the endpoint as a Zipkin-compliant JSON representation as a string. * - * @return a stringified JSON + * @return a stringified JSON. */ const std::string& toJson() override; @@ -130,38 +130,38 @@ class Endpoint : public ZipkinBase { class Annotation : public ZipkinBase { public: /** - * Copy constructor + * Copy constructor. */ Annotation(const Annotation&); /** - * Assignment operator + * Assignment operator. */ Annotation& operator=(const Annotation&); /** - * Default constructor. Creates an empty annotation + * Default constructor. Creates an empty annotation. */ Annotation() : timestamp_(0), value_(), isset_endpoint_(false) {} /** - * Constructor that creates an annotation based on the given parameters + * Constructor that creates an annotation based on the given parameters. * - * @param timestamp A 64-bit integer containing the annotation timestasmp attribute + * @param timestamp A 64-bit integer containing the annotation timestasmp attribute. * @param value A string containing the annotation's value attribute. Valid values * appear on ZipkinCoreConstants. The most commonly used values are "cs", "cr", "ss" and "sr". - * @param endpoint The endpoint object representing the annotation's endpoint attribute + * @param endpoint The endpoint object representing the annotation's endpoint attribute. */ Annotation(uint64_t timestamp, const std::string value, Endpoint& endpoint) : timestamp_(timestamp), value_(value), endpoint_(endpoint), isset_endpoint_(true) {} /** - * @return the annotation's endpoint attribute + * @return the annotation's endpoint attribute. */ const Endpoint& endpoint() const { return endpoint_; } /** - * Sets the annotation's endpoint attribute (copy semantics) + * Sets the annotation's endpoint attribute (copy semantics). */ void setEndpoint(const Endpoint& endpoint) { endpoint_ = endpoint; @@ -169,7 +169,7 @@ class Annotation : public ZipkinBase { } /** - * Sets the annotation's endpoint attribute (move semantics) + * Sets the annotation's endpoint attribute (move semantics). */ void setEndpoint(const Endpoint&& endpoint) { endpoint_ = endpoint; @@ -177,34 +177,34 @@ class Annotation : public ZipkinBase { } /** - * @return the annotation's timestamp attribute + * @return the annotation's timestamp attribute. */ uint64_t timestamp() const { return timestamp_; } /** - * Sets the annotation's timestamp attribute + * Sets the annotation's timestamp attribute. */ void setTimestamp(uint64_t timestamp) { timestamp_ = timestamp; } /** - * return the annotation's value attribute + * return the annotation's value attribute. */ const std::string& value() const { return value_; } /** - * Sets the annotation's value attribute + * Sets the annotation's value attribute. */ void setValue(const std::string& value) { value_ = value; } /** - * @return true if the endpoint attribute is set, or false otherwise + * @return true if the endpoint attribute is set, or false otherwise. */ bool isSetEndpoint() const { return isset_endpoint_; } /** - * Serializes the annotation as a Zipkin-compliant JSON representation as a string + * Serializes the annotation as a Zipkin-compliant JSON representation as a string. * - * @return a stringified JSON + * @return a stringified JSON. */ const std::string& toJson() override; @@ -217,7 +217,7 @@ class Annotation : public ZipkinBase { }; /** - * Enum representing valid types of Zipkin binary annotations + * Enum representing valid types of Zipkin binary annotations. */ enum AnnotationType { BOOL = 0, STRING = 1 }; @@ -229,46 +229,46 @@ enum AnnotationType { BOOL = 0, STRING = 1 }; class BinaryAnnotation : public ZipkinBase { public: /** - * Copy constructor + * Copy constructor. */ BinaryAnnotation(const BinaryAnnotation&); /** - * Assignment operator + * Assignment operator. */ BinaryAnnotation& operator=(const BinaryAnnotation&); /** - * Default constructor. Creates an empty binary annotation + * Default constructor. Creates an empty binary annotation. */ BinaryAnnotation() : key_(), value_(), annotation_type_(STRING), isset_endpoint_(false) {} /** - * Constructor that creates a binary annotation based on the given parameters + * Constructor that creates a binary annotation based on the given parameters. * - * @param key The key name of the annotation - * @param value The value associated with the key + * @param key The key name of the annotation. + * @param value The value associated with the key. */ BinaryAnnotation(const std::string& key, const std::string& value) : key_(key), value_(value), annotation_type_(STRING), isset_endpoint_(false) {} /** - * @return the type of the binary annotation + * @return the type of the binary annotation. */ AnnotationType annotationType() const { return annotation_type_; } /** - * Sets the binary's annotation type + * Sets the binary's annotation type. */ void setAnnotationType(AnnotationType annotationType) { annotation_type_ = annotationType; } /** - * @return the annotation's endpoint attribute + * @return the annotation's endpoint attribute. */ const Endpoint& endpoint() const { return endpoint_; } /** - * Sets the annotation's endpoint attribute (copy semantics) + * Sets the annotation's endpoint attribute (copy semantics). */ void setEndpoint(const Endpoint& endpoint) { endpoint_ = endpoint; @@ -276,7 +276,7 @@ class BinaryAnnotation : public ZipkinBase { } /** - * Sets the annotation's endpoint attribute (move semantics) + * Sets the annotation's endpoint attribute (move semantics). */ void setEndpoint(const Endpoint&& endpoint) { endpoint_ = endpoint; @@ -284,34 +284,34 @@ class BinaryAnnotation : public ZipkinBase { } /** - * @return true of the endpoint attribute has been set, or false otherwise + * @return true of the endpoint attribute has been set, or false otherwise. */ bool isSetEndpoint() const { return isset_endpoint_; } /** - * @return the key attribute + * @return the key attribute. */ const std::string& key() const { return key_; } /** - * Sets the key attribute + * Sets the key attribute. */ void setKey(const std::string& key) { key_ = key; } /** - * @return the value attribute + * @return the value attribute. */ const std::string& value() const { return value_; } /** - * Sets the value attribute + * Sets the value attribute. */ void setValue(const std::string& value) { value_ = value; } /** - * Serializes the binary annotation as a Zipkin-compliant JSON representation as a string + * Serializes the binary annotation as a Zipkin-compliant JSON representation as a string. * - * @return a stringified JSON + * @return a stringified JSON. */ const std::string& toJson() override; @@ -329,8 +329,8 @@ class BinaryAnnotation : public ZipkinBase { * This struct identifies which of the optional attributes are set in a Span object. * Each member is a one-bit boolean indicating whether or not the corresponding attribute is set. */ -typedef struct Span_isset_ { - Span_isset_() +typedef struct SpanIsSet { + SpanIsSet() : parent_id_(false), debug_(false), timestamp_(false), duration_(false), trace_id_high_(false) {} bool parent_id_ : 1; @@ -338,7 +338,7 @@ typedef struct Span_isset_ { bool timestamp_ : 1; bool duration_ : 1; bool trace_id_high_ : 1; -} Span_isset_; +} SpanIsSet; /** * Represents a Zipkin span. This class is based on Zipkin's Thrift definition of a span. @@ -346,12 +346,12 @@ typedef struct Span_isset_ { class Span : public ZipkinBase { public: /** - * Copy constructor + * Copy constructor. */ Span(const Span&); /** - * Assignment operator + * Assignment operator. */ Span& operator=(const Span&); @@ -363,22 +363,22 @@ class Span : public ZipkinBase { trace_id_high_(0), start_time_(0), tracer_(nullptr) {} /** - * Sets the span's trace id attribute + * Sets the span's trace id attribute. */ void setTraceId(const uint64_t val) { trace_id_ = val; } /** - * Sets the span's name attribute + * Sets the span's name attribute. */ void setName(const std::string& val) { name_ = val; } /** - * Sets the span's id + * Sets the span's id. */ void setId(const uint64_t val) { id_ = val; } /** - * Sets the span's parent id + * Sets the span's parent id. */ void setParentId(const uint64_t val) { parent_id_ = val; @@ -386,47 +386,47 @@ class Span : public ZipkinBase { } /** - * @return a vector with all annotations added to the span + * @return a vector with all annotations added to the span. */ const std::vector& annotations() { return annotations_; } /** - * Sets the span's annotations all at once + * Sets the span's annotations all at once. */ void setAannotations(const std::vector& val) { annotations_ = val; } /** - * Adds an annotation to the span (copy semantics) + * Adds an annotation to the span (copy semantics). */ void addAnnotation(const Annotation& ann) { annotations_.push_back(ann); } /** - * Adds an annotation to the span (move semantics) + * Adds an annotation to the span (move semantics). */ void addAnnotation(const Annotation&& ann) { annotations_.push_back(ann); } /** - * Sets the span's binary annotations all at once + * Sets the span's binary annotations all at once. */ void setBinaryAnnotations(const std::vector& val) { binary_annotations_ = val; } /** - * Adds a binary annotation to the span (copy semantics) + * Adds a binary annotation to the span (copy semantics). */ void addBinaryAnnotation(const BinaryAnnotation& bann) { binary_annotations_.push_back(bann); } /** - * Adds a binary annotation to the span (move semantics) + * Adds a binary annotation to the span (move semantics). */ void addBinaryAnnotation(const BinaryAnnotation&& bann) { binary_annotations_.push_back(bann); } /** - * Sets the span's debug attribute + * Sets the span's debug attribute. */ void setDebug() { isset_.debug_ = true; } /** - * Sets the span's timestamp attribute + * Sets the span's timestamp attribute. */ void setTimestamp(const int64_t val) { timestamp_ = val; @@ -434,7 +434,7 @@ class Span : public ZipkinBase { } /** - * Sets the span's duration attribute + * Sets the span's duration attribute. */ void setDuration(const int64_t val) { duration_ = val; @@ -442,8 +442,8 @@ class Span : public ZipkinBase { } /** - * Sets the higher 64 bits of the span's 128-bit trace id - * Note that this is optional, since 64-bit trace ids are valid + * Sets the higher 64 bits of the span's 128-bit trace id. + * Note that this is optional, since 64-bit trace ids are valid. */ void setTraceIdHigh(const uint64_t val) { trace_id_high_ = val; @@ -451,86 +451,86 @@ class Span : public ZipkinBase { } /** - * Sets the span start-time attribute + * Sets the span start-time attribute. */ void setStartTime(const int64_t time) { start_time_ = time; } /** - * @return the span's annotations + * @return the span's annotations. */ const std::vector& annotations() const { return annotations_; } /** - * @return the span's binary annotations + * @return the span's binary annotations. */ const std::vector& binaryAnnotations() const { return binary_annotations_; } /** - * @return the span's duration attribute + * @return the span's duration attribute. */ int64_t duration() const { return duration_; } /** - * @return the span's id as an integer + * @return the span's id as an integer. */ uint64_t id() const { return id_; } /** - * @return the span's id as a hexadecimal string + * @return the span's id as a hexadecimal string. */ std::string idAsHexString() const { return Util::uint64ToHex(id_); } /** - * @return a struct indicating which of the span's optional attributes are set + * @return a struct indicating which of the span's optional attributes are set. */ - const Span_isset_& isSet() const { return isset_; } + const SpanIsSet& isSet() const { return isset_; } /** - * @return the span's name + * @return the span's name. */ const std::string& name() const { return name_; } /** - * @return the span's parent id as an integer + * @return the span's parent id as an integer. */ uint64_t parentId() const { return parent_id_; } /** - * @return the span's parent id as a hexadecimal string + * @return the span's parent id as a hexadecimal string. */ std::string parentIdAsHexString() const { return Util::uint64ToHex(parent_id_); } /** - * @return the span's timestamp + * @return the span's timestamp. */ int64_t timestamp() const { return timestamp_; } /** - * @return the span's trace id as an integer + * @return the span's trace id as an integer. */ uint64_t traceId() const { return trace_id_; } /** - * @return the span's trace id as a hexadecimal string + * @return the span's trace id as a hexadecimal string. */ std::string traceIdAsHexString() const { return Util::uint64ToHex(trace_id_); } /** - * @return the higher 64 bits of a 128-bit trace id + * @return the higher 64 bits of a 128-bit trace id. */ uint64_t traceIdHigh() const { return trace_id_high_; } /** - * @return the span's start time + * @return the span's start time. */ int64_t startTime() const { return start_time_; } /** * Serializes the span as a Zipkin-compliant JSON representation as a string. * The resulting JSON string can be used as part of an HTTP POST call to - * send the span to Zipkin + * send the span to Zipkin. * - * @return a stringified JSON + * @return a stringified JSON. */ const std::string& toJson() override; @@ -539,12 +539,12 @@ class Span : public ZipkinBase { * by the span's finish() method so that the tracer can decide what to do with the span * when it is finished. * - * @param tracer Represents the Tracer object to be associated with the span + * @param tracer Represents the Tracer object to be associated with the span. */ void setTracer(TracerRawPtr tracer) { tracer_ = tracer; } /** - * @return the Tracer object associated with the span + * @return the Tracer object associated with the span. */ TracerRawPtr tracer() const { return tracer_; } @@ -553,16 +553,16 @@ class Span : public ZipkinBase { * * (1) determine if it needs to add more annotations to the span (e.g., a span containing a CS * annotation will need to add a CR annotation) and add them; - * (2) compute and set the span's duration; + * (2) compute and set the span's duration; and * (3) invoke the tracer's reportSpan() method if a tracer has been associated with the span. */ void finish(); /** - * Adds a binary annotation to the span + * Adds a binary annotation to the span. * - * @param name The binary annotation's key - * @param value The binary annotation's value + * @param name The binary annotation's key. + * @param value The binary annotation's value. */ void setTag(const std::string& name, const std::string& value); @@ -581,6 +581,6 @@ class Span : public ZipkinBase { TracerRawPtr tracer_; - Span_isset_ isset_; + SpanIsSet isset_; }; } // Zipkin From e0ecb7718c3e0a84244ef89a7e8d8dfcf9e914e2 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Thu, 13 Apr 2017 16:42:53 -0400 Subject: [PATCH 34/55] Code clean-up (#430) --- source/common/tracing/zipkin/span_context.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/source/common/tracing/zipkin/span_context.cc b/source/common/tracing/zipkin/span_context.cc index 83e154d157703..c263c668ae2d0 100644 --- a/source/common/tracing/zipkin/span_context.cc +++ b/source/common/tracing/zipkin/span_context.cc @@ -102,7 +102,6 @@ void SpanContext::populateFromString(const std::string& span_context_str) { std::vector annotation_value_strings = StringUtil::split(matched_annotations, FIELD_SEPARATOR_); for (const std::string& annotation_value : annotation_value_strings) { - // while (annotation_value) { if (annotation_value == ZipkinCoreConstants::CLIENT_RECV) { annotation_values_.cr_ = true; } else if (annotation_value == ZipkinCoreConstants::CLIENT_SEND) { From c1e8841302ea8b6ea75182f97dc4e298abf81b69 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Fri, 14 Apr 2017 15:24:56 -0400 Subject: [PATCH 35/55] Improved legibility of tests by using raw strings (#430) --- .../common/tracing/zipkin/span_buffer_test.cc | 83 ++++++----- .../tracing/zipkin/zipkin_core_types_test.cc | 136 +++++++++--------- 2 files changed, 114 insertions(+), 105 deletions(-) diff --git a/test/common/tracing/zipkin/span_buffer_test.cc b/test/common/tracing/zipkin/span_buffer_test.cc index 33ce69d7538aa..2dec48ea80614 100644 --- a/test/common/tracing/zipkin/span_buffer_test.cc +++ b/test/common/tracing/zipkin/span_buffer_test.cc @@ -17,13 +17,13 @@ TEST(ZipkinSpanBufferTest, defaultConstructorEndToEnd) { buffer.addSpan(std::move(span)); EXPECT_EQ(1ULL, buffer.pendingSpans()); - std::string expected_json_array_string = "" - "[{\"traceId\":\"0000000000000000\"," - "\"name\":\"\"," - "\"id\":\"0000000000000000\"," - "\"annotations\":[]," - "\"binaryAnnotations\":[]}" - "]"; + std::string expected_json_array_string = "[{" + R"("traceId":"0000000000000000",)" + R"("name":"",)" + R"("id":"0000000000000000",)" + R"("annotations":[],)" + R"("binaryAnnotations":[])" + "}]"; EXPECT_EQ(expected_json_array_string, buffer.toStringifiedJsonArray()); buffer.clear(); @@ -32,18 +32,21 @@ TEST(ZipkinSpanBufferTest, defaultConstructorEndToEnd) { buffer.addSpan(std::move(span)); buffer.addSpan(Span(span)); - expected_json_array_string = "" - "[" - "{\"traceId\":\"0000000000000000\"," - "\"name\":\"\"," - "\"id\":\"0000000000000000\"," - "\"annotations\":[]," - "\"binaryAnnotations\":[]}," - "{\"traceId\":\"0000000000000000\"," - "\"name\":\"\"," - "\"id\":\"0000000000000000\"," - "\"annotations\":[]," - "\"binaryAnnotations\":[]}" + expected_json_array_string = "[" + "{" + R"("traceId":"0000000000000000",)" + R"("name":"",)" + R"("id":"0000000000000000",)" + R"("annotations":[],)" + R"("binaryAnnotations":[])" + "}," + "{" + R"("traceId":"0000000000000000",)" + R"("name":"",)" + R"("id":"0000000000000000",)" + R"("annotations":[],)" + R"("binaryAnnotations":[])" + "}" "]"; EXPECT_EQ(2ULL, buffer.pendingSpans()); EXPECT_EQ(expected_json_array_string, buffer.toStringifiedJsonArray()); @@ -62,13 +65,13 @@ TEST(ZipkinSpanBufferTest, sizeConstructorEndtoEnd) { buffer.addSpan(std::move(span)); EXPECT_EQ(1ULL, buffer.pendingSpans()); - std::string expected_json_array_string = "" - "[{\"traceId\":\"0000000000000000\"," - "\"name\":\"\"," - "\"id\":\"0000000000000000\"," - "\"annotations\":[]," - "\"binaryAnnotations\":[]}" - "]"; + std::string expected_json_array_string = "[{" + R"("traceId":"0000000000000000",)" + R"("name":"",)" + R"("id":"0000000000000000",)" + R"("annotations":[],)" + R"("binaryAnnotations":[])" + "}]"; EXPECT_EQ(expected_json_array_string, buffer.toStringifiedJsonArray()); buffer.clear(); @@ -77,19 +80,21 @@ TEST(ZipkinSpanBufferTest, sizeConstructorEndtoEnd) { buffer.addSpan(std::move(span)); buffer.addSpan(Span(span)); - expected_json_array_string = "" - "[" - "{\"traceId\":\"0000000000000000\"," - "\"name\":\"\"," - "\"id\":\"0000000000000000\"," - "\"annotations\":[]," - "\"binaryAnnotations\":[]}," - "{\"traceId\":\"0000000000000000\"," - "\"name\":\"\"," - "\"id\":\"0000000000000000\"," - "\"annotations\":[]," - "\"binaryAnnotations\":[]}" - "]"; + expected_json_array_string = "[" + "{" + R"("traceId":"0000000000000000",)" + R"("name":"",)" + R"("id":"0000000000000000",)" + R"("annotations":[],)" + R"("binaryAnnotations":[])" + "}," + "{" + R"("traceId":"0000000000000000",)" + R"("name":"",)" + R"("id":"0000000000000000",)" + R"("annotations":[],)" + R"("binaryAnnotations":[])" + "}]"; EXPECT_EQ(2ULL, buffer.pendingSpans()); EXPECT_EQ(expected_json_array_string, buffer.toStringifiedJsonArray()); diff --git a/test/common/tracing/zipkin/zipkin_core_types_test.cc b/test/common/tracing/zipkin/zipkin_core_types_test.cc index e790aeb0f8e02..46f7d288f3cf3 100644 --- a/test/common/tracing/zipkin/zipkin_core_types_test.cc +++ b/test/common/tracing/zipkin/zipkin_core_types_test.cc @@ -12,7 +12,7 @@ TEST(ZipkinCoreTypesEndpointTest, defaultConstructor) { EXPECT_EQ(0, ep.port()); EXPECT_EQ("", ep.serviceName()); EXPECT_FALSE(ep.isSetIpv6()); - EXPECT_EQ("{\"ipv4\":\"\",\"port\":0,\"serviceName\":\"\"}", ep.toJson()); + EXPECT_EQ(R"({"ipv4":"","port":0,"serviceName":""})", ep.toJson()); ep.setIpv4(std::string("127.0.0.1")); EXPECT_EQ("127.0.0.1", ep.ipv4()); @@ -27,8 +27,8 @@ TEST(ZipkinCoreTypesEndpointTest, defaultConstructor) { ep.setServiceName("my_service"); EXPECT_EQ("my_service", ep.serviceName()); - EXPECT_EQ("{\"ipv4\":\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\",\"ipv6\":\"2001:" - "0db8:85a3:0000:0000:8a2e:0370:7334\"}", + EXPECT_EQ(R"({"ipv4":"127.0.0.1","port":3306,"serviceName":"my_service",)" + R"("ipv6":"2001:0db8:85a3:0000:0000:8a2e:0370:7334"})", ep.toJson()); } @@ -39,14 +39,14 @@ TEST(ZipkinCoreTypesEndpointTest, customConstructor) { EXPECT_EQ(3306, ep.port()); EXPECT_EQ("my_service", ep.serviceName()); EXPECT_FALSE(ep.isSetIpv6()); - EXPECT_EQ("{\"ipv4\":\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\"}", ep.toJson()); + EXPECT_EQ(R"({"ipv4":"127.0.0.1","port":3306,"serviceName":"my_service"})", ep.toJson()); ep.setIpv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); EXPECT_EQ("2001:0db8:85a3:0000:0000:8a2e:0370:7334", ep.ipv6()); EXPECT_TRUE(ep.isSetIpv6()); - EXPECT_EQ("{\"ipv4\":\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\",\"ipv6\":\"2001:" - "0db8:85a3:0000:0000:8a2e:0370:7334\"}", + EXPECT_EQ(R"({"ipv4":"127.0.0.1","port":3306,"serviceName":"my_service",)" + R"("ipv6":"2001:0db8:85a3:0000:0000:8a2e:0370:7334"})", ep.toJson()); } @@ -58,7 +58,7 @@ TEST(ZipkinCoreTypesEndpointTest, copyOperator) { EXPECT_EQ(3306, ep1.port()); EXPECT_EQ("my_service", ep1.serviceName()); EXPECT_FALSE(ep1.isSetIpv6()); - EXPECT_EQ("{\"ipv4\":\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\"}", ep1.toJson()); + EXPECT_EQ(R"({"ipv4":"127.0.0.1","port":3306,"serviceName":"my_service"})", ep1.toJson()); EXPECT_EQ(ep1.ipv4(), ep2.ipv4()); EXPECT_EQ(ep1.port(), ep2.port()); @@ -75,7 +75,7 @@ TEST(ZipkinCoreTypesEndpointTest, assignmentOperator) { EXPECT_EQ(3306, ep1.port()); EXPECT_EQ("my_service", ep1.serviceName()); EXPECT_FALSE(ep1.isSetIpv6()); - EXPECT_EQ("{\"ipv4\":\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\"}", ep1.toJson()); + EXPECT_EQ(R"({"ipv4":"127.0.0.1","port":3306,"serviceName":"my_service"})", ep1.toJson()); EXPECT_EQ(ep1.ipv4(), ep2.ipv4()); EXPECT_EQ(ep1.port(), ep2.port()); @@ -98,8 +98,8 @@ TEST(ZipkinCoreTypesAnnotationTest, defaultConstructor) { ann.setValue(ZipkinCoreConstants::CLIENT_SEND); EXPECT_EQ(ZipkinCoreConstants::CLIENT_SEND, ann.value()); - std::string expected_json = "{\"timestamp\":" + std::to_string(timestamp) + ",\"value\":\"" + - ZipkinCoreConstants::CLIENT_SEND + "\"}"; + std::string expected_json = R"({"timestamp":)" + std::to_string(timestamp) + R"(,"value":")" + + ZipkinCoreConstants::CLIENT_SEND + R"("})"; EXPECT_EQ(expected_json, ann.toJson()); // Test the copy-semantics flavor of setEndpoint @@ -111,13 +111,13 @@ TEST(ZipkinCoreTypesAnnotationTest, defaultConstructor) { EXPECT_EQ(3306, ann.endpoint().port()); EXPECT_EQ("my_service", ann.endpoint().serviceName()); EXPECT_FALSE(ann.endpoint().isSetIpv6()); - EXPECT_EQ("{\"ipv4\":\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\"}", + EXPECT_EQ(R"({"ipv4":"127.0.0.1","port":3306,"serviceName":"my_service"})", (const_cast(ann.endpoint())).toJson()); - expected_json = "{\"timestamp\":" + std::to_string(timestamp) + ",\"value\":\"" + + expected_json = R"({"timestamp":)" + std::to_string(timestamp) + R"(,"value":")" + ZipkinCoreConstants::CLIENT_SEND + - "\",\"endpoint\":{\"ipv4\":" - "\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\"}}"; + R"(","endpoint":{"ipv4":)" + R"("127.0.0.1","port":3306,"serviceName":"my_service"}})"; EXPECT_EQ(expected_json, ann.toJson()); // Test the move-semantics flavor of setEndpoint @@ -129,13 +129,13 @@ TEST(ZipkinCoreTypesAnnotationTest, defaultConstructor) { EXPECT_EQ(5555, ann.endpoint().port()); EXPECT_EQ("my_service_2", ann.endpoint().serviceName()); EXPECT_FALSE(ann.endpoint().isSetIpv6()); - EXPECT_EQ("{\"ipv4\":\"192.168.1.1\",\"port\":5555,\"serviceName\":\"my_service_2\"}", + EXPECT_EQ(R"({"ipv4":"192.168.1.1","port":5555,"serviceName":"my_service_2"})", (const_cast(ann.endpoint())).toJson()); - expected_json = "{\"timestamp\":" + std::to_string(timestamp) + ",\"value\":\"" + + expected_json = R"({"timestamp":)" + std::to_string(timestamp) + R"(,"value":")" + ZipkinCoreConstants::CLIENT_SEND + - "\",\"endpoint\":{\"ipv4\":" - "\"192.168.1.1\",\"port\":5555,\"serviceName\":\"my_service_2\"}}"; + R"(","endpoint":{"ipv4":"192.168.1.1",)" + R"("port":5555,"serviceName":"my_service_2"}})"; EXPECT_EQ(expected_json, ann.toJson()); } @@ -152,13 +152,13 @@ TEST(ZipkinCoreTypesAnnotationTest, customConstructor) { EXPECT_EQ(3306, ann.endpoint().port()); EXPECT_EQ("my_service", ann.endpoint().serviceName()); EXPECT_FALSE(ann.endpoint().isSetIpv6()); - EXPECT_EQ("{\"ipv4\":\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\"}", + EXPECT_EQ(R"({"ipv4":"127.0.0.1","port":3306,"serviceName":"my_service"})", (const_cast(ann.endpoint())).toJson()); - std::string expected_json = "{\"timestamp\":" + std::to_string(timestamp) + ",\"value\":\"" + + std::string expected_json = R"({"timestamp":)" + std::to_string(timestamp) + R"(,"value":")" + ZipkinCoreConstants::CLIENT_SEND + - "\",\"endpoint\":{\"ipv4\":" - "\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\"}}"; + R"(","endpoint":{"ipv4":"127.0.0.1",)" + R"("port":3306,"serviceName":"my_service"}})"; EXPECT_EQ(expected_json, ann.toJson()); } @@ -208,7 +208,7 @@ TEST(ZipkinCoreTypesBinaryAnnotationTest, defaultConstructor) { ann.setValue("value"); EXPECT_EQ("value", ann.value()); - std::string expected_json = "{\"key\":\"key\",\"value\":\"value\"}"; + std::string expected_json = R"({"key":"key","value":"value"})"; EXPECT_EQ(expected_json, ann.toJson()); // Test the copy-semantics flavor of setEndpoint @@ -220,11 +220,14 @@ TEST(ZipkinCoreTypesBinaryAnnotationTest, defaultConstructor) { EXPECT_EQ(3306, ann.endpoint().port()); EXPECT_EQ("my_service", ann.endpoint().serviceName()); EXPECT_FALSE(ann.endpoint().isSetIpv6()); - EXPECT_EQ("{\"ipv4\":\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\"}", + EXPECT_EQ(R"({"ipv4":"127.0.0.1","port":3306,"serviceName":"my_service"})", (const_cast(ann.endpoint())).toJson()); - expected_json = "{\"key\":\"key\",\"value\":\"value\",\"endpoint\":{\"ipv4\":" - "\"127.0.0.1\",\"port\":3306,\"serviceName\":\"my_service\"}}"; + expected_json = "{" + R"("key":"key","value":"value",)" + R"("endpoint":)" + R"({"ipv4":"127.0.0.1","port":3306,"serviceName":"my_service"})" + "}"; EXPECT_EQ(expected_json, ann.toJson()); // Test the move-semantics flavor of setEndpoint @@ -236,11 +239,13 @@ TEST(ZipkinCoreTypesBinaryAnnotationTest, defaultConstructor) { EXPECT_EQ(5555, ann.endpoint().port()); EXPECT_EQ("my_service_2", ann.endpoint().serviceName()); EXPECT_FALSE(ann.endpoint().isSetIpv6()); - EXPECT_EQ("{\"ipv4\":\"192.168.1.1\",\"port\":5555,\"serviceName\":\"my_service_2\"}", + EXPECT_EQ(R"({"ipv4":"192.168.1.1","port":5555,"serviceName":"my_service_2"})", (const_cast(ann.endpoint())).toJson()); - - expected_json = "{\"key\":\"key\",\"value\":\"value\",\"endpoint\":{\"ipv4\":" - "\"192.168.1.1\",\"port\":5555,\"serviceName\":\"my_service_2\"}}"; + expected_json = "{" + R"("key":"key","value":"value",)" + R"("endpoint":)" + R"({"ipv4":"192.168.1.1","port":5555,"serviceName":"my_service_2"})" + "}"; EXPECT_EQ(expected_json, ann.toJson()); } @@ -251,7 +256,7 @@ TEST(ZipkinCoreTypesBinaryAnnotationTest, customConstructor) { EXPECT_EQ("value", ann.value()); EXPECT_FALSE(ann.isSetEndpoint()); EXPECT_EQ(AnnotationType::STRING, ann.annotationType()); - std::string expected_json = "{\"key\":\"key\",\"value\":\"value\"}"; + std::string expected_json = R"({"key":"key","value":"value"})"; EXPECT_EQ(expected_json, ann.toJson()); } @@ -298,8 +303,8 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { EXPECT_FALSE(span.isSet().parent_id_); EXPECT_FALSE(span.isSet().timestamp_); EXPECT_FALSE(span.isSet().trace_id_high_); - EXPECT_EQ("{\"traceId\":\"0000000000000000\",\"name\":\"\",\"id\":\"0000000000000000\"," - "\"annotations\":[],\"binaryAnnotations\":[]}", + EXPECT_EQ(R"({"traceId":"0000000000000000","name":"","id":"0000000000000000",)" + R"("annotations":[],"binaryAnnotations":[]})", span.toJson()); uint64_t id = Util::generateRandom64(); @@ -373,16 +378,16 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { EXPECT_EQ(1ULL, span.binaryAnnotations().size()); EXPECT_EQ( - "{\"traceId\":\"" + span.traceIdAsHexString() + "\",\"name\":\"span_name\",\"id\":\"" + - span.idAsHexString() + "\",\"parentId\":\"" + span.parentIdAsHexString() + - "\",\"timestamp\":" + std::to_string(span.timestamp()) + ",\"duration\":3000," - "\"annotations\":[" - "{\"timestamp\":" + + R"({"traceId":")" + span.traceIdAsHexString() + R"(","name":"span_name","id":")" + + span.idAsHexString() + R"(","parentId":")" + span.parentIdAsHexString() + + R"(","timestamp":)" + std::to_string(span.timestamp()) + R"(,"duration":3000,)" + R"("annotations":[)" + R"({"timestamp":)" + std::to_string(span.timestamp()) + - ",\"value\":\"cs\",\"endpoint\":" - "{\"ipv4\":\"192.168.1.2\",\"port\":3306,\"serviceName\":\"my_service_name\"}}]," - "\"binaryAnnotations\":[{\"key\":\"lc\",\"value\":\"my_component_name\",\"endpoint\":" - "{\"ipv4\":\"192.168.1.2\",\"port\":3306,\"serviceName\":\"my_service_name\"}}]}", + R"(,"value":"cs","endpoint":)" + R"({"ipv4":"192.168.1.2","port":3306,"serviceName":"my_service_name"}}],)" + R"("binaryAnnotations":[{"key":"lc","value":"my_component_name","endpoint":)" + R"({"ipv4":"192.168.1.2","port":3306,"serviceName":"my_service_name"}}]})", span.toJson()); // Test the copy-semantics flavor of addAnnotation and addBinaryAnnotation @@ -407,32 +412,31 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { EXPECT_EQ(3ULL, span.annotations().size()); EXPECT_EQ(3ULL, span.binaryAnnotations().size()); - EXPECT_EQ("{\"traceId\":\"" + span.traceIdAsHexString() + "\",\"name\":\"span_name\",\"id\":\"" + - span.idAsHexString() + "\",\"parentId\":\"" + span.parentIdAsHexString() + - "\",\"timestamp\":" + std::to_string(span.timestamp()) + ",\"duration\":3000," - "\"annotations\":[" - "{\"timestamp\":" + + EXPECT_EQ(R"({"traceId":")" + span.traceIdAsHexString() + R"(","name":"span_name","id":")" + + span.idAsHexString() + R"(","parentId":")" + span.parentIdAsHexString() + + R"(","timestamp":)" + std::to_string(span.timestamp()) + R"(,"duration":3000,)" + R"("annotations":[)" + R"({"timestamp":)" + std::to_string(timestamp) + - ",\"value\":\"cs\",\"endpoint\":" - "{\"ipv4\":\"192.168.1.2\",\"port\":3306,\"serviceName\":\"my_service_name\"}}," - "{\"timestamp\":" + - std::to_string(timestamp) + ",\"value\":\"ss\"," - "\"endpoint\":{\"ipv4\":\"192.168.1.2\",\"port\":3306," - "\"serviceName\":\"my_service_name\"}}," - "{\"timestamp\":" + + R"(,"value":"cs","endpoint":)" + R"({"ipv4":"192.168.1.2","port":3306,"serviceName":"my_service_name"}},)" + R"({"timestamp":)" + + std::to_string(timestamp) + R"(,"value":"ss",)" + R"("endpoint":{"ipv4":"192.168.1.2","port":3306,)" + R"("serviceName":"my_service_name"}},)" + R"({"timestamp":)" + std::to_string(timestamp) + - ",\"value\":\"sr\"," - "\"endpoint\":{\"ipv4\":\"192.168.1.2\",\"port\":3306,\"serviceName\":\"my_service_" - "name\"}}]," - "\"binaryAnnotations\":[{\"key\":\"lc\",\"value\":\"my_component_name\"," - "\"endpoint\":{\"ipv4\":\"192.168.1.2\",\"port\":3306,\"serviceName\":\"my_service_" - "name\"}}," - "{\"key\":\"http.return_code\",\"value\":\"200\"," - "\"endpoint\":{\"ipv4\":\"192.168.1.2\",\"port\":3306,\"serviceName\":\"my_service_" - "name\"}}," - "{\"key\":\"http.return_code\",\"value\":\"400\"," - "\"endpoint\":{\"ipv4\":\"192.168.1.2\",\"port\":3306,\"serviceName\":\"my_service_" - "name\"}}]}", + R"(,"value":"sr","endpoint":{"ipv4":"192.168.1.2","port":3306,)" + R"("serviceName":"my_service_name"}}],)" + R"("binaryAnnotations":[{"key":"lc","value":"my_component_name",)" + R"("endpoint":{"ipv4":"192.168.1.2","port":3306,)" + R"("serviceName":"my_service_name"}},)" + R"({"key":"http.return_code","value":"200",)" + R"("endpoint":{"ipv4":"192.168.1.2","port":3306,)" + R"("serviceName":"my_service_name"}},)" + R"({"key":"http.return_code","value":"400",)" + R"("endpoint":{"ipv4":"192.168.1.2","port":3306,)" + R"("serviceName":"my_service_name"}}]})", span.toJson()); } From 7ace62b7cac832b4f1b0699212b9b9a436a82c8c Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Mon, 17 Apr 2017 11:14:57 -0400 Subject: [PATCH 36/55] Support setting a custom random-number generator (#430) --- source/common/tracing/zipkin/tracer.cc | 18 +++++++++---- source/common/tracing/zipkin/tracer.h | 20 +++++++++++++- test/common/tracing/zipkin/tracer_test.cc | 33 +++++++++++++++++++++++ 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/source/common/tracing/zipkin/tracer.cc b/source/common/tracing/zipkin/tracer.cc index a2fcec9fca908..01fa24d1884cc 100644 --- a/source/common/tracing/zipkin/tracer.cc +++ b/source/common/tracing/zipkin/tracer.cc @@ -22,9 +22,9 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time) { // Create an all-new span, with no parent id Span span; span.setName(span_name); - uint64_t randon_number = Util::generateRandom64(); - span.setId(randon_number); - span.setTraceId(randon_number); + uint64_t random_number = generateRandomNumber(); + span.setId(random_number); + span.setTraceId(random_number); span.setStartTime(start_time); // Set the timestamp globally for the span and also for the CS annotation @@ -55,8 +55,8 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time, // We need to create a new span that is a child of the previous span; no shared context // Create a new span id - uint64_t randon_number = Util::generateRandom64(); - span.setId(randon_number); + uint64_t random_number = generateRandomNumber(); + span.setId(random_number); span.setName(span_name); @@ -118,4 +118,12 @@ void Tracer::reportSpan(Span&& span) { void Tracer::setReporter(ReporterUniquePtr reporter) { reporter_ = ReporterSharedPtr(std::move(reporter)); } + +void Tracer::setRandomGenerator(RandomGeneratorSharedPtr random_generator) { + random_generator_ = random_generator; +} + +uint64_t Tracer::generateRandomNumber() { + return random_generator_ ? random_generator_->random() : Util::generateRandom64(); +} } // Zipkin diff --git a/source/common/tracing/zipkin/tracer.h b/source/common/tracing/zipkin/tracer.h index 3c93c411471b4..42e8b9ef1f50d 100644 --- a/source/common/tracing/zipkin/tracer.h +++ b/source/common/tracing/zipkin/tracer.h @@ -1,6 +1,7 @@ #pragma once #include "envoy/common/pure.h" +#include "envoy/runtime/runtime.h" #include "common/tracing/zipkin/span_context.h" #include "common/tracing/zipkin/tracer_interface.h" @@ -33,6 +34,8 @@ typedef std::shared_ptr ReporterSharedPtr; typedef std::unique_ptr ReporterUniquePtr; +typedef std::shared_ptr RandomGeneratorSharedPtr; + /** * This class implements the Zipkin tracer. It has methods to create the appropriate Zipkin span * type, i.e., root span, child span, or shared-context span. @@ -52,7 +55,7 @@ class Tracer : public TracerInterface { * in all annotations' endpoints of the spans created by the Tracer. */ Tracer(const std::string& service_name, const std::string& address) - : service_name_(service_name), address_(address) {} + : service_name_(service_name), address_(address), random_generator_(nullptr) {} virtual ~Tracer() {} @@ -88,10 +91,25 @@ class Tracer : public TracerInterface { */ void setReporter(ReporterUniquePtr reporter); + /** + * Provides a random-number generator to be used by the Tracer. + * If this method is not used, the Tracer will use a default random-number generator. + * + * @param random_generator Random-number generator to be used. + */ + void setRandomGenerator(RandomGeneratorSharedPtr random_generator); + private: + /** + * Uses the default random-number generator if one was not provided by the user + */ + uint64_t generateRandomNumber(); + std::string service_name_; std::string address_; ReporterSharedPtr reporter_; + + RandomGeneratorSharedPtr random_generator_; }; } // Zipkin diff --git a/test/common/tracing/zipkin/tracer_test.cc b/test/common/tracing/zipkin/tracer_test.cc index b56a512c77692..0e82b5afe2da3 100644 --- a/test/common/tracing/zipkin/tracer_test.cc +++ b/test/common/tracing/zipkin/tracer_test.cc @@ -1,3 +1,5 @@ +#include "common/runtime/runtime_impl.h" + #include "common/tracing/zipkin/tracer.h" #include "common/tracing/zipkin/util.h" #include "common/tracing/zipkin/zipkin_core_constants.h" @@ -6,6 +8,26 @@ namespace Zipkin { +class TestReporterImpl : public Reporter { +public: + TestReporterImpl(int value) : value_(value) {} + void reportSpan(Span&& span) { reported_spans_.push_back(span); } + int getValue() { return value_; } + std::vector& reportedSpans() { return reported_spans_; } + +private: + int value_; + std::vector reported_spans_; +}; + +TEST(ZipkinTracerTest, reporterSetting) { + Tracer tracer("my_service_name", "127.0.0.1:9000"); + ReporterUniquePtr reporter_ptr(new TestReporterImpl(135)); + tracer.setReporter(std::move(reporter_ptr)); + ReporterSharedPtr reporter_from_tracer = tracer.reporter(); + EXPECT_EQ(135, std::static_pointer_cast(reporter_from_tracer)->getValue()); +} + TEST(ZipkinTracerTest, spanCreation) { Tracer tracer("my_service_name", "127.0.0.1:9000"); int64_t timestamp = Util::timeSinceEpochMicro(); @@ -130,6 +152,7 @@ TEST(ZipkinTracerTest, spanCreation) { TEST(ZipkinTracerTest, finishSpan) { Tracer tracer("my_service_name", "127.0.0.1:9000"); + tracer.setRandomGenerator(RandomGeneratorSharedPtr(new Runtime::RandomGeneratorImpl())); int64_t timestamp = Util::timeSinceEpochMicro(); // ============== @@ -172,10 +195,20 @@ TEST(ZipkinTracerTest, finishSpan) { SpanContext context(span); Span server_side = tracer.startSpan("my_span", timestamp, context); + // Associate a reporter with the tracer + ReporterUniquePtr reporter_ptr(new TestReporterImpl(135)); + tracer.setReporter(std::move(reporter_ptr)); + // Finishing a server-side span with an SR annotation must add an SS annotation server_side.finish(); EXPECT_EQ(2ULL, server_side.annotations().size()); + // Test if the reporter's reportSpan method was actually called upon finishing the span + ReporterSharedPtr reporter_from_tracer = tracer.reporter(); + EXPECT_EQ( + 1ULL, + std::static_pointer_cast(reporter_from_tracer)->reportedSpans().size()); + // Check the SR annotation added at span-creation time ann = server_side.annotations()[0]; EXPECT_EQ(ZipkinCoreConstants::SERVER_RECV, ann.value()); From d5bcc2f98552b68f5fe2e134adf5429853dd0713 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Mon, 17 Apr 2017 11:15:54 -0400 Subject: [PATCH 37/55] Add new dependencies to BUILD file of zipkin library (#430) --- source/common/tracing/zipkin/BUILD | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/common/tracing/zipkin/BUILD b/source/common/tracing/zipkin/BUILD index da08b730888ec..7de298ff527f1 100644 --- a/source/common/tracing/zipkin/BUILD +++ b/source/common/tracing/zipkin/BUILD @@ -24,4 +24,8 @@ envoy_cc_library( "zipkin_json_field_names.h", ], external_deps = ["rapidjson"], + deps = [ + "//include/envoy/runtime:runtime_interface", + "//source/common/common:utility_lib", + ], ) From 8ad37c37e1e66db4c534e1e6dba4b18edbf4a5bf Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Mon, 17 Apr 2017 11:16:58 -0400 Subject: [PATCH 38/55] Improvements to zipkin library tests (#430) --- .../common/tracing/zipkin/zipkin_core_types.h | 2 +- .../tracing/zipkin/span_context_test.cc | 81 ++++++++++++++++++- .../tracing/zipkin/zipkin_core_types_test.cc | 2 +- 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/source/common/tracing/zipkin/zipkin_core_types.h b/source/common/tracing/zipkin/zipkin_core_types.h index c9880bc8c77c9..fae6cb29d64c1 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.h +++ b/source/common/tracing/zipkin/zipkin_core_types.h @@ -393,7 +393,7 @@ class Span : public ZipkinBase { /** * Sets the span's annotations all at once. */ - void setAannotations(const std::vector& val) { annotations_ = val; } + void setAnnotations(const std::vector& val) { annotations_ = val; } /** * Adds an annotation to the span (copy semantics). diff --git a/test/common/tracing/zipkin/span_context_test.cc b/test/common/tracing/zipkin/span_context_test.cc index cad52b468c7d9..f01b62a687959 100644 --- a/test/common/tracing/zipkin/span_context_test.cc +++ b/test/common/tracing/zipkin/span_context_test.cc @@ -50,7 +50,7 @@ TEST(ZipkinSpanContextTest, populateFromString) { EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cs", span_context.serializeToString()); - // Span context populated with trace id, id, parent id, and multiple annotations + // Span context populated with trace id, id, parent id, and two annotations span_context.populateFromString("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cs;cr"); EXPECT_EQ(2722130815203937912ULL, span_context.trace_id()); EXPECT_EQ("25c6f38dd0600e78", span_context.traceIdAsHexString()); @@ -65,6 +65,36 @@ TEST(ZipkinSpanContextTest, populateFromString) { EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cr;cs", span_context.serializeToString()); + // Span context populated with trace id, id, parent id, and three annotations + span_context.populateFromString("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cs;cr;ss"); + EXPECT_EQ(2722130815203937912ULL, span_context.trace_id()); + EXPECT_EQ("25c6f38dd0600e78", span_context.traceIdAsHexString()); + EXPECT_EQ(6228615153417491119ULL, span_context.id()); + EXPECT_EQ("56707c7b3e1092af", span_context.idAsHexString()); + EXPECT_EQ(14164264937399213340ULL, span_context.parent_id()); + EXPECT_EQ("c49193ea42335d1c", span_context.parentIdAsHexString()); + EXPECT_TRUE(span_context.isSetAnnotation().cr_); + EXPECT_TRUE(span_context.isSetAnnotation().cs_); + EXPECT_FALSE(span_context.isSetAnnotation().sr_); + EXPECT_TRUE(span_context.isSetAnnotation().ss_); + EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cr;cs;ss", + span_context.serializeToString()); + + // Span context populated with trace id, id, parent id, and four annotations + span_context.populateFromString("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cs;cr;ss;sr"); + EXPECT_EQ(2722130815203937912ULL, span_context.trace_id()); + EXPECT_EQ("25c6f38dd0600e78", span_context.traceIdAsHexString()); + EXPECT_EQ(6228615153417491119ULL, span_context.id()); + EXPECT_EQ("56707c7b3e1092af", span_context.idAsHexString()); + EXPECT_EQ(14164264937399213340ULL, span_context.parent_id()); + EXPECT_EQ("c49193ea42335d1c", span_context.parentIdAsHexString()); + EXPECT_TRUE(span_context.isSetAnnotation().cr_); + EXPECT_TRUE(span_context.isSetAnnotation().cs_); + EXPECT_TRUE(span_context.isSetAnnotation().sr_); + EXPECT_TRUE(span_context.isSetAnnotation().ss_); + EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cr;cs;sr;ss", + span_context.serializeToString()); + // Span context populated with invalid string: it gets reset to its non-initialized state span_context.populateFromString("invalid string"); EXPECT_EQ(0ULL, span_context.trace_id()); @@ -119,10 +149,10 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { EXPECT_FALSE(span.isSet().trace_id_high_); span.setTraceIdHigh(9922130815203937912ULL); EXPECT_TRUE(span.isSet().trace_id_high_); - SpanContext span_context_5(span); + SpanContext span_context_high_id(span); // We currently drop the high bits. So, we expect the same context as above EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c", - span_context_5.serializeToString()); + span_context_high_id.serializeToString()); // Span context populated with trace id, id, parent id, and one annotation Annotation ann; @@ -142,7 +172,7 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;sr", span_context_3.serializeToString()); - // Span context populated with trace id, id, parent id, and multiple annotations + // Span context populated with trace id, id, parent id, and two annotations ann.setValue(ZipkinCoreConstants::SERVER_SEND); span.addAnnotation(ann); SpanContext span_context_4(span); @@ -158,5 +188,48 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { EXPECT_TRUE(span_context_4.isSetAnnotation().ss_); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;sr;ss", span_context_4.serializeToString()); + + // Span context populated with trace id, id, parent id, and three annotations + ann.setValue(ZipkinCoreConstants::CLIENT_SEND); + span.addAnnotation(ann); + SpanContext span_context_5(span); + EXPECT_EQ(2722130815203937912ULL, span_context_5.trace_id()); + EXPECT_EQ("25c6f38dd0600e78", span_context_5.traceIdAsHexString()); + EXPECT_EQ(6228615153417491119ULL, span_context_5.id()); + EXPECT_EQ("56707c7b3e1092af", span_context_5.idAsHexString()); + EXPECT_EQ(14164264937399213340ULL, span_context_5.parent_id()); + EXPECT_EQ("c49193ea42335d1c", span_context_5.parentIdAsHexString()); + EXPECT_FALSE(span_context_5.isSetAnnotation().cr_); + EXPECT_TRUE(span_context_5.isSetAnnotation().cs_); + EXPECT_TRUE(span_context_5.isSetAnnotation().sr_); + EXPECT_TRUE(span_context_5.isSetAnnotation().ss_); + EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cs;sr;ss", + span_context_5.serializeToString()); + + // Span context populated with trace id, id, parent id, and four annotations + std::vector annotations; + annotations.push_back(ann); + ann.setValue(ZipkinCoreConstants::SERVER_SEND); + annotations.push_back(ann); + ann.setValue(ZipkinCoreConstants::SERVER_RECV); + annotations.push_back(ann); + ann.setValue(ZipkinCoreConstants::CLIENT_RECV); + annotations.push_back(ann); + ann.setValue(ZipkinCoreConstants::CLIENT_SEND); + annotations.push_back(ann); + span.setAnnotations(annotations); + SpanContext span_context_6(span); + EXPECT_EQ(2722130815203937912ULL, span_context_6.trace_id()); + EXPECT_EQ("25c6f38dd0600e78", span_context_6.traceIdAsHexString()); + EXPECT_EQ(6228615153417491119ULL, span_context_6.id()); + EXPECT_EQ("56707c7b3e1092af", span_context_6.idAsHexString()); + EXPECT_EQ(14164264937399213340ULL, span_context_6.parent_id()); + EXPECT_EQ("c49193ea42335d1c", span_context_6.parentIdAsHexString()); + EXPECT_TRUE(span_context_6.isSetAnnotation().cr_); + EXPECT_TRUE(span_context_6.isSetAnnotation().cs_); + EXPECT_TRUE(span_context_6.isSetAnnotation().sr_); + EXPECT_TRUE(span_context_6.isSetAnnotation().ss_); + EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cr;cs;sr;ss", + span_context_6.serializeToString()); } } // Zipkin diff --git a/test/common/tracing/zipkin/zipkin_core_types_test.cc b/test/common/tracing/zipkin/zipkin_core_types_test.cc index 46f7d288f3cf3..eba9b9bb46e95 100644 --- a/test/common/tracing/zipkin/zipkin_core_types_test.cc +++ b/test/common/tracing/zipkin/zipkin_core_types_test.cc @@ -366,7 +366,7 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { ann.setEndpoint(endpoint); annotations.push_back(ann); - span.setAannotations(annotations); + span.setAnnotations(annotations); EXPECT_EQ(1ULL, span.annotations().size()); bann.setKey(Zipkin::ZipkinCoreConstants::LOCAL_COMPONENT); From dc1e357f6eaa787739f6364d16aa6726ad6a97a2 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Tue, 25 Apr 2017 10:52:55 -0400 Subject: [PATCH 39/55] SpanBuffer consistently handles a unique_ptr to Span. Streamlined SpanBuffer. (#430) --- source/common/tracing/zipkin/span_buffer.cc | 16 +++++--------- source/common/tracing/zipkin/span_buffer.h | 22 ++++++------------- .../common/tracing/zipkin/zipkin_core_types.h | 2 ++ .../common/tracing/zipkin/span_buffer_test.cc | 12 +++++----- 4 files changed, 20 insertions(+), 32 deletions(-) diff --git a/source/common/tracing/zipkin/span_buffer.cc b/source/common/tracing/zipkin/span_buffer.cc index 1753d2fa308dd..05223c53f0d0f 100644 --- a/source/common/tracing/zipkin/span_buffer.cc +++ b/source/common/tracing/zipkin/span_buffer.cc @@ -2,29 +2,23 @@ namespace Zipkin { -void SpanBuffer::allocateBuffer(uint64_t size) { span_buffer_.resize(size); } - -bool SpanBuffer::addSpan(Span&& span) { - if (next_position_ == span_buffer_.size()) { +bool SpanBuffer::addSpan(SpanPtr&& span) { + if (span_buffer_.size() == span_buffer_.capacity()) { // Buffer full return false; } - span_buffer_[next_position_++] = std::make_shared(span); + span_buffer_.push_back(std::move(span)); return true; } -void SpanBuffer::clear() { - next_position_ = 0; - std::fill(span_buffer_.begin(), span_buffer_.end(), nullptr); -} - std::string SpanBuffer::toStringifiedJsonArray() { std::string stringified_json_array = "["; if (pendingSpans()) { stringified_json_array += span_buffer_[0]->toJson(); - for (uint64_t i = 1; i < next_position_; i++) { + 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(); } diff --git a/source/common/tracing/zipkin/span_buffer.h b/source/common/tracing/zipkin/span_buffer.h index 9f542a98d0b38..dd6249298bc4a 100644 --- a/source/common/tracing/zipkin/span_buffer.h +++ b/source/common/tracing/zipkin/span_buffer.h @@ -4,8 +4,6 @@ namespace Zipkin { -typedef std::shared_ptr SpanSharedPtr; - /** * This class implements a simple buffer to store Zipkin tracing spans * prior to flushing them. @@ -16,26 +14,21 @@ class SpanBuffer { * Constructor that creates an empty buffer. Space needs to be allocated by invoking * the method allocateBuffer(size). */ - SpanBuffer() : next_position_(0) {} + SpanBuffer() {} /** * Constructor that initializes a buffer with the given size. * * @param size The desired buffer size. */ - SpanBuffer(uint64_t size) : span_buffer_(size, nullptr), next_position_(0) {} - - /** - * Destructor. - */ - virtual ~SpanBuffer() {} + SpanBuffer(uint64_t size) { span_buffer_.reserve(size); } /** * Allocates space for an empty buffer or resizes a previously-allocated one. * * @param size The desired buffer size. */ - void allocateBuffer(uint64_t size); + void allocateBuffer(uint64_t size) { span_buffer_.reserve(size); } /** * Adds the given Zipkin span to the buffer. @@ -44,18 +37,18 @@ class SpanBuffer { * * @return true if the span was successfully added, or false if the buffer was full. */ - bool addSpan(Span&& span); + bool addSpan(SpanPtr&& span); /** * Empties the buffer. This method is supposed to be called when all buffered spans * have been sent to to the Zipkin service. */ - void clear(); + void clear() { span_buffer_.clear(); } /** * @return the number of spans currently buffered. */ - uint64_t pendingSpans() { return next_position_; } + uint64_t pendingSpans() { return span_buffer_.size(); } /** * @return the contents of the buffer as a stringified array of JSONs, where @@ -65,7 +58,6 @@ class SpanBuffer { private: // We use a pre-allocated vector to improve performance - std::vector span_buffer_; - uint64_t next_position_; + std::vector span_buffer_; }; } // Zipkin diff --git a/source/common/tracing/zipkin/zipkin_core_types.h b/source/common/tracing/zipkin/zipkin_core_types.h index fae6cb29d64c1..9ed2f218d4025 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.h +++ b/source/common/tracing/zipkin/zipkin_core_types.h @@ -340,6 +340,8 @@ typedef struct SpanIsSet { bool trace_id_high_ : 1; } SpanIsSet; +typedef std::unique_ptr SpanPtr; + /** * Represents a Zipkin span. This class is based on Zipkin's Thrift definition of a span. */ diff --git a/test/common/tracing/zipkin/span_buffer_test.cc b/test/common/tracing/zipkin/span_buffer_test.cc index 2dec48ea80614..755b7aece2f22 100644 --- a/test/common/tracing/zipkin/span_buffer_test.cc +++ b/test/common/tracing/zipkin/span_buffer_test.cc @@ -6,7 +6,7 @@ namespace Zipkin { TEST(ZipkinSpanBufferTest, defaultConstructorEndToEnd) { SpanBuffer buffer; - Span span; + SpanPtr span(new Span()); EXPECT_EQ(0ULL, buffer.pendingSpans()); EXPECT_EQ("[]", buffer.toStringifiedJsonArray()); @@ -30,8 +30,8 @@ TEST(ZipkinSpanBufferTest, defaultConstructorEndToEnd) { EXPECT_EQ(0ULL, buffer.pendingSpans()); EXPECT_EQ("[]", buffer.toStringifiedJsonArray()); - buffer.addSpan(std::move(span)); - buffer.addSpan(Span(span)); + buffer.addSpan(SpanPtr(new Span())); + buffer.addSpan(SpanPtr(new Span())); expected_json_array_string = "[" "{" R"("traceId":"0000000000000000",)" @@ -58,7 +58,7 @@ TEST(ZipkinSpanBufferTest, defaultConstructorEndToEnd) { TEST(ZipkinSpanBufferTest, sizeConstructorEndtoEnd) { SpanBuffer buffer(2); - Span span; + SpanPtr span(new Span()); EXPECT_EQ(0ULL, buffer.pendingSpans()); EXPECT_EQ("[]", buffer.toStringifiedJsonArray()); @@ -78,8 +78,8 @@ TEST(ZipkinSpanBufferTest, sizeConstructorEndtoEnd) { EXPECT_EQ(0ULL, buffer.pendingSpans()); EXPECT_EQ("[]", buffer.toStringifiedJsonArray()); - buffer.addSpan(std::move(span)); - buffer.addSpan(Span(span)); + buffer.addSpan(SpanPtr(new Span())); + buffer.addSpan(SpanPtr(new Span())); expected_json_array_string = "[" "{" R"("traceId":"0000000000000000",)" From c53be4658ac2950117ed46c6857e29df84f0a8c4 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Tue, 25 Apr 2017 11:48:45 -0400 Subject: [PATCH 40/55] Safer handling of static string variable. Cleaned up SpanContext. (#430) --- source/common/tracing/zipkin/span_context.cc | 8 ++++---- source/common/tracing/zipkin/span_context.h | 15 +++++---------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/source/common/tracing/zipkin/span_context.cc b/source/common/tracing/zipkin/span_context.cc index c263c668ae2d0..5e7018cfb901d 100644 --- a/source/common/tracing/zipkin/span_context.cc +++ b/source/common/tracing/zipkin/span_context.cc @@ -21,17 +21,17 @@ const std::string SpanContext::HEX_DIGIT_GROUP_REGEX_STR_ = "([0-9,a-z]{16})"; const std::string& SpanContext::SPAN_CONTEXT_REGEX_STR() { // ^([0-9,a-z]{16});([0-9,a-z]{16});([0-9,a-z]{16})((;(cs|sr|cr|ss))*)$ - static std::string span_context_regex_str = + static const std::string* span_context_regex_str = new std::string( "^" + HEX_DIGIT_GROUP_REGEX_STR_ + FIELD_SEPARATOR_ + HEX_DIGIT_GROUP_REGEX_STR_ + FIELD_SEPARATOR_ + HEX_DIGIT_GROUP_REGEX_STR_ + "((" + FIELD_SEPARATOR_ + "(" + ZipkinCoreConstants::CLIENT_SEND + "|" + ZipkinCoreConstants::SERVER_RECV + "|" + - ZipkinCoreConstants::CLIENT_RECV + "|" + ZipkinCoreConstants::SERVER_SEND + "))*)$"; + ZipkinCoreConstants::CLIENT_RECV + "|" + ZipkinCoreConstants::SERVER_SEND + "))*)$"); - return span_context_regex_str; + return *span_context_regex_str; } const std::regex& SpanContext::SPAN_CONTEXT_REGEX() { - static std::regex span_context_regex(SPAN_CONTEXT_REGEX_STR()); + static const std::regex span_context_regex(SPAN_CONTEXT_REGEX_STR()); return span_context_regex; } diff --git a/source/common/tracing/zipkin/span_context.h b/source/common/tracing/zipkin/span_context.h index 42d6c80c37ce4..2bbef01b20e62 100644 --- a/source/common/tracing/zipkin/span_context.h +++ b/source/common/tracing/zipkin/span_context.h @@ -17,13 +17,13 @@ namespace Zipkin { * SS: "Server Send" * SR: "Server Receive" */ -typedef struct AnnotationIsSet { - AnnotationIsSet() : cs_(false), cr_(false), ss_(false), sr_(false) {} +struct AnnotationSet { + AnnotationSet() : cs_(false), cr_(false), ss_(false), sr_(false) {} bool cs_ : 1; bool cr_ : 1; bool ss_ : 1; bool sr_ : 1; -} AnnotationIsSet; +}; /** * This class represents the context of a Zipkin span. It embodies the following @@ -43,11 +43,6 @@ class SpanContext { */ SpanContext(const Span& span); - /** - * Destructor - */ - virtual ~SpanContext() {} - /** * Serializes the SpanContext object as a string. This encoding of a SpanContext is used * as the contents of the x-ot-span-context HTTP header, and allows Envoy to track the @@ -110,7 +105,7 @@ class SpanContext { /** * @return a struct indicating which annotations are present in the span. */ - AnnotationIsSet isSetAnnotation() const { return annotation_values_; } + AnnotationSet isSetAnnotation() const { return annotation_values_; } private: static const std::string FIELD_SEPARATOR_; @@ -137,7 +132,7 @@ class SpanContext { uint64_t trace_id_; uint64_t id_; uint64_t parent_id_; - AnnotationIsSet annotation_values_; + AnnotationSet annotation_values_; bool is_initialized_; }; } // Zipkin From 759c6bce2d990fe59090cd13a6225e3500772f1e Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Tue, 25 Apr 2017 16:50:32 -0400 Subject: [PATCH 41/55] Zipkin Tracer must return unique_ptr to Span. Cleaned up Tracer. (#430) --- include/envoy/runtime/runtime.h | 2 + source/common/tracing/zipkin/tracer.cc | 63 +++++----- source/common/tracing/zipkin/tracer.h | 32 ++--- .../common/tracing/zipkin/tracer_interface.h | 3 - .../common/tracing/zipkin/zipkin_core_types.h | 9 +- test/common/tracing/zipkin/tracer_test.cc | 117 ++++++++---------- 6 files changed, 99 insertions(+), 127 deletions(-) diff --git a/include/envoy/runtime/runtime.h b/include/envoy/runtime/runtime.h index 2051897a604ee..0eec82f5830d3 100644 --- a/include/envoy/runtime/runtime.h +++ b/include/envoy/runtime/runtime.h @@ -27,6 +27,8 @@ class RandomGenerator { virtual std::string uuid() PURE; }; +typedef std::unique_ptr RandomGeneratorPtr; + /** * A snapshot of runtime data. */ diff --git a/source/common/tracing/zipkin/tracer.cc b/source/common/tracing/zipkin/tracer.cc index 01fa24d1884cc..1613cecef0969 100644 --- a/source/common/tracing/zipkin/tracer.cc +++ b/source/common/tracing/zipkin/tracer.cc @@ -4,7 +4,7 @@ namespace Zipkin { -Span Tracer::startSpan(const std::string& span_name, uint64_t start_time) { +SpanPtr Tracer::startSpan(const std::string& span_name, uint64_t start_time) { // Build the endpoint std::string ip; uint16_t port; @@ -20,30 +20,30 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time) { cs.setValue(ZipkinCoreConstants::CLIENT_SEND); // Create an all-new span, with no parent id - Span span; - span.setName(span_name); + SpanPtr span_ptr(new Span()); + span_ptr->setName(span_name); uint64_t random_number = generateRandomNumber(); - span.setId(random_number); - span.setTraceId(random_number); - span.setStartTime(start_time); + span_ptr->setId(random_number); + span_ptr->setTraceId(random_number); + span_ptr->setStartTime(start_time); // Set the timestamp globally for the span and also for the CS annotation uint64_t timestamp_micro; timestamp_micro = Util::timeSinceEpochMicro(); cs.setTimestamp(timestamp_micro); - span.setTimestamp(timestamp_micro); + span_ptr->setTimestamp(timestamp_micro); // Add CS annotation to the span - span.addAnnotation(std::move(cs)); + span_ptr->addAnnotation(std::move(cs)); - span.setTracer(this); + span_ptr->setTracer(this); - return span; + return span_ptr; } -Span Tracer::startSpan(const std::string& span_name, uint64_t start_time, - SpanContext& previous_context) { - Span span; +SpanPtr Tracer::startSpan(const std::string& span_name, uint64_t start_time, + SpanContext& previous_context) { + SpanPtr span_ptr(new Span()); Annotation annotation; uint64_t timestamp_micro; @@ -56,32 +56,32 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time, // Create a new span id uint64_t random_number = generateRandomNumber(); - span.setId(random_number); + span_ptr->setId(random_number); - span.setName(span_name); + span_ptr->setName(span_name); // Set the parent id to the id of the previous span - span.setParentId(previous_context.id()); + span_ptr->setParentId(previous_context.id()); // Set the CS annotation value annotation.setValue(ZipkinCoreConstants::CLIENT_SEND); // Set the timestamp globally for the span - span.setTimestamp(timestamp_micro); + span_ptr->setTimestamp(timestamp_micro); } else if ((previous_context.isSetAnnotation().cs_) && (!previous_context.isSetAnnotation().sr_)) { // We need to create a new span that will share context with the previous span // Initialize the shared context for the new span - span.setId(previous_context.id()); + span_ptr->setId(previous_context.id()); if (previous_context.parent_id()) { - span.setParentId(previous_context.parent_id()); + span_ptr->setParentId(previous_context.parent_id()); } // Set the SR annotation value annotation.setValue(ZipkinCoreConstants::SERVER_RECV); } else { - return span; // return an empty span + return span_ptr; // return an empty span } // Build the endpoint @@ -96,31 +96,28 @@ Span Tracer::startSpan(const std::string& span_name, uint64_t start_time, // Add the newly-created annotation to the span annotation.setEndpoint(std::move(ep)); annotation.setTimestamp(timestamp_micro); - span.addAnnotation(std::move(annotation)); + span_ptr->addAnnotation(std::move(annotation)); // Keep the same trace id - span.setTraceId(previous_context.trace_id()); + span_ptr->setTraceId(previous_context.trace_id()); - span.setStartTime(start_time); + span_ptr->setStartTime(start_time); - span.setTracer(this); + span_ptr->setTracer(this); - return span; + return span_ptr; } void Tracer::reportSpan(Span&& span) { - auto r = reporter(); - if (r) { - r->reportSpan(std::move(span)); + if (reporter_) { + reporter_->reportSpan(std::move(span)); } } -void Tracer::setReporter(ReporterUniquePtr reporter) { - reporter_ = ReporterSharedPtr(std::move(reporter)); -} +void Tracer::setReporter(ReporterPtr reporter) { reporter_ = std::move(reporter); } -void Tracer::setRandomGenerator(RandomGeneratorSharedPtr random_generator) { - random_generator_ = random_generator; +void Tracer::setRandomGenerator(Runtime::RandomGeneratorPtr random_generator) { + random_generator_ = std::move(random_generator); } uint64_t Tracer::generateRandomNumber() { diff --git a/source/common/tracing/zipkin/tracer.h b/source/common/tracing/zipkin/tracer.h index 42e8b9ef1f50d..84ccf664e3c07 100644 --- a/source/common/tracing/zipkin/tracer.h +++ b/source/common/tracing/zipkin/tracer.h @@ -30,11 +30,7 @@ class Reporter { virtual void reportSpan(Span&& span) PURE; }; -typedef std::shared_ptr ReporterSharedPtr; - -typedef std::unique_ptr ReporterUniquePtr; - -typedef std::shared_ptr RandomGeneratorSharedPtr; +typedef std::unique_ptr ReporterPtr; /** * This class implements the Zipkin tracer. It has methods to create the appropriate Zipkin span @@ -57,15 +53,13 @@ class Tracer : public TracerInterface { Tracer(const std::string& service_name, const std::string& address) : service_name_(service_name), address_(address), random_generator_(nullptr) {} - virtual ~Tracer() {} - /** * Creates a "root" Zipkin span. * * @param span_name Name of the new span. * @param start_time The time indicating the beginning of the span. */ - Span startSpan(const std::string& span_name, uint64_t start_time); + SpanPtr startSpan(const std::string& span_name, uint64_t start_time); /** * Depending on the given context, creates either a "child" or a "shared-context" Zipkin span. @@ -74,22 +68,18 @@ class Tracer : public TracerInterface { * @param start_time The time indicating the beginning of the span. * @param previous_context The context of the span preceding the one to be created. */ - Span startSpan(const std::string& span_name, uint64_t start_time, SpanContext& previous_context); + SpanPtr startSpan(const std::string& span_name, uint64_t start_time, + SpanContext& previous_context); /** * TracerInterface::reportSpan. */ void reportSpan(Span&& span) override; - /** - * @return the Reporter associated with the Tracer object. - */ - ReporterSharedPtr reporter() { return reporter_; } - /** * Associates a Reporter object with this Tracer. */ - void setReporter(ReporterUniquePtr reporter); + void setReporter(ReporterPtr reporter); /** * Provides a random-number generator to be used by the Tracer. @@ -97,7 +87,7 @@ class Tracer : public TracerInterface { * * @param random_generator Random-number generator to be used. */ - void setRandomGenerator(RandomGeneratorSharedPtr random_generator); + void setRandomGenerator(Runtime::RandomGeneratorPtr random_generator); private: /** @@ -105,11 +95,9 @@ class Tracer : public TracerInterface { */ uint64_t generateRandomNumber(); - std::string service_name_; - std::string address_; - - ReporterSharedPtr reporter_; - - RandomGeneratorSharedPtr random_generator_; + const std::string service_name_; + const std::string address_; + ReporterPtr reporter_; + Runtime::RandomGeneratorPtr random_generator_; }; } // Zipkin diff --git a/source/common/tracing/zipkin/tracer_interface.h b/source/common/tracing/zipkin/tracer_interface.h index 1c514362d6712..62fb779f91cd4 100644 --- a/source/common/tracing/zipkin/tracer_interface.h +++ b/source/common/tracing/zipkin/tracer_interface.h @@ -27,7 +27,4 @@ class TracerInterface { */ virtual void reportSpan(Span&& span) PURE; }; - -typedef TracerInterface* TracerRawPtr; - } // Zipkin diff --git a/source/common/tracing/zipkin/zipkin_core_types.h b/source/common/tracing/zipkin/zipkin_core_types.h index 9ed2f218d4025..c6abc4abe42f9 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.h +++ b/source/common/tracing/zipkin/zipkin_core_types.h @@ -543,12 +543,12 @@ class Span : public ZipkinBase { * * @param tracer Represents the Tracer object to be associated with the span. */ - void setTracer(TracerRawPtr tracer) { tracer_ = tracer; } + void setTracer(TracerInterface* tracer) { tracer_ = tracer; } /** * @return the Tracer object associated with the span. */ - TracerRawPtr tracer() const { return tracer_; } + TracerInterface* tracer() const { return tracer_; } /** * Marks a successful end of the span. This method will: @@ -578,11 +578,8 @@ class Span : public ZipkinBase { int64_t timestamp_; int64_t duration_; uint64_t trace_id_high_; - int64_t start_time_; - - TracerRawPtr tracer_; - + TracerInterface* tracer_; SpanIsSet isset_; }; } // Zipkin diff --git a/test/common/tracing/zipkin/tracer_test.cc b/test/common/tracing/zipkin/tracer_test.cc index 0e82b5afe2da3..ddcb8dbdd0350 100644 --- a/test/common/tracing/zipkin/tracer_test.cc +++ b/test/common/tracing/zipkin/tracer_test.cc @@ -20,14 +20,6 @@ class TestReporterImpl : public Reporter { std::vector reported_spans_; }; -TEST(ZipkinTracerTest, reporterSetting) { - Tracer tracer("my_service_name", "127.0.0.1:9000"); - ReporterUniquePtr reporter_ptr(new TestReporterImpl(135)); - tracer.setReporter(std::move(reporter_ptr)); - ReporterSharedPtr reporter_from_tracer = tracer.reporter(); - EXPECT_EQ(135, std::static_pointer_cast(reporter_from_tracer)->getValue()); -} - TEST(ZipkinTracerTest, spanCreation) { Tracer tracer("my_service_name", "127.0.0.1:9000"); int64_t timestamp = Util::timeSinceEpochMicro(); @@ -36,19 +28,19 @@ TEST(ZipkinTracerTest, spanCreation) { // Test the creation of a root span --> CS // ============== - Span root_span = tracer.startSpan("my_span", timestamp); + SpanPtr root_span = tracer.startSpan("my_span", timestamp); - EXPECT_EQ("my_span", root_span.name()); - EXPECT_EQ(timestamp, root_span.startTime()); + EXPECT_EQ("my_span", root_span->name()); + EXPECT_EQ(timestamp, root_span->startTime()); - EXPECT_NE(0ULL, root_span.traceId()); // trace id must be set - EXPECT_EQ(root_span.traceId(), root_span.id()); // span id and trace id must be the same - EXPECT_FALSE(root_span.isSet().parent_id_); // no parent set - EXPECT_NE(0LL, root_span.timestamp()); // span's timestamp must be set + EXPECT_NE(0ULL, root_span->traceId()); // trace id must be set + EXPECT_EQ(root_span->traceId(), root_span->id()); // span id and trace id must be the same + EXPECT_FALSE(root_span->isSet().parent_id_); // no parent set + EXPECT_NE(0LL, root_span->timestamp()); // span's timestamp must be set // A CS annotation must have been added - EXPECT_EQ(1ULL, root_span.annotations().size()); - Annotation ann = root_span.annotations()[0]; + EXPECT_EQ(1ULL, root_span->annotations().size()); + Annotation ann = root_span->annotations()[0]; EXPECT_EQ(ZipkinCoreConstants::CLIENT_SEND, ann.value()); EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); @@ -59,39 +51,40 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_FALSE(endpoint.isSetIpv6()); // The tracer must have been properly set - EXPECT_EQ(dynamic_cast(&tracer), root_span.tracer()); + EXPECT_EQ(dynamic_cast(&tracer), root_span->tracer()); // Duration is not set at span-creation time - EXPECT_FALSE(root_span.isSet().duration_); + EXPECT_FALSE(root_span->isSet().duration_); // ============== // Test the creation of a shared-context span --> SR // ============== - SpanContext root_span_context(root_span); - Span server_side_shared_context_span = tracer.startSpan("my_span", timestamp, root_span_context); + SpanContext root_span_context(*root_span); + SpanPtr server_side_shared_context_span = + tracer.startSpan("my_span", timestamp, root_span_context); - EXPECT_EQ(timestamp, server_side_shared_context_span.startTime()); + EXPECT_EQ(timestamp, server_side_shared_context_span->startTime()); // span name should NOT be set (it was set in the CS side) - EXPECT_EQ("", server_side_shared_context_span.name()); + EXPECT_EQ("", server_side_shared_context_span->name()); // trace id must be the same in the CS and SR sides - EXPECT_EQ(root_span.traceId(), server_side_shared_context_span.traceId()); + EXPECT_EQ(root_span->traceId(), server_side_shared_context_span->traceId()); // span id must be the same in the CS and SR sides - EXPECT_EQ(root_span.id(), server_side_shared_context_span.id()); + EXPECT_EQ(root_span->id(), server_side_shared_context_span->id()); // The parent should be the same as in the CS side (none in this case) - EXPECT_FALSE(server_side_shared_context_span.isSet().parent_id_); + EXPECT_FALSE(server_side_shared_context_span->isSet().parent_id_); // span timestamp should not be set (it was set in the CS side) - EXPECT_EQ(0LL, server_side_shared_context_span.timestamp()); - EXPECT_FALSE(server_side_shared_context_span.isSet().timestamp_); + EXPECT_EQ(0LL, server_side_shared_context_span->timestamp()); + EXPECT_FALSE(server_side_shared_context_span->isSet().timestamp_); // An SR annotation must have been added - EXPECT_EQ(1ULL, server_side_shared_context_span.annotations().size()); - ann = server_side_shared_context_span.annotations()[0]; + EXPECT_EQ(1ULL, server_side_shared_context_span->annotations().size()); + ann = server_side_shared_context_span->annotations()[0]; EXPECT_EQ(ZipkinCoreConstants::SERVER_RECV, ann.value()); EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); @@ -102,38 +95,38 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_FALSE(endpoint.isSetIpv6()); // The tracer must have been properly set - EXPECT_EQ(dynamic_cast(&tracer), server_side_shared_context_span.tracer()); + EXPECT_EQ(dynamic_cast(&tracer), server_side_shared_context_span->tracer()); // Duration is not set at span-creation time - EXPECT_FALSE(server_side_shared_context_span.isSet().duration_); + EXPECT_FALSE(server_side_shared_context_span->isSet().duration_); // ============== // Test the creation of a child span --> CS // ============== - SpanContext server_side_context(server_side_shared_context_span); - Span child_span = tracer.startSpan("my_child_span", timestamp, server_side_context); + SpanContext server_side_context(*server_side_shared_context_span); + SpanPtr child_span = tracer.startSpan("my_child_span", timestamp, server_side_context); - EXPECT_EQ("my_child_span", child_span.name()); - EXPECT_EQ(timestamp, child_span.startTime()); + EXPECT_EQ("my_child_span", child_span->name()); + EXPECT_EQ(timestamp, child_span->startTime()); // trace id must be retained - EXPECT_NE(0ULL, child_span.traceId()); - EXPECT_EQ(server_side_shared_context_span.traceId(), child_span.traceId()); + EXPECT_NE(0ULL, child_span->traceId()); + EXPECT_EQ(server_side_shared_context_span->traceId(), child_span->traceId()); // span id and trace id must NOT be the same - EXPECT_NE(child_span.traceId(), child_span.id()); + EXPECT_NE(child_span->traceId(), child_span->id()); // parent should be the previous span - EXPECT_TRUE(child_span.isSet().parent_id_); - EXPECT_EQ(server_side_shared_context_span.id(), child_span.parentId()); + EXPECT_TRUE(child_span->isSet().parent_id_); + EXPECT_EQ(server_side_shared_context_span->id(), child_span->parentId()); // span's timestamp must be set - EXPECT_NE(0LL, child_span.timestamp()); + EXPECT_NE(0LL, child_span->timestamp()); // A CS annotation must have been added - EXPECT_EQ(1ULL, child_span.annotations().size()); - ann = child_span.annotations()[0]; + EXPECT_EQ(1ULL, child_span->annotations().size()); + ann = child_span->annotations()[0]; EXPECT_EQ(ZipkinCoreConstants::CLIENT_SEND, ann.value()); EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); @@ -144,15 +137,15 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_FALSE(endpoint.isSetIpv6()); // The tracer must have been properly set - EXPECT_EQ(dynamic_cast(&tracer), child_span.tracer()); + EXPECT_EQ(dynamic_cast(&tracer), child_span->tracer()); // Duration is not set at span-creation time - EXPECT_FALSE(child_span.isSet().duration_); + EXPECT_FALSE(child_span->isSet().duration_); } TEST(ZipkinTracerTest, finishSpan) { Tracer tracer("my_service_name", "127.0.0.1:9000"); - tracer.setRandomGenerator(RandomGeneratorSharedPtr(new Runtime::RandomGeneratorImpl())); + tracer.setRandomGenerator(Runtime::RandomGeneratorPtr(new Runtime::RandomGeneratorImpl())); int64_t timestamp = Util::timeSinceEpochMicro(); // ============== @@ -160,14 +153,14 @@ TEST(ZipkinTracerTest, finishSpan) { // ============== // Creates a root-span with a CS annotation - Span span = tracer.startSpan("my_span", timestamp); + SpanPtr span = tracer.startSpan("my_span", timestamp); // Finishing a root span with a CS annotation must add a CR annotation - span.finish(); - EXPECT_EQ(2ULL, span.annotations().size()); + span->finish(); + EXPECT_EQ(2ULL, span->annotations().size()); // Check the CS annotation added at span-creation time - Annotation ann = span.annotations()[0]; + Annotation ann = span->annotations()[0]; EXPECT_EQ(ZipkinCoreConstants::CLIENT_SEND, ann.value()); EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); @@ -178,7 +171,7 @@ TEST(ZipkinTracerTest, finishSpan) { EXPECT_FALSE(endpoint.isSetIpv6()); // Check the CR annotation added when ending the span - ann = span.annotations()[1]; + ann = span->annotations()[1]; EXPECT_EQ(ZipkinCoreConstants::CLIENT_RECV, ann.value()); EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); @@ -192,25 +185,23 @@ TEST(ZipkinTracerTest, finishSpan) { // Test finishing a span containing an SR annotation // ============== - SpanContext context(span); - Span server_side = tracer.startSpan("my_span", timestamp, context); + SpanContext context(*span); + SpanPtr server_side = tracer.startSpan("my_span", timestamp, context); // Associate a reporter with the tracer - ReporterUniquePtr reporter_ptr(new TestReporterImpl(135)); + TestReporterImpl* reporter_object = new TestReporterImpl(135); + ReporterPtr reporter_ptr(reporter_object); tracer.setReporter(std::move(reporter_ptr)); // Finishing a server-side span with an SR annotation must add an SS annotation - server_side.finish(); - EXPECT_EQ(2ULL, server_side.annotations().size()); + server_side->finish(); + EXPECT_EQ(2ULL, server_side->annotations().size()); // Test if the reporter's reportSpan method was actually called upon finishing the span - ReporterSharedPtr reporter_from_tracer = tracer.reporter(); - EXPECT_EQ( - 1ULL, - std::static_pointer_cast(reporter_from_tracer)->reportedSpans().size()); + EXPECT_EQ(1ULL, reporter_object->reportedSpans().size()); // Check the SR annotation added at span-creation time - ann = server_side.annotations()[0]; + ann = server_side->annotations()[0]; EXPECT_EQ(ZipkinCoreConstants::SERVER_RECV, ann.value()); EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); @@ -221,7 +212,7 @@ TEST(ZipkinTracerTest, finishSpan) { EXPECT_FALSE(endpoint.isSetIpv6()); // Check the SS annotation added when ending the span - ann = server_side.annotations()[1]; + ann = server_side->annotations()[1]; EXPECT_EQ(ZipkinCoreConstants::SERVER_SEND, ann.value()); EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); From 6cb2ac910fd6b203fd522abfe4615b63a13b2881 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Thu, 27 Apr 2017 10:03:43 -0400 Subject: [PATCH 42/55] Using MonotonicTime to compute Zipkin span duration. Using SystemTime for timestamps. Fixed header file usage. (#430) --- source/common/tracing/zipkin/BUILD | 1 + source/common/tracing/zipkin/span_context.cc | 4 +-- source/common/tracing/zipkin/span_context.h | 2 ++ source/common/tracing/zipkin/tracer.cc | 28 +++++++++------ source/common/tracing/zipkin/tracer.h | 5 +-- source/common/tracing/zipkin/util.cc | 27 +++++++------- source/common/tracing/zipkin/util.h | 16 ++------- .../tracing/zipkin/zipkin_core_constants.h | 2 ++ .../tracing/zipkin/zipkin_core_types.cc | 26 +++++++++----- .../common/tracing/zipkin/zipkin_core_types.h | 19 +++++----- .../tracing/zipkin/zipkin_json_field_names.h | 2 ++ test/common/tracing/zipkin/tracer_test.cc | 28 +++++++++------ test/common/tracing/zipkin/util_test.cc | 2 -- .../tracing/zipkin/zipkin_core_types_test.cc | 35 +++++++++++++------ 14 files changed, 115 insertions(+), 82 deletions(-) diff --git a/source/common/tracing/zipkin/BUILD b/source/common/tracing/zipkin/BUILD index 7de298ff527f1..ad8fe6a933b4a 100644 --- a/source/common/tracing/zipkin/BUILD +++ b/source/common/tracing/zipkin/BUILD @@ -25,6 +25,7 @@ envoy_cc_library( ], external_deps = ["rapidjson"], deps = [ + "//include/envoy/common:time_interface", "//include/envoy/runtime:runtime_interface", "//source/common/common:utility_lib", ], diff --git a/source/common/tracing/zipkin/span_context.cc b/source/common/tracing/zipkin/span_context.cc index 5e7018cfb901d..72bac2e23d45e 100644 --- a/source/common/tracing/zipkin/span_context.cc +++ b/source/common/tracing/zipkin/span_context.cc @@ -1,7 +1,7 @@ -#include "common/common/utility.h" +#include "common/tracing/zipkin/span_context.h" +#include "common/common/utility.h" #include "common/tracing/zipkin/zipkin_core_constants.h" -#include "common/tracing/zipkin/span_context.h" namespace Zipkin { diff --git a/source/common/tracing/zipkin/span_context.h b/source/common/tracing/zipkin/span_context.h index 2bbef01b20e62..6137ec943c42a 100644 --- a/source/common/tracing/zipkin/span_context.h +++ b/source/common/tracing/zipkin/span_context.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "common/tracing/zipkin/util.h" #include "common/tracing/zipkin/zipkin_core_types.h" diff --git a/source/common/tracing/zipkin/tracer.cc b/source/common/tracing/zipkin/tracer.cc index 1613cecef0969..3655155af7c15 100644 --- a/source/common/tracing/zipkin/tracer.cc +++ b/source/common/tracing/zipkin/tracer.cc @@ -1,10 +1,14 @@ +#include "common/tracing/zipkin/tracer.h" + +#include + +#include "common/common/utility.h" #include "common/tracing/zipkin/util.h" #include "common/tracing/zipkin/zipkin_core_constants.h" -#include "common/tracing/zipkin/tracer.h" namespace Zipkin { -SpanPtr Tracer::startSpan(const std::string& span_name, uint64_t start_time) { +SpanPtr Tracer::startSpan(const std::string& span_name, MonotonicTime start_time) { // Build the endpoint std::string ip; uint16_t port; @@ -25,11 +29,14 @@ SpanPtr Tracer::startSpan(const std::string& span_name, uint64_t start_time) { uint64_t random_number = generateRandomNumber(); span_ptr->setId(random_number); span_ptr->setTraceId(random_number); - span_ptr->setStartTime(start_time); + int64_t start_time_micro = + std::chrono::duration_cast(start_time.time_since_epoch()).count(); + span_ptr->setStartTime(start_time_micro); // Set the timestamp globally for the span and also for the CS annotation - uint64_t timestamp_micro; - timestamp_micro = Util::timeSinceEpochMicro(); + uint64_t timestamp_micro = + std::chrono::duration_cast( + ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); cs.setTimestamp(timestamp_micro); span_ptr->setTimestamp(timestamp_micro); @@ -41,15 +48,14 @@ SpanPtr Tracer::startSpan(const std::string& span_name, uint64_t start_time) { return span_ptr; } -SpanPtr Tracer::startSpan(const std::string& span_name, uint64_t start_time, +SpanPtr Tracer::startSpan(const std::string& span_name, MonotonicTime start_time, SpanContext& previous_context) { SpanPtr span_ptr(new Span()); Annotation annotation; uint64_t timestamp_micro; - // TODO(fabolive) We currently ignore the start_time to set the span/annotation timestamps - // Is start_time really needed? - timestamp_micro = Util::timeSinceEpochMicro(); + timestamp_micro = std::chrono::duration_cast( + ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); if ((previous_context.isSetAnnotation().sr_) && (!previous_context.isSetAnnotation().cs_)) { // We need to create a new span that is a child of the previous span; no shared context @@ -101,7 +107,9 @@ SpanPtr Tracer::startSpan(const std::string& span_name, uint64_t start_time, // Keep the same trace id span_ptr->setTraceId(previous_context.trace_id()); - span_ptr->setStartTime(start_time); + int64_t start_time_micro = + std::chrono::duration_cast(start_time.time_since_epoch()).count(); + span_ptr->setStartTime(start_time_micro); span_ptr->setTracer(this); diff --git a/source/common/tracing/zipkin/tracer.h b/source/common/tracing/zipkin/tracer.h index 84ccf664e3c07..a46eba2ce55c0 100644 --- a/source/common/tracing/zipkin/tracer.h +++ b/source/common/tracing/zipkin/tracer.h @@ -1,6 +1,7 @@ #pragma once #include "envoy/common/pure.h" +#include "envoy/common/time.h" #include "envoy/runtime/runtime.h" #include "common/tracing/zipkin/span_context.h" @@ -59,7 +60,7 @@ class Tracer : public TracerInterface { * @param span_name Name of the new span. * @param start_time The time indicating the beginning of the span. */ - SpanPtr startSpan(const std::string& span_name, uint64_t start_time); + SpanPtr startSpan(const std::string& span_name, MonotonicTime start_time); /** * Depending on the given context, creates either a "child" or a "shared-context" Zipkin span. @@ -68,7 +69,7 @@ class Tracer : public TracerInterface { * @param start_time The time indicating the beginning of the span. * @param previous_context The context of the span preceding the one to be created. */ - SpanPtr startSpan(const std::string& span_name, uint64_t start_time, + SpanPtr startSpan(const std::string& span_name, MonotonicTime start_time, SpanContext& previous_context); /** diff --git a/source/common/tracing/zipkin/util.cc b/source/common/tracing/zipkin/util.cc index ff02df2a06b81..c3d1af1dbe42f 100644 --- a/source/common/tracing/zipkin/util.cc +++ b/source/common/tracing/zipkin/util.cc @@ -1,9 +1,15 @@ -#include "rapidjson/writer.h" -#include "rapidjson/stringbuffer.h" -#include "rapidjson/document.h" +#include "common/tracing/zipkin/util.h" + +#include +#include +#include #include "common/common/hex.h" -#include "common/tracing/zipkin/util.h" +#include "common/common/utility.h" + +#include "rapidjson/document.h" +#include "rapidjson/stringbuffer.h" +#include "rapidjson/writer.h" namespace Zipkin { @@ -38,18 +44,9 @@ void Util::addArrayToJson(std::string& target, const std::vector( - std::chrono::system_clock::now().time_since_epoch()).count(); -} - -uint64_t Util::timeSinceEpochNano() { - return std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()).count(); -} - uint64_t Util::generateRandom64() { - uint64_t seed = timeSinceEpochNano(); + uint64_t seed = std::chrono::duration_cast( + ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); std::mt19937_64 rand_64(seed); return rand_64(); } diff --git a/source/common/tracing/zipkin/util.h b/source/common/tracing/zipkin/util.h index 6a1f5af170285..7a7b5b80e866d 100644 --- a/source/common/tracing/zipkin/util.h +++ b/source/common/tracing/zipkin/util.h @@ -1,5 +1,8 @@ #pragma once +#include +#include + namespace Zipkin { /** @@ -31,19 +34,6 @@ class Util { */ static void addArrayToJson(std::string& target, const std::vector& json_array, const std::string& field_name); - // ==== - // Time manipulation - // ==== - - /** - * Returns the time since epoch in microseconds. - */ - static uint64_t timeSinceEpochMicro(); - - /** - * Returns the time since epoch in nanoseconds. - */ - static uint64_t timeSinceEpochNano(); // ==== // Miscellaneous diff --git a/source/common/tracing/zipkin/zipkin_core_constants.h b/source/common/tracing/zipkin/zipkin_core_constants.h index 4b93deecbaa42..9021aa286c0f4 100644 --- a/source/common/tracing/zipkin/zipkin_core_constants.h +++ b/source/common/tracing/zipkin/zipkin_core_constants.h @@ -1,5 +1,7 @@ #pragma once +#include + namespace Zipkin { class ZipkinCoreConstants { diff --git a/source/common/tracing/zipkin/zipkin_core_types.cc b/source/common/tracing/zipkin/zipkin_core_types.cc index dcc63d0b5a27a..bd8d0a3f19d4a 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.cc +++ b/source/common/tracing/zipkin/zipkin_core_types.cc @@ -1,11 +1,13 @@ -#include "rapidjson/writer.h" -#include "rapidjson/stringbuffer.h" +#include "common/tracing/zipkin/zipkin_core_types.h" +#include "common/common/utility.h" #include "common/tracing/zipkin/span_context.h" #include "common/tracing/zipkin/util.h" #include "common/tracing/zipkin/zipkin_core_constants.h" #include "common/tracing/zipkin/zipkin_json_field_names.h" -#include "common/tracing/zipkin/zipkin_core_types.h" + +#include "rapidjson/stringbuffer.h" +#include "rapidjson/writer.h" namespace Zipkin { @@ -132,7 +134,7 @@ Span::Span(const Span& span) { duration_ = span.duration(); trace_id_high_ = span.traceIdHigh(); isset_ = span.isSet(); - start_time_ = span.startTime(); + monotonic_start_time_ = span.startTime(); tracer_ = span.tracer(); } @@ -147,7 +149,7 @@ Span& Span::operator=(const Span& span) { duration_ = span.duration(); trace_id_high_ = span.traceIdHigh(); isset_ = span.isSet(); - start_time_ = span.startTime(); + monotonic_start_time_ = span.startTime(); tracer_ = span.tracer(); return *this; @@ -208,18 +210,24 @@ void Span::finish() { // Need to set the SS annotation Annotation ss; ss.setEndpoint(annotations_[0].endpoint()); - ss.setTimestamp(Util::timeSinceEpochMicro()); + ss.setTimestamp(std::chrono::duration_cast( + ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count()); ss.setValue(ZipkinCoreConstants::SERVER_SEND); annotations_.push_back(std::move(ss)); } else if ((context.isSetAnnotation().cs_) && (!context.isSetAnnotation().cr_)) { // Need to set the CR annotation Annotation cr; - uint64_t stop_time = Util::timeSinceEpochMicro(); + uint64_t stop_timestamp = + std::chrono::duration_cast( + ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); cr.setEndpoint(annotations_[0].endpoint()); - cr.setTimestamp(stop_time); + cr.setTimestamp(stop_timestamp); cr.setValue(ZipkinCoreConstants::CLIENT_RECV); annotations_.push_back(std::move(cr)); - setDuration(stop_time - timestamp_); + int64_t monotonic_stop_time = + std::chrono::duration_cast( + ProdMonotonicTimeSource::instance_.currentTime().time_since_epoch()).count(); + setDuration(monotonic_stop_time - monotonic_start_time_); } auto t = tracer(); diff --git a/source/common/tracing/zipkin/zipkin_core_types.h b/source/common/tracing/zipkin/zipkin_core_types.h index c6abc4abe42f9..13423bdcb54c8 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.h +++ b/source/common/tracing/zipkin/zipkin_core_types.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "envoy/common/pure.h" #include "common/tracing/zipkin/tracer_interface.h" @@ -177,7 +179,8 @@ class Annotation : public ZipkinBase { } /** - * @return the annotation's timestamp attribute. + * @return the annotation's timestamp attribute + * (clock time for user presentation: microseconds since epoch). */ uint64_t timestamp() const { return timestamp_; } @@ -362,7 +365,7 @@ class Span : public ZipkinBase { */ Span() : trace_id_(0), name_(), id_(0), parent_id_(0), timestamp_(0), duration_(0), - trace_id_high_(0), start_time_(0), tracer_(nullptr) {} + trace_id_high_(0), monotonic_start_time_(0), tracer_(nullptr) {} /** * Sets the span's trace id attribute. @@ -453,9 +456,9 @@ class Span : public ZipkinBase { } /** - * Sets the span start-time attribute. + * Sets the span start-time attribute (monotonic, used to calculate duration). */ - void setStartTime(const int64_t time) { start_time_ = time; } + void setStartTime(const int64_t time) { monotonic_start_time_ = time; } /** * @return the span's annotations. @@ -503,7 +506,7 @@ class Span : public ZipkinBase { std::string parentIdAsHexString() const { return Util::uint64ToHex(parent_id_); } /** - * @return the span's timestamp. + * @return the span's timestamp (clock time for user presentation: microseconds since epoch). */ int64_t timestamp() const { return timestamp_; } @@ -523,9 +526,9 @@ class Span : public ZipkinBase { uint64_t traceIdHigh() const { return trace_id_high_; } /** - * @return the span's start time. + * @return the span's start time (monotonic, used to calculate duration). */ - int64_t startTime() const { return start_time_; } + int64_t startTime() const { return monotonic_start_time_; } /** * Serializes the span as a Zipkin-compliant JSON representation as a string. @@ -578,7 +581,7 @@ class Span : public ZipkinBase { int64_t timestamp_; int64_t duration_; uint64_t trace_id_high_; - int64_t start_time_; + int64_t monotonic_start_time_; TracerInterface* tracer_; SpanIsSet isset_; }; diff --git a/source/common/tracing/zipkin/zipkin_json_field_names.h b/source/common/tracing/zipkin/zipkin_json_field_names.h index 7e7d6410c71ca..cf49d5d67bedb 100644 --- a/source/common/tracing/zipkin/zipkin_json_field_names.h +++ b/source/common/tracing/zipkin/zipkin_json_field_names.h @@ -1,5 +1,7 @@ #pragma once +#include + namespace Zipkin { class ZipkinJsonFieldNames { diff --git a/test/common/tracing/zipkin/tracer_test.cc b/test/common/tracing/zipkin/tracer_test.cc index ddcb8dbdd0350..e536c7179f313 100644 --- a/test/common/tracing/zipkin/tracer_test.cc +++ b/test/common/tracing/zipkin/tracer_test.cc @@ -1,5 +1,5 @@ +#include "common/common/utility.h" #include "common/runtime/runtime_impl.h" - #include "common/tracing/zipkin/tracer.h" #include "common/tracing/zipkin/util.h" #include "common/tracing/zipkin/zipkin_core_constants.h" @@ -22,16 +22,18 @@ class TestReporterImpl : public Reporter { TEST(ZipkinTracerTest, spanCreation) { Tracer tracer("my_service_name", "127.0.0.1:9000"); - int64_t timestamp = Util::timeSinceEpochMicro(); + MonotonicTime start_time = ProdMonotonicTimeSource::instance_.currentTime(); // ============== // Test the creation of a root span --> CS // ============== - SpanPtr root_span = tracer.startSpan("my_span", timestamp); + SpanPtr root_span = tracer.startSpan("my_span", start_time); EXPECT_EQ("my_span", root_span->name()); - EXPECT_EQ(timestamp, root_span->startTime()); + EXPECT_EQ( + std::chrono::duration_cast(start_time.time_since_epoch()).count(), + root_span->startTime()); EXPECT_NE(0ULL, root_span->traceId()); // trace id must be set EXPECT_EQ(root_span->traceId(), root_span->id()); // span id and trace id must be the same @@ -62,9 +64,11 @@ TEST(ZipkinTracerTest, spanCreation) { SpanContext root_span_context(*root_span); SpanPtr server_side_shared_context_span = - tracer.startSpan("my_span", timestamp, root_span_context); + tracer.startSpan("my_span", start_time, root_span_context); - EXPECT_EQ(timestamp, server_side_shared_context_span->startTime()); + EXPECT_EQ( + std::chrono::duration_cast(start_time.time_since_epoch()).count(), + server_side_shared_context_span->startTime()); // span name should NOT be set (it was set in the CS side) EXPECT_EQ("", server_side_shared_context_span->name()); @@ -105,10 +109,12 @@ TEST(ZipkinTracerTest, spanCreation) { // ============== SpanContext server_side_context(*server_side_shared_context_span); - SpanPtr child_span = tracer.startSpan("my_child_span", timestamp, server_side_context); + SpanPtr child_span = tracer.startSpan("my_child_span", start_time, server_side_context); EXPECT_EQ("my_child_span", child_span->name()); - EXPECT_EQ(timestamp, child_span->startTime()); + EXPECT_EQ( + std::chrono::duration_cast(start_time.time_since_epoch()).count(), + child_span->startTime()); // trace id must be retained EXPECT_NE(0ULL, child_span->traceId()); @@ -146,14 +152,14 @@ TEST(ZipkinTracerTest, spanCreation) { TEST(ZipkinTracerTest, finishSpan) { Tracer tracer("my_service_name", "127.0.0.1:9000"); tracer.setRandomGenerator(Runtime::RandomGeneratorPtr(new Runtime::RandomGeneratorImpl())); - int64_t timestamp = Util::timeSinceEpochMicro(); + MonotonicTime start_time = ProdMonotonicTimeSource::instance_.currentTime(); // ============== // Test finishing a span containing a CS annotation // ============== // Creates a root-span with a CS annotation - SpanPtr span = tracer.startSpan("my_span", timestamp); + SpanPtr span = tracer.startSpan("my_span", start_time); // Finishing a root span with a CS annotation must add a CR annotation span->finish(); @@ -186,7 +192,7 @@ TEST(ZipkinTracerTest, finishSpan) { // ============== SpanContext context(*span); - SpanPtr server_side = tracer.startSpan("my_span", timestamp, context); + SpanPtr server_side = tracer.startSpan("my_span", start_time, context); // Associate a reporter with the tracer TestReporterImpl* reporter_object = new TestReporterImpl(135); diff --git a/test/common/tracing/zipkin/util_test.cc b/test/common/tracing/zipkin/util_test.cc index 7c8c823676424..29eeded6bbe33 100644 --- a/test/common/tracing/zipkin/util_test.cc +++ b/test/common/tracing/zipkin/util_test.cc @@ -6,8 +6,6 @@ namespace Zipkin { TEST(ZipkinUtilTest, utilTests) { EXPECT_EQ(typeid(uint64_t).name(), typeid(Util::generateRandom64()).name()); - EXPECT_EQ(typeid(uint64_t).name(), typeid(Util::timeSinceEpochMicro()).name()); - EXPECT_EQ(typeid(uint64_t).name(), typeid(Util::timeSinceEpochNano()).name()); std::string base16_string = Util::uint64ToHex(2722130815203937912ULL); EXPECT_EQ(typeid(std::string).name(), typeid(base16_string).name()); diff --git a/test/common/tracing/zipkin/zipkin_core_types_test.cc b/test/common/tracing/zipkin/zipkin_core_types_test.cc index eba9b9bb46e95..1adcdd24699e2 100644 --- a/test/common/tracing/zipkin/zipkin_core_types_test.cc +++ b/test/common/tracing/zipkin/zipkin_core_types_test.cc @@ -1,5 +1,6 @@ -#include "common/tracing/zipkin/zipkin_core_types.h" +#include "common/common/utility.h" #include "common/tracing/zipkin/zipkin_core_constants.h" +#include "common/tracing/zipkin/zipkin_core_types.h" #include "gtest/gtest.h" @@ -91,7 +92,9 @@ TEST(ZipkinCoreTypesAnnotationTest, defaultConstructor) { EXPECT_EQ("", ann.value()); EXPECT_FALSE(ann.isSetEndpoint()); - uint64_t timestamp = Util::timeSinceEpochMicro(); + uint64_t timestamp = + std::chrono::duration_cast( + ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); ann.setTimestamp(timestamp); EXPECT_EQ(timestamp, ann.timestamp()); @@ -141,7 +144,9 @@ TEST(ZipkinCoreTypesAnnotationTest, defaultConstructor) { TEST(ZipkinCoreTypesAnnotationTest, customConstructor) { Endpoint ep(std::string("127.0.0.1"), 3306, std::string("my_service")); - uint64_t timestamp = Util::timeSinceEpochMicro(); + uint64_t timestamp = + std::chrono::duration_cast( + ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); Annotation ann(timestamp, ZipkinCoreConstants::CLIENT_SEND, ep); EXPECT_EQ(timestamp, ann.timestamp()); @@ -164,7 +169,9 @@ TEST(ZipkinCoreTypesAnnotationTest, customConstructor) { TEST(ZipkinCoreTypesAnnotationTest, copyConstructor) { Endpoint ep(std::string("127.0.0.1"), 3306, std::string("my_service")); - uint64_t timestamp = Util::timeSinceEpochMicro(); + uint64_t timestamp = + std::chrono::duration_cast( + ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); Annotation ann(timestamp, ZipkinCoreConstants::CLIENT_SEND, ep); Annotation ann2(ann); @@ -180,7 +187,9 @@ TEST(ZipkinCoreTypesAnnotationTest, copyConstructor) { TEST(ZipkinCoreTypesAnnotationTest, assignmentOperator) { Endpoint ep(std::string("127.0.0.1"), 3306, std::string("my_service")); - uint64_t timestamp = Util::timeSinceEpochMicro(); + uint64_t timestamp = + std::chrono::duration_cast( + ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); Annotation ann(timestamp, ZipkinCoreConstants::CLIENT_SEND, ep); Annotation ann2 = ann; @@ -332,13 +341,17 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { EXPECT_EQ(id, span.traceIdHigh()); EXPECT_TRUE(span.isSet().trace_id_high_); - int64_t timestamp = Util::timeSinceEpochMicro(); + int64_t timestamp = std::chrono::duration_cast( + ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); span.setTimestamp(timestamp); EXPECT_EQ(timestamp, span.timestamp()); EXPECT_TRUE(span.isSet().timestamp_); - span.setStartTime(timestamp); - EXPECT_EQ(timestamp, span.startTime()); + int64_t start_time = + std::chrono::duration_cast( + ProdMonotonicTimeSource::instance_.currentTime().time_since_epoch()).count(); + span.setStartTime(start_time); + EXPECT_EQ(start_time, span.startTime()); span.setDuration(3000LL); EXPECT_EQ(3000LL, span.duration()); @@ -448,7 +461,8 @@ TEST(ZipkinCoreTypesSpanTest, copyConstructor) { span.setId(id); span.setParentId(id); span.setTraceId(id); - int64_t timestamp = Util::timeSinceEpochMicro(); + int64_t timestamp = std::chrono::duration_cast( + ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); span.setTimestamp(timestamp); span.setDuration(3000LL); span.setName("span_name"); @@ -483,7 +497,8 @@ TEST(ZipkinCoreTypesSpanTest, assignmentOperator) { span.setId(id); span.setParentId(id); span.setTraceId(id); - int64_t timestamp = Util::timeSinceEpochMicro(); + int64_t timestamp = std::chrono::duration_cast( + ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); span.setTimestamp(timestamp); span.setDuration(3000LL); span.setName("span_name"); From e14d8611805313e68c74db679e8796923e83a979 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Thu, 27 Apr 2017 10:24:44 -0400 Subject: [PATCH 43/55] Moved uintToHex() from the Zipkin library to the Hex library. (#430) --- source/common/common/hex.cc | 15 +++++++++++++++ source/common/common/hex.h | 6 ++++++ source/common/tracing/zipkin/span_context.h | 6 +++--- source/common/tracing/zipkin/util.cc | 15 --------------- source/common/tracing/zipkin/util.h | 7 ------- source/common/tracing/zipkin/zipkin_core_types.cc | 6 +++--- source/common/tracing/zipkin/zipkin_core_types.h | 7 ++++--- test/common/common/hex_test.cc | 6 ++++++ test/common/tracing/zipkin/util_test.cc | 4 ---- .../tracing/zipkin/zipkin_core_types_test.cc | 12 ++++++------ 10 files changed, 43 insertions(+), 41 deletions(-) diff --git a/source/common/common/hex.cc b/source/common/common/hex.cc index 0cf510c56e43c..b907eddda960d 100644 --- a/source/common/common/hex.cc +++ b/source/common/common/hex.cc @@ -43,3 +43,18 @@ std::vector Hex::decode(const std::string& hex_string) { return segment; } + +std::string Hex::uint64ToHex(uint64_t value) { + std::vector data(8); + + data[7] = (value & 0x00000000000000FF); + data[6] = (value & 0x000000000000FF00) >> 8; + data[5] = (value & 0x0000000000FF0000) >> 16; + data[4] = (value & 0x00000000FF000000) >> 24; + data[3] = (value & 0x000000FF00000000) >> 32; + data[2] = (value & 0x0000FF0000000000) >> 40; + data[1] = (value & 0x00FF000000000000) >> 48; + data[0] = (value & 0xFF00000000000000) >> 56; + + return encode(&data[0], data.size()); +} diff --git a/source/common/common/hex.h b/source/common/common/hex.h index 84d90b02fb854..75d42ac92f99e 100644 --- a/source/common/common/hex.h +++ b/source/common/common/hex.h @@ -32,4 +32,10 @@ class Hex final { * @return binary data */ static std::vector decode(const std::string& input); + + /** + * Converts the given 64-bit integer into a hexadecimal string. + * @param value The integer to be converted. + */ + static std::string uint64ToHex(uint64_t value); }; diff --git a/source/common/tracing/zipkin/span_context.h b/source/common/tracing/zipkin/span_context.h index 6137ec943c42a..3e203b8128ebc 100644 --- a/source/common/tracing/zipkin/span_context.h +++ b/source/common/tracing/zipkin/span_context.h @@ -82,7 +82,7 @@ class SpanContext { /** * @return the span id as a 16-character hexadecimal string. */ - std::string idAsHexString() const { return Util::uint64ToHex(id_); } + std::string idAsHexString() const { return Hex::uint64ToHex(id_); } /** * @return the span's parent id as an integer. @@ -92,7 +92,7 @@ class SpanContext { /** * @return the parent id as a 16-character hexadecimal string. */ - std::string parentIdAsHexString() const { return Util::uint64ToHex(parent_id_); } + std::string parentIdAsHexString() const { return Hex::uint64ToHex(parent_id_); } /** * @return the trace id as an integer. @@ -102,7 +102,7 @@ class SpanContext { /** * @return the trace id as a 16-character hexadecimal string. */ - std::string traceIdAsHexString() const { return Util::uint64ToHex(trace_id_); } + std::string traceIdAsHexString() const { return Hex::uint64ToHex(trace_id_); } /** * @return a struct indicating which annotations are present in the span. diff --git a/source/common/tracing/zipkin/util.cc b/source/common/tracing/zipkin/util.cc index c3d1af1dbe42f..a9f67cdbbc6ab 100644 --- a/source/common/tracing/zipkin/util.cc +++ b/source/common/tracing/zipkin/util.cc @@ -51,21 +51,6 @@ uint64_t Util::generateRandom64() { return rand_64(); } -std::string Util::uint64ToHex(uint64_t value) { - std::vector data(8); - - data[7] = (value & 0x00000000000000FF); - data[6] = (value & 0x000000000000FF00) >> 8; - data[5] = (value & 0x0000000000FF0000) >> 16; - data[4] = (value & 0x00000000FF000000) >> 24; - data[3] = (value & 0x000000FF00000000) >> 32; - data[2] = (value & 0x0000FF0000000000) >> 40; - data[1] = (value & 0x00FF000000000000) >> 48; - data[0] = (value & 0xFF00000000000000) >> 56; - - return Hex::encode(&data[0], data.size()); -} - void Util::getIPAndPort(const std::string& address, std::string& ip, uint16_t& port) { std::regex re("^(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})(:(\\d+))?$"); std::smatch match; diff --git a/source/common/tracing/zipkin/util.h b/source/common/tracing/zipkin/util.h index 7a7b5b80e866d..9bbfbf1657fca 100644 --- a/source/common/tracing/zipkin/util.h +++ b/source/common/tracing/zipkin/util.h @@ -44,13 +44,6 @@ class Util { */ static uint64_t generateRandom64(); - /** - * Converts the given 64-bit integer into a hexadecimal string. - * - * @param value The integer to be converted. - */ - static std::string uint64ToHex(uint64_t value); - /** * Extracts the IP address and port from a string of the form ":". * diff --git a/source/common/tracing/zipkin/zipkin_core_types.cc b/source/common/tracing/zipkin/zipkin_core_types.cc index bd8d0a3f19d4a..700ead32c7c33 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.cc +++ b/source/common/tracing/zipkin/zipkin_core_types.cc @@ -160,15 +160,15 @@ const std::string& Span::toJson() { rapidjson::Writer writer(s); writer.StartObject(); writer.Key(ZipkinJsonFieldNames::SPAN_TRACE_ID.c_str()); - writer.String(Util::uint64ToHex(trace_id_).c_str()); + writer.String(Hex::uint64ToHex(trace_id_).c_str()); writer.Key(ZipkinJsonFieldNames::SPAN_NAME.c_str()); writer.String(name_.c_str()); writer.Key(ZipkinJsonFieldNames::SPAN_ID.c_str()); - writer.String(Util::uint64ToHex(id_).c_str()); + writer.String(Hex::uint64ToHex(id_).c_str()); if (isset_.parent_id_ && parent_id_) { writer.Key(ZipkinJsonFieldNames::SPAN_PARENT_ID.c_str()); - writer.String(Util::uint64ToHex(parent_id_).c_str()); + writer.String(Hex::uint64ToHex(parent_id_).c_str()); } if (isset_.timestamp_) { diff --git a/source/common/tracing/zipkin/zipkin_core_types.h b/source/common/tracing/zipkin/zipkin_core_types.h index 13423bdcb54c8..1bcfd907d5dc0 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.h +++ b/source/common/tracing/zipkin/zipkin_core_types.h @@ -4,6 +4,7 @@ #include "envoy/common/pure.h" +#include "common/common/hex.h" #include "common/tracing/zipkin/tracer_interface.h" #include "common/tracing/zipkin/util.h" @@ -483,7 +484,7 @@ class Span : public ZipkinBase { /** * @return the span's id as a hexadecimal string. */ - std::string idAsHexString() const { return Util::uint64ToHex(id_); } + std::string idAsHexString() const { return Hex::uint64ToHex(id_); } /** * @return a struct indicating which of the span's optional attributes are set. @@ -503,7 +504,7 @@ class Span : public ZipkinBase { /** * @return the span's parent id as a hexadecimal string. */ - std::string parentIdAsHexString() const { return Util::uint64ToHex(parent_id_); } + std::string parentIdAsHexString() const { return Hex::uint64ToHex(parent_id_); } /** * @return the span's timestamp (clock time for user presentation: microseconds since epoch). @@ -518,7 +519,7 @@ class Span : public ZipkinBase { /** * @return the span's trace id as a hexadecimal string. */ - std::string traceIdAsHexString() const { return Util::uint64ToHex(trace_id_); } + std::string traceIdAsHexString() const { return Hex::uint64ToHex(trace_id_); } /** * @return the higher 64 bits of a 128-bit trace id. diff --git a/test/common/common/hex_test.cc b/test/common/common/hex_test.cc index e3f190a4fe8ae..ee1717d96f5d8 100644 --- a/test/common/common/hex_test.cc +++ b/test/common/common/hex_test.cc @@ -27,3 +27,9 @@ TEST(Hex, RoundTrip) { TEST(Hex, BadHex) { EXPECT_THROW(Hex::decode("abcde"), EnvoyException); } TEST(Hex, DecodeUppercase) { Hex::decode("ABCDEFAB"); } + +TEST(Hex, UIntToHex) { + std::string base16_string = Hex::uint64ToHex(2722130815203937912ULL); + EXPECT_EQ("25c6f38dd0600e78", base16_string); + EXPECT_EQ("0000000000000000", Hex::uint64ToHex(0ULL)); +} diff --git a/test/common/tracing/zipkin/util_test.cc b/test/common/tracing/zipkin/util_test.cc index 29eeded6bbe33..ed664a201233a 100644 --- a/test/common/tracing/zipkin/util_test.cc +++ b/test/common/tracing/zipkin/util_test.cc @@ -7,10 +7,6 @@ namespace Zipkin { TEST(ZipkinUtilTest, utilTests) { EXPECT_EQ(typeid(uint64_t).name(), typeid(Util::generateRandom64()).name()); - std::string base16_string = Util::uint64ToHex(2722130815203937912ULL); - EXPECT_EQ(typeid(std::string).name(), typeid(base16_string).name()); - EXPECT_EQ("25c6f38dd0600e78", base16_string); - // Test JSON merging std::string merged_json = "{}"; diff --git a/test/common/tracing/zipkin/zipkin_core_types_test.cc b/test/common/tracing/zipkin/zipkin_core_types_test.cc index 1adcdd24699e2..738ff3e18834e 100644 --- a/test/common/tracing/zipkin/zipkin_core_types_test.cc +++ b/test/common/tracing/zipkin/zipkin_core_types_test.cc @@ -317,26 +317,26 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { span.toJson()); uint64_t id = Util::generateRandom64(); - std::string id_hex = Util::uint64ToHex(id); + std::string id_hex = Hex::uint64ToHex(id); span.setId(id); EXPECT_EQ(id, span.id()); EXPECT_EQ(id_hex, span.idAsHexString()); id = Util::generateRandom64(); - id_hex = Util::uint64ToHex(id); + id_hex = Hex::uint64ToHex(id); span.setParentId(id); EXPECT_EQ(id, span.parentId()); EXPECT_EQ(id_hex, span.parentIdAsHexString()); EXPECT_TRUE(span.isSet().parent_id_); id = Util::generateRandom64(); - id_hex = Util::uint64ToHex(id); + id_hex = Hex::uint64ToHex(id); span.setTraceId(id); EXPECT_EQ(id, span.traceId()); EXPECT_EQ(id_hex, span.traceIdAsHexString()); id = Util::generateRandom64(); - id_hex = Util::uint64ToHex(id); + id_hex = Hex::uint64ToHex(id); span.setTraceIdHigh(id); EXPECT_EQ(id, span.traceIdHigh()); EXPECT_TRUE(span.isSet().trace_id_high_); @@ -457,7 +457,7 @@ TEST(ZipkinCoreTypesSpanTest, copyConstructor) { Span span; uint64_t id = Util::generateRandom64(); - std::string id_hex = Util::uint64ToHex(id); + std::string id_hex = Hex::uint64ToHex(id); span.setId(id); span.setParentId(id); span.setTraceId(id); @@ -493,7 +493,7 @@ TEST(ZipkinCoreTypesSpanTest, assignmentOperator) { Span span; uint64_t id = Util::generateRandom64(); - std::string id_hex = Util::uint64ToHex(id); + std::string id_hex = Hex::uint64ToHex(id); span.setId(id); span.setParentId(id); span.setTraceId(id); From 9560f8e9d7a373ce7e59941dcf06534f4d6003bf Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Thu, 27 Apr 2017 10:29:15 -0400 Subject: [PATCH 44/55] Added TODO to remove explicit rapidjson header files (#430) --- source/common/tracing/zipkin/util.cc | 1 + source/common/tracing/zipkin/zipkin_core_types.cc | 1 + 2 files changed, 2 insertions(+) diff --git a/source/common/tracing/zipkin/util.cc b/source/common/tracing/zipkin/util.cc index a9f67cdbbc6ab..9b807afbc5e8e 100644 --- a/source/common/tracing/zipkin/util.cc +++ b/source/common/tracing/zipkin/util.cc @@ -10,6 +10,7 @@ #include "rapidjson/document.h" #include "rapidjson/stringbuffer.h" #include "rapidjson/writer.h" +// TODO(fabolive): Need to add interfaces to the JSON namespace namespace Zipkin { diff --git a/source/common/tracing/zipkin/zipkin_core_types.cc b/source/common/tracing/zipkin/zipkin_core_types.cc index 700ead32c7c33..40633d857eb99 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.cc +++ b/source/common/tracing/zipkin/zipkin_core_types.cc @@ -8,6 +8,7 @@ #include "rapidjson/stringbuffer.h" #include "rapidjson/writer.h" +// TODO(fabolive): Need to add interfaces to the JSON namespace namespace Zipkin { From 50322ee138635f31867a1624476804812f704d7c Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Thu, 27 Apr 2017 13:59:44 -0400 Subject: [PATCH 45/55] Refactored Zipkin library to use Network::Address and to handle IPv6. (#430) --- source/common/tracing/zipkin/BUILD | 2 + source/common/tracing/zipkin/tracer.cc | 16 +-- source/common/tracing/zipkin/tracer.h | 6 +- source/common/tracing/zipkin/util.cc | 12 +- source/common/tracing/zipkin/util.h | 9 -- .../tracing/zipkin/zipkin_core_types.cc | 37 +++--- .../common/tracing/zipkin/zipkin_core_types.h | 54 ++------ test/common/tracing/zipkin/tracer_test.cc | 30 +---- .../tracing/zipkin/zipkin_core_types_test.cc | 121 +++++++----------- 9 files changed, 95 insertions(+), 192 deletions(-) diff --git a/source/common/tracing/zipkin/BUILD b/source/common/tracing/zipkin/BUILD index ad8fe6a933b4a..655ad362e2c6b 100644 --- a/source/common/tracing/zipkin/BUILD +++ b/source/common/tracing/zipkin/BUILD @@ -27,6 +27,8 @@ envoy_cc_library( deps = [ "//include/envoy/common:time_interface", "//include/envoy/runtime:runtime_interface", + "//source/common/common:hex_lib", "//source/common/common:utility_lib", + "//source/common/network:address_lib", ], ) diff --git a/source/common/tracing/zipkin/tracer.cc b/source/common/tracing/zipkin/tracer.cc index 3655155af7c15..17b86279de10a 100644 --- a/source/common/tracing/zipkin/tracer.cc +++ b/source/common/tracing/zipkin/tracer.cc @@ -10,13 +10,7 @@ namespace Zipkin { SpanPtr Tracer::startSpan(const std::string& span_name, MonotonicTime start_time) { // Build the endpoint - std::string ip; - uint16_t port; - Util::getIPAndPort(address_, ip, port); - Endpoint ep; - ep.setIpv4(ip); - ep.setPort(port); - ep.setServiceName(service_name_); + Endpoint ep(service_name_, address_); // Build the CS annotation Annotation cs; @@ -91,13 +85,7 @@ SpanPtr Tracer::startSpan(const std::string& span_name, MonotonicTime start_time } // Build the endpoint - std::string ip; - uint16_t port; - Util::getIPAndPort(address_, ip, port); - Endpoint ep; - ep.setIpv4(ip); - ep.setPort(port); - ep.setServiceName(service_name_); + Endpoint ep(service_name_, address_); // Add the newly-created annotation to the span annotation.setEndpoint(std::move(ep)); diff --git a/source/common/tracing/zipkin/tracer.h b/source/common/tracing/zipkin/tracer.h index a46eba2ce55c0..7cf52fdfdfcb8 100644 --- a/source/common/tracing/zipkin/tracer.h +++ b/source/common/tracing/zipkin/tracer.h @@ -48,10 +48,10 @@ class Tracer : public TracerInterface { * * @param service_name The name of the service where the Tracer is running. This name is * used in all annotations' endpoints of the spans created by the Tracer. - * @param address A string in the format :. The IP address and port are used + * @param address Pointer to a network-address object. The IP address and port are used * in all annotations' endpoints of the spans created by the Tracer. */ - Tracer(const std::string& service_name, const std::string& address) + Tracer(const std::string& service_name, Network::Address::InstanceConstSharedPtr address) : service_name_(service_name), address_(address), random_generator_(nullptr) {} /** @@ -97,7 +97,7 @@ class Tracer : public TracerInterface { uint64_t generateRandomNumber(); const std::string service_name_; - const std::string address_; + Network::Address::InstanceConstSharedPtr address_; ReporterPtr reporter_; Runtime::RandomGeneratorPtr random_generator_; }; diff --git a/source/common/tracing/zipkin/util.cc b/source/common/tracing/zipkin/util.cc index 9b807afbc5e8e..6cbcee1281121 100644 --- a/source/common/tracing/zipkin/util.cc +++ b/source/common/tracing/zipkin/util.cc @@ -10,6 +10,7 @@ #include "rapidjson/document.h" #include "rapidjson/stringbuffer.h" #include "rapidjson/writer.h" + // TODO(fabolive): Need to add interfaces to the JSON namespace namespace Zipkin { @@ -51,15 +52,4 @@ uint64_t Util::generateRandom64() { std::mt19937_64 rand_64(seed); return rand_64(); } - -void Util::getIPAndPort(const std::string& address, std::string& ip, uint16_t& port) { - std::regex re("^(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})(:(\\d+))?$"); - std::smatch match; - if (std::regex_search(address, match, re)) { - ip = match.str(1); - if (match.str(3).size() > 0) { - port = std::stoi(match.str(3)); - } - } -} } // Zipkin diff --git a/source/common/tracing/zipkin/util.h b/source/common/tracing/zipkin/util.h index 9bbfbf1657fca..643b1dc1e3275 100644 --- a/source/common/tracing/zipkin/util.h +++ b/source/common/tracing/zipkin/util.h @@ -43,14 +43,5 @@ class Util { * Returns a randomly-generated 64-bit integer number. */ static uint64_t generateRandom64(); - - /** - * Extracts the IP address and port from a string of the form ":". - * - * @param address String of the form ":". - * @param ip It will be assigned the IP part of the address string. - * @param port It will be assigned the port part of the address string. - */ - static void getIPAndPort(const std::string& address, std::string& ip, uint16_t& port); }; } // Zipkin diff --git a/source/common/tracing/zipkin/zipkin_core_types.cc b/source/common/tracing/zipkin/zipkin_core_types.cc index 40633d857eb99..1f93c7d1d5f98 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.cc +++ b/source/common/tracing/zipkin/zipkin_core_types.cc @@ -8,25 +8,19 @@ #include "rapidjson/stringbuffer.h" #include "rapidjson/writer.h" + // TODO(fabolive): Need to add interfaces to the JSON namespace namespace Zipkin { Endpoint::Endpoint(const Endpoint& ep) { - ipv4_ = ep.ipv4(); - port_ = ep.port(); service_name_ = ep.serviceName(); - ipv6_ = ep.ipv6(); - isset_ipv6_ = ep.isSetIpv6(); + address_ = ep.address(); } Endpoint& Endpoint::operator=(const Endpoint& ep) { - ipv4_ = ep.ipv4(); - port_ = ep.port(); service_name_ = ep.serviceName(); - ipv6_ = ep.ipv6(); - isset_ipv6_ = ep.isSetIpv6(); - + address_ = ep.address(); return *this; } @@ -34,16 +28,25 @@ const std::string& Endpoint::toJson() { rapidjson::StringBuffer s; rapidjson::Writer writer(s); writer.StartObject(); - writer.Key(ZipkinJsonFieldNames::ENDPOINT_IPV4.c_str()); - writer.String(ipv4_.c_str()); - writer.Key(ZipkinJsonFieldNames::ENDPOINT_PORT.c_str()); - writer.Uint(port_); + if (!address_) { + writer.Key(ZipkinJsonFieldNames::ENDPOINT_IPV4.c_str()); + writer.String(""); + writer.Key(ZipkinJsonFieldNames::ENDPOINT_PORT.c_str()); + writer.Uint(0); + } else { + if (address_->ip()->version() == Network::Address::IpVersion::v4) { + // IPv4 + writer.Key(ZipkinJsonFieldNames::ENDPOINT_IPV4.c_str()); + } else { + // IPv6 + writer.Key(ZipkinJsonFieldNames::ENDPOINT_IPV6.c_str()); + } + writer.String(address_->ip()->addressAsString().c_str()); + writer.Key(ZipkinJsonFieldNames::ENDPOINT_PORT.c_str()); + writer.Uint(address_->ip()->port()); + } writer.Key(ZipkinJsonFieldNames::ENDPOINT_SERVICE_NAME.c_str()); writer.String(service_name_.c_str()); - if (isset_ipv6_) { - writer.Key(ZipkinJsonFieldNames::ENDPOINT_IPV6.c_str()); - writer.String(ipv6_.c_str()); - } writer.EndObject(); json_string_ = s.GetString(); diff --git a/source/common/tracing/zipkin/zipkin_core_types.h b/source/common/tracing/zipkin/zipkin_core_types.h index 1bcfd907d5dc0..1c1b682b1b88b 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.h +++ b/source/common/tracing/zipkin/zipkin_core_types.h @@ -3,6 +3,7 @@ #include #include "envoy/common/pure.h" +#include "envoy/network/address.h" #include "common/common/hex.h" #include "common/tracing/zipkin/tracer_interface.h" @@ -50,55 +51,26 @@ class Endpoint : public ZipkinBase { /** * Default constructor. Creates an empty Endpoint. */ - Endpoint() : ipv4_(), port_(0), service_name_(), isset_ipv6_(false) {} + Endpoint() : service_name_(), address_(nullptr) {} /** * Constructor that initializes an endpoint with the given attributes. * - * @param ipv4 String representing the endpoint's ipv4 attribute. - * @param port Integer representing the endpoint's port attribute. - * @param service_name String representing the endpoint's. + * @param service_name String representing the endpoint's service name + * @param address Pointer to an object representing the endpoint's network address */ - Endpoint(const std::string& ipv4, uint16_t port, const std::string& service_name) - : ipv4_(ipv4), port_(port), service_name_(service_name), isset_ipv6_(false) {} + Endpoint(const std::string& service_name, Network::Address::InstanceConstSharedPtr address) + : service_name_(service_name), address_(address) {} /** - * @return the endpoint's ipv4 as a string. + * @return the endpoint's address. */ - const std::string& ipv4() const { return ipv4_; } + Network::Address::InstanceConstSharedPtr address() const { return address_; } /** - * Sets the endpoint's ipv4 attribute. + * Sets the endpoint's address */ - void setIpv4(const std::string& ipv4) { ipv4_ = ipv4; } - - /** - * @return the endpoint's ipv6 as a string. - */ - const std::string& ipv6() const { return ipv6_; } - - /** - * Sets the endpoint's ipv6 attribute. - */ - void setIpv6(const std::string& ipv6) { - ipv6_ = ipv6; - isset_ipv6_ = true; - } - - /** - * @return true if the ipv6 attribute is set, or false otherwise. - */ - bool isSetIpv6() const { return isset_ipv6_; } - - /** - * @return the endpoint's port attribute. - */ - uint16_t port() const { return port_; } - - /** - * Sets the endpoint's port attribute. - */ - void setPort(uint16_t port) { port_ = port; } + void setAddress(Network::Address::InstanceConstSharedPtr address) { address_ = address; } /** * @return the endpoint's service name attribute. @@ -118,12 +90,8 @@ class Endpoint : public ZipkinBase { const std::string& toJson() override; private: - std::string ipv4_; - uint16_t port_; std::string service_name_; - std::string ipv6_; - - bool isset_ipv6_; + Network::Address::InstanceConstSharedPtr address_; }; /** diff --git a/test/common/tracing/zipkin/tracer_test.cc b/test/common/tracing/zipkin/tracer_test.cc index e536c7179f313..c147e5a1c7fd3 100644 --- a/test/common/tracing/zipkin/tracer_test.cc +++ b/test/common/tracing/zipkin/tracer_test.cc @@ -1,4 +1,5 @@ #include "common/common/utility.h" +#include "common/network/address_impl.h" #include "common/runtime/runtime_impl.h" #include "common/tracing/zipkin/tracer.h" #include "common/tracing/zipkin/util.h" @@ -21,7 +22,9 @@ class TestReporterImpl : public Reporter { }; TEST(ZipkinTracerTest, spanCreation) { - Tracer tracer("my_service_name", "127.0.0.1:9000"); + Network::Address::InstanceConstSharedPtr addr = + Network::Address::parseInternetAddressAndPort("127.0.0.1:9000"); + Tracer tracer("my_service_name", addr); MonotonicTime start_time = ProdMonotonicTimeSource::instance_.currentTime(); // ============== @@ -47,10 +50,7 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); Endpoint endpoint = ann.endpoint(); - EXPECT_EQ("127.0.0.1", endpoint.ipv4()); - EXPECT_EQ(9000, endpoint.port()); EXPECT_EQ("my_service_name", endpoint.serviceName()); - EXPECT_FALSE(endpoint.isSetIpv6()); // The tracer must have been properly set EXPECT_EQ(dynamic_cast(&tracer), root_span->tracer()); @@ -93,10 +93,7 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); endpoint = ann.endpoint(); - EXPECT_EQ("127.0.0.1", endpoint.ipv4()); - EXPECT_EQ(9000, endpoint.port()); EXPECT_EQ("my_service_name", endpoint.serviceName()); - EXPECT_FALSE(endpoint.isSetIpv6()); // The tracer must have been properly set EXPECT_EQ(dynamic_cast(&tracer), server_side_shared_context_span->tracer()); @@ -137,10 +134,7 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); endpoint = ann.endpoint(); - EXPECT_EQ("127.0.0.1", endpoint.ipv4()); - EXPECT_EQ(9000, endpoint.port()); EXPECT_EQ("my_service_name", endpoint.serviceName()); - EXPECT_FALSE(endpoint.isSetIpv6()); // The tracer must have been properly set EXPECT_EQ(dynamic_cast(&tracer), child_span->tracer()); @@ -150,7 +144,9 @@ TEST(ZipkinTracerTest, spanCreation) { } TEST(ZipkinTracerTest, finishSpan) { - Tracer tracer("my_service_name", "127.0.0.1:9000"); + Network::Address::InstanceConstSharedPtr addr = + Network::Address::parseInternetAddressAndPort("127.0.0.1:9000"); + Tracer tracer("my_service_name", addr); tracer.setRandomGenerator(Runtime::RandomGeneratorPtr(new Runtime::RandomGeneratorImpl())); MonotonicTime start_time = ProdMonotonicTimeSource::instance_.currentTime(); @@ -171,10 +167,7 @@ TEST(ZipkinTracerTest, finishSpan) { EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); Endpoint endpoint = ann.endpoint(); - EXPECT_EQ("127.0.0.1", endpoint.ipv4()); - EXPECT_EQ(9000, endpoint.port()); EXPECT_EQ("my_service_name", endpoint.serviceName()); - EXPECT_FALSE(endpoint.isSetIpv6()); // Check the CR annotation added when ending the span ann = span->annotations()[1]; @@ -182,10 +175,7 @@ TEST(ZipkinTracerTest, finishSpan) { EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); endpoint = ann.endpoint(); - EXPECT_EQ("127.0.0.1", endpoint.ipv4()); - EXPECT_EQ(9000, endpoint.port()); EXPECT_EQ("my_service_name", endpoint.serviceName()); - EXPECT_FALSE(endpoint.isSetIpv6()); // ============== // Test finishing a span containing an SR annotation @@ -212,10 +202,7 @@ TEST(ZipkinTracerTest, finishSpan) { EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); endpoint = ann.endpoint(); - EXPECT_EQ("127.0.0.1", endpoint.ipv4()); - EXPECT_EQ(9000, endpoint.port()); EXPECT_EQ("my_service_name", endpoint.serviceName()); - EXPECT_FALSE(endpoint.isSetIpv6()); // Check the SS annotation added when ending the span ann = server_side->annotations()[1]; @@ -223,9 +210,6 @@ TEST(ZipkinTracerTest, finishSpan) { EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); endpoint = ann.endpoint(); - EXPECT_EQ("127.0.0.1", endpoint.ipv4()); - EXPECT_EQ(9000, endpoint.port()); EXPECT_EQ("my_service_name", endpoint.serviceName()); - EXPECT_FALSE(endpoint.isSetIpv6()); } } // Zipkin diff --git a/test/common/tracing/zipkin/zipkin_core_types_test.cc b/test/common/tracing/zipkin/zipkin_core_types_test.cc index 738ff3e18834e..fff28999702dd 100644 --- a/test/common/tracing/zipkin/zipkin_core_types_test.cc +++ b/test/common/tracing/zipkin/zipkin_core_types_test.cc @@ -1,4 +1,5 @@ #include "common/common/utility.h" +#include "common/network/address_impl.h" #include "common/tracing/zipkin/zipkin_core_constants.h" #include "common/tracing/zipkin/zipkin_core_types.h" @@ -9,79 +10,67 @@ namespace Zipkin { TEST(ZipkinCoreTypesEndpointTest, defaultConstructor) { Endpoint ep; - EXPECT_EQ("", ep.ipv4()); - EXPECT_EQ(0, ep.port()); EXPECT_EQ("", ep.serviceName()); - EXPECT_FALSE(ep.isSetIpv6()); EXPECT_EQ(R"({"ipv4":"","port":0,"serviceName":""})", ep.toJson()); - ep.setIpv4(std::string("127.0.0.1")); - EXPECT_EQ("127.0.0.1", ep.ipv4()); + Network::Address::InstanceConstSharedPtr addr = + Network::Address::parseInternetAddress("127.0.0.1"); + ep.setAddress(addr); + EXPECT_EQ(R"({"ipv4":"127.0.0.1","port":0,"serviceName":""})", ep.toJson()); - ep.setIpv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); - EXPECT_EQ("2001:0db8:85a3:0000:0000:8a2e:0370:7334", ep.ipv6()); - EXPECT_TRUE(ep.isSetIpv6()); - - ep.setPort(3306); - EXPECT_EQ(3306, ep.port()); + addr = Network::Address::parseInternetAddressAndPort( + "[2001:0db8:85a3:0000:0000:8a2e:0370:4444]:7334"); + ep.setAddress(addr); + EXPECT_EQ(R"({"ipv6":"2001:db8:85a3::8a2e:370:4444","port":7334,"serviceName":""})", ep.toJson()); ep.setServiceName("my_service"); EXPECT_EQ("my_service", ep.serviceName()); - EXPECT_EQ(R"({"ipv4":"127.0.0.1","port":3306,"serviceName":"my_service",)" - R"("ipv6":"2001:0db8:85a3:0000:0000:8a2e:0370:7334"})", - ep.toJson()); + EXPECT_EQ( + R"({"ipv6":"2001:db8:85a3::8a2e:370:4444","port":7334,"serviceName":"my_service"})", + ep.toJson()); } TEST(ZipkinCoreTypesEndpointTest, customConstructor) { - Endpoint ep(std::string("127.0.0.1"), 3306, std::string("my_service")); + Network::Address::InstanceConstSharedPtr addr = + Network::Address::parseInternetAddressAndPort("127.0.0.1:3306"); + Endpoint ep(std::string("my_service"), addr); - EXPECT_EQ("127.0.0.1", ep.ipv4()); - EXPECT_EQ(3306, ep.port()); EXPECT_EQ("my_service", ep.serviceName()); - EXPECT_FALSE(ep.isSetIpv6()); EXPECT_EQ(R"({"ipv4":"127.0.0.1","port":3306,"serviceName":"my_service"})", ep.toJson()); - ep.setIpv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); - EXPECT_EQ("2001:0db8:85a3:0000:0000:8a2e:0370:7334", ep.ipv6()); - EXPECT_TRUE(ep.isSetIpv6()); + addr = Network::Address::parseInternetAddressAndPort( + "[2001:0db8:85a3:0000:0000:8a2e:0370:4444]:7334"); + ep.setAddress(addr); - EXPECT_EQ(R"({"ipv4":"127.0.0.1","port":3306,"serviceName":"my_service",)" - R"("ipv6":"2001:0db8:85a3:0000:0000:8a2e:0370:7334"})", - ep.toJson()); + EXPECT_EQ( + R"({"ipv6":"2001:db8:85a3::8a2e:370:4444","port":7334,"serviceName":"my_service"})", + ep.toJson()); } TEST(ZipkinCoreTypesEndpointTest, copyOperator) { - Endpoint ep1(std::string("127.0.0.1"), 3306, std::string("my_service")); + Network::Address::InstanceConstSharedPtr addr = + Network::Address::parseInternetAddressAndPort("127.0.0.1:3306"); + Endpoint ep1(std::string("my_service"), addr); Endpoint ep2(ep1); - EXPECT_EQ("127.0.0.1", ep1.ipv4()); - EXPECT_EQ(3306, ep1.port()); EXPECT_EQ("my_service", ep1.serviceName()); - EXPECT_FALSE(ep1.isSetIpv6()); EXPECT_EQ(R"({"ipv4":"127.0.0.1","port":3306,"serviceName":"my_service"})", ep1.toJson()); - EXPECT_EQ(ep1.ipv4(), ep2.ipv4()); - EXPECT_EQ(ep1.port(), ep2.port()); EXPECT_EQ(ep1.serviceName(), ep2.serviceName()); - EXPECT_FALSE(ep2.isSetIpv6()); EXPECT_EQ(ep1.toJson(), ep2.toJson()); } TEST(ZipkinCoreTypesEndpointTest, assignmentOperator) { - Endpoint ep1(std::string("127.0.0.1"), 3306, std::string("my_service")); + Network::Address::InstanceConstSharedPtr addr = + Network::Address::parseInternetAddressAndPort("127.0.0.1:3306"); + Endpoint ep1(std::string("my_service"), addr); Endpoint ep2 = ep1; - EXPECT_EQ("127.0.0.1", ep1.ipv4()); - EXPECT_EQ(3306, ep1.port()); EXPECT_EQ("my_service", ep1.serviceName()); - EXPECT_FALSE(ep1.isSetIpv6()); EXPECT_EQ(R"({"ipv4":"127.0.0.1","port":3306,"serviceName":"my_service"})", ep1.toJson()); - EXPECT_EQ(ep1.ipv4(), ep2.ipv4()); - EXPECT_EQ(ep1.port(), ep2.port()); EXPECT_EQ(ep1.serviceName(), ep2.serviceName()); - EXPECT_FALSE(ep2.isSetIpv6()); EXPECT_EQ(ep1.toJson(), ep2.toJson()); } @@ -106,14 +95,12 @@ TEST(ZipkinCoreTypesAnnotationTest, defaultConstructor) { EXPECT_EQ(expected_json, ann.toJson()); // Test the copy-semantics flavor of setEndpoint - - Endpoint ep(std::string("127.0.0.1"), 3306, std::string("my_service")); + Network::Address::InstanceConstSharedPtr addr = + Network::Address::parseInternetAddressAndPort("127.0.0.1:3306"); + Endpoint ep(std::string("my_service"), addr); ann.setEndpoint(ep); EXPECT_TRUE(ann.isSetEndpoint()); - EXPECT_EQ("127.0.0.1", ann.endpoint().ipv4()); - EXPECT_EQ(3306, ann.endpoint().port()); EXPECT_EQ("my_service", ann.endpoint().serviceName()); - EXPECT_FALSE(ann.endpoint().isSetIpv6()); EXPECT_EQ(R"({"ipv4":"127.0.0.1","port":3306,"serviceName":"my_service"})", (const_cast(ann.endpoint())).toJson()); @@ -124,14 +111,11 @@ TEST(ZipkinCoreTypesAnnotationTest, defaultConstructor) { EXPECT_EQ(expected_json, ann.toJson()); // Test the move-semantics flavor of setEndpoint - - Endpoint ep2(std::string("192.168.1.1"), 5555, std::string("my_service_2")); + addr = Network::Address::parseInternetAddressAndPort("192.168.1.1:5555"); + Endpoint ep2(std::string("my_service_2"), addr); ann.setEndpoint(std::move(ep2)); EXPECT_TRUE(ann.isSetEndpoint()); - EXPECT_EQ("192.168.1.1", ann.endpoint().ipv4()); - EXPECT_EQ(5555, ann.endpoint().port()); EXPECT_EQ("my_service_2", ann.endpoint().serviceName()); - EXPECT_FALSE(ann.endpoint().isSetIpv6()); EXPECT_EQ(R"({"ipv4":"192.168.1.1","port":5555,"serviceName":"my_service_2"})", (const_cast(ann.endpoint())).toJson()); @@ -143,7 +127,9 @@ TEST(ZipkinCoreTypesAnnotationTest, defaultConstructor) { } TEST(ZipkinCoreTypesAnnotationTest, customConstructor) { - Endpoint ep(std::string("127.0.0.1"), 3306, std::string("my_service")); + Network::Address::InstanceConstSharedPtr addr = + Network::Address::parseInternetAddressAndPort("127.0.0.1:3306"); + Endpoint ep(std::string("my_service"), addr); uint64_t timestamp = std::chrono::duration_cast( ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); @@ -153,10 +139,7 @@ TEST(ZipkinCoreTypesAnnotationTest, customConstructor) { EXPECT_EQ(ZipkinCoreConstants::CLIENT_SEND, ann.value()); EXPECT_TRUE(ann.isSetEndpoint()); - EXPECT_EQ("127.0.0.1", ann.endpoint().ipv4()); - EXPECT_EQ(3306, ann.endpoint().port()); EXPECT_EQ("my_service", ann.endpoint().serviceName()); - EXPECT_FALSE(ann.endpoint().isSetIpv6()); EXPECT_EQ(R"({"ipv4":"127.0.0.1","port":3306,"serviceName":"my_service"})", (const_cast(ann.endpoint())).toJson()); @@ -168,7 +151,9 @@ TEST(ZipkinCoreTypesAnnotationTest, customConstructor) { } TEST(ZipkinCoreTypesAnnotationTest, copyConstructor) { - Endpoint ep(std::string("127.0.0.1"), 3306, std::string("my_service")); + Network::Address::InstanceConstSharedPtr addr = + Network::Address::parseInternetAddressAndPort("127.0.0.1:3306"); + Endpoint ep(std::string("my_service"), addr); uint64_t timestamp = std::chrono::duration_cast( ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); @@ -179,14 +164,13 @@ TEST(ZipkinCoreTypesAnnotationTest, copyConstructor) { EXPECT_EQ(ann.timestamp(), ann2.timestamp()); EXPECT_EQ(ann.isSetEndpoint(), ann2.isSetEndpoint()); EXPECT_EQ(ann.toJson(), ann2.toJson()); - - EXPECT_EQ(ann.endpoint().ipv4(), ann2.endpoint().ipv4()); - EXPECT_EQ(ann.endpoint().port(), ann2.endpoint().port()); EXPECT_EQ(ann.endpoint().serviceName(), ann2.endpoint().serviceName()); } TEST(ZipkinCoreTypesAnnotationTest, assignmentOperator) { - Endpoint ep(std::string("127.0.0.1"), 3306, std::string("my_service")); + Network::Address::InstanceConstSharedPtr addr = + Network::Address::parseInternetAddressAndPort("127.0.0.1:3306"); + Endpoint ep(std::string("my_service"), addr); uint64_t timestamp = std::chrono::duration_cast( ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); @@ -197,9 +181,6 @@ TEST(ZipkinCoreTypesAnnotationTest, assignmentOperator) { EXPECT_EQ(ann.timestamp(), ann2.timestamp()); EXPECT_EQ(ann.isSetEndpoint(), ann2.isSetEndpoint()); EXPECT_EQ(ann.toJson(), ann2.toJson()); - - EXPECT_EQ(ann.endpoint().ipv4(), ann2.endpoint().ipv4()); - EXPECT_EQ(ann.endpoint().port(), ann2.endpoint().port()); EXPECT_EQ(ann.endpoint().serviceName(), ann2.endpoint().serviceName()); } @@ -222,13 +203,12 @@ TEST(ZipkinCoreTypesBinaryAnnotationTest, defaultConstructor) { // Test the copy-semantics flavor of setEndpoint - Endpoint ep(std::string("127.0.0.1"), 3306, std::string("my_service")); + Network::Address::InstanceConstSharedPtr addr = + Network::Address::parseInternetAddressAndPort("127.0.0.1:3306"); + Endpoint ep(std::string("my_service"), addr); ann.setEndpoint(ep); EXPECT_TRUE(ann.isSetEndpoint()); - EXPECT_EQ("127.0.0.1", ann.endpoint().ipv4()); - EXPECT_EQ(3306, ann.endpoint().port()); EXPECT_EQ("my_service", ann.endpoint().serviceName()); - EXPECT_FALSE(ann.endpoint().isSetIpv6()); EXPECT_EQ(R"({"ipv4":"127.0.0.1","port":3306,"serviceName":"my_service"})", (const_cast(ann.endpoint())).toJson()); @@ -240,14 +220,11 @@ TEST(ZipkinCoreTypesBinaryAnnotationTest, defaultConstructor) { EXPECT_EQ(expected_json, ann.toJson()); // Test the move-semantics flavor of setEndpoint - - Endpoint ep2(std::string("192.168.1.1"), 5555, std::string("my_service_2")); + addr = Network::Address::parseInternetAddressAndPort("192.168.1.1:5555"); + Endpoint ep2(std::string("my_service_2"), addr); ann.setEndpoint(std::move(ep2)); EXPECT_TRUE(ann.isSetEndpoint()); - EXPECT_EQ("192.168.1.1", ann.endpoint().ipv4()); - EXPECT_EQ(5555, ann.endpoint().port()); EXPECT_EQ("my_service_2", ann.endpoint().serviceName()); - EXPECT_FALSE(ann.endpoint().isSetIpv6()); EXPECT_EQ(R"({"ipv4":"192.168.1.1","port":5555,"serviceName":"my_service_2"})", (const_cast(ann.endpoint())).toJson()); expected_json = "{" @@ -370,9 +347,9 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { std::vector binary_annotations; endpoint.setServiceName("my_service_name"); - std::string ip = "192.168.1.2"; - endpoint.setIpv4(ip); - endpoint.setPort(3306); + Network::Address::InstanceConstSharedPtr addr = + Network::Address::parseInternetAddressAndPort("192.168.1.2:3306"); + endpoint.setAddress(addr); ann.setValue(Zipkin::ZipkinCoreConstants::CLIENT_SEND); ann.setTimestamp(timestamp); From 4628898808ece69663c7bd52a3bdcb849c31dd17 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Thu, 27 Apr 2017 20:29:12 -0400 Subject: [PATCH 46/55] Refactored Zipkin library to use the Optional class. (#430) --- source/common/tracing/zipkin/span_context.cc | 8 +- .../tracing/zipkin/zipkin_core_types.cc | 78 ++++++---- .../common/tracing/zipkin/zipkin_core_types.h | 143 ++++++++---------- .../tracing/zipkin/span_context_test.cc | 4 +- test/common/tracing/zipkin/tracer_test.cc | 15 +- .../tracing/zipkin/zipkin_core_types_test.cc | 46 +++--- 6 files changed, 144 insertions(+), 150 deletions(-) diff --git a/source/common/tracing/zipkin/span_context.cc b/source/common/tracing/zipkin/span_context.cc index 72bac2e23d45e..623b40f1354ae 100644 --- a/source/common/tracing/zipkin/span_context.cc +++ b/source/common/tracing/zipkin/span_context.cc @@ -39,7 +39,7 @@ const std::regex& SpanContext::SPAN_CONTEXT_REGEX() { SpanContext::SpanContext(const Span& span) { trace_id_ = span.traceId(); id_ = span.id(); - parent_id_ = span.parentId(); + parent_id_ = span.isSetParentId() ? span.parentId() : 0; for (const Annotation& annotation : span.annotations()) { if (annotation.value() == ZipkinCoreConstants::CLIENT_RECV) { @@ -93,9 +93,9 @@ void SpanContext::populateFromString(const std::string& span_context_str) { if (std::regex_search(span_context_str, match, SPAN_CONTEXT_REGEX())) { // This is a valid string encoding of the context - trace_id_ = stoull(match.str(1), nullptr, 16); - id_ = stoull(match.str(2), nullptr, 16); - parent_id_ = stoull(match.str(3), nullptr, 16); + trace_id_ = std::stoull(match.str(1), nullptr, 16); + id_ = std::stoull(match.str(2), nullptr, 16); + parent_id_ = std::stoull(match.str(3), nullptr, 16); std::string matched_annotations = match.str(4); if (matched_annotations.size() > 0) { diff --git a/source/common/tracing/zipkin/zipkin_core_types.cc b/source/common/tracing/zipkin/zipkin_core_types.cc index 1f93c7d1d5f98..c6e13ad15ecc5 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.cc +++ b/source/common/tracing/zipkin/zipkin_core_types.cc @@ -56,15 +56,17 @@ const std::string& Endpoint::toJson() { Annotation::Annotation(const Annotation& ann) { timestamp_ = ann.timestamp(); value_ = ann.value(); - endpoint_ = ann.endpoint(); - isset_endpoint_ = ann.isSetEndpoint(); + if (ann.isSetEndpoint()) { + endpoint_ = ann.endpoint(); + } } Annotation& Annotation::operator=(const Annotation& ann) { timestamp_ = ann.timestamp(); value_ = ann.value(); - endpoint_ = ann.endpoint(); - isset_endpoint_ = ann.isSetEndpoint(); + if (ann.isSetEndpoint()) { + endpoint_ = ann.endpoint(); + } return *this; } @@ -81,8 +83,8 @@ const std::string& Annotation::toJson() { json_string_ = s.GetString(); - if (isset_endpoint_) { - Util::mergeJsons(json_string_, endpoint_.toJson(), + if (endpoint_.valid()) { + Util::mergeJsons(json_string_, static_cast(endpoint_.value()).toJson(), ZipkinJsonFieldNames::ANNOTATION_ENDPOINT.c_str()); } @@ -93,16 +95,18 @@ BinaryAnnotation::BinaryAnnotation(const BinaryAnnotation& ann) { key_ = ann.key(); value_ = ann.value(); annotation_type_ = ann.annotationType(); - endpoint_ = ann.endpoint(); - isset_endpoint_ = ann.isSetEndpoint(); + if (ann.isSetEndpoint()) { + endpoint_ = ann.endpoint(); + } } BinaryAnnotation& BinaryAnnotation::operator=(const BinaryAnnotation& ann) { key_ = ann.key(); value_ = ann.value(); annotation_type_ = ann.annotationType(); - endpoint_ = ann.endpoint(); - isset_endpoint_ = ann.isSetEndpoint(); + if (ann.isSetEndpoint()) { + endpoint_ = ann.endpoint(); + } return *this; } @@ -119,25 +123,35 @@ const std::string& BinaryAnnotation::toJson() { json_string_ = s.GetString(); - if (isset_endpoint_) { - Util::mergeJsons(json_string_, endpoint_.toJson(), + if (endpoint_.valid()) { + Util::mergeJsons(json_string_, static_cast(endpoint_.value()).toJson(), ZipkinJsonFieldNames::BINARY_ANNOTATION_ENDPOINT.c_str()); } return json_string_; } +const std::string Span::EMPTY_HEX_STRING_ = "0000000000000000"; + Span::Span(const Span& span) { trace_id_ = span.traceId(); name_ = span.name(); id_ = span.id(); - parent_id_ = span.parentId(); + if (span.isSetParentId()) { + parent_id_ = span.parentId(); + } + debug_ = span.debug(); annotations_ = span.annotations(); binary_annotations_ = span.binaryAnnotations(); - timestamp_ = span.timestamp(); - duration_ = span.duration(); - trace_id_high_ = span.traceIdHigh(); - isset_ = span.isSet(); + if (span.isSetTimestamp()) { + timestamp_ = span.timestamp(); + } + if (span.isSetDuration()) { + duration_ = span.duration(); + } + if (span.isSetTraceIdHigh()) { + trace_id_high_ = span.traceIdHigh(); + } monotonic_start_time_ = span.startTime(); tracer_ = span.tracer(); } @@ -146,13 +160,21 @@ Span& Span::operator=(const Span& span) { trace_id_ = span.traceId(); name_ = span.name(); id_ = span.id(); - parent_id_ = span.parentId(); + if (span.isSetParentId()) { + parent_id_ = span.parentId(); + } + debug_ = span.debug(); annotations_ = span.annotations(); binary_annotations_ = span.binaryAnnotations(); - timestamp_ = span.timestamp(); - duration_ = span.duration(); - trace_id_high_ = span.traceIdHigh(); - isset_ = span.isSet(); + if (span.isSetTimestamp()) { + timestamp_ = span.timestamp(); + } + if (span.isSetDuration()) { + duration_ = span.duration(); + } + if (span.isSetTraceIdHigh()) { + trace_id_high_ = span.traceIdHigh(); + } monotonic_start_time_ = span.startTime(); tracer_ = span.tracer(); @@ -170,19 +192,19 @@ const std::string& Span::toJson() { writer.Key(ZipkinJsonFieldNames::SPAN_ID.c_str()); writer.String(Hex::uint64ToHex(id_).c_str()); - if (isset_.parent_id_ && parent_id_) { + if (parent_id_.valid() && parent_id_.value()) { writer.Key(ZipkinJsonFieldNames::SPAN_PARENT_ID.c_str()); - writer.String(Hex::uint64ToHex(parent_id_).c_str()); + writer.String(Hex::uint64ToHex(parent_id_.value()).c_str()); } - if (isset_.timestamp_) { + if (timestamp_.valid()) { writer.Key(ZipkinJsonFieldNames::SPAN_TIMESTAMP.c_str()); - writer.Int64(timestamp_); + writer.Int64(timestamp_.value()); } - if (isset_.duration_) { + if (duration_.valid()) { writer.Key(ZipkinJsonFieldNames::SPAN_DURATION.c_str()); - writer.Int64(duration_); + writer.Int64(duration_.value()); } writer.EndObject(); diff --git a/source/common/tracing/zipkin/zipkin_core_types.h b/source/common/tracing/zipkin/zipkin_core_types.h index 1c1b682b1b88b..139c4a646e544 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.h +++ b/source/common/tracing/zipkin/zipkin_core_types.h @@ -2,6 +2,7 @@ #include +#include "envoy/common/optional.h" #include "envoy/common/pure.h" #include "envoy/network/address.h" @@ -113,7 +114,7 @@ class Annotation : public ZipkinBase { /** * Default constructor. Creates an empty annotation. */ - Annotation() : timestamp_(0), value_(), isset_endpoint_(false) {} + Annotation() : timestamp_(0), value_() {} /** * Constructor that creates an annotation based on the given parameters. @@ -124,28 +125,22 @@ class Annotation : public ZipkinBase { * @param endpoint The endpoint object representing the annotation's endpoint attribute. */ Annotation(uint64_t timestamp, const std::string value, Endpoint& endpoint) - : timestamp_(timestamp), value_(value), endpoint_(endpoint), isset_endpoint_(true) {} + : timestamp_(timestamp), value_(value), endpoint_(endpoint) {} /** * @return the annotation's endpoint attribute. */ - const Endpoint& endpoint() const { return endpoint_; } + const Endpoint& endpoint() const { return endpoint_.value(); } /** * Sets the annotation's endpoint attribute (copy semantics). */ - void setEndpoint(const Endpoint& endpoint) { - endpoint_ = endpoint; - isset_endpoint_ = true; - } + void setEndpoint(const Endpoint& endpoint) { endpoint_.value(endpoint); } /** * Sets the annotation's endpoint attribute (move semantics). */ - void setEndpoint(const Endpoint&& endpoint) { - endpoint_ = endpoint; - isset_endpoint_ = true; - } + void setEndpoint(const Endpoint&& endpoint) { endpoint_.value(endpoint); } /** * @return the annotation's timestamp attribute @@ -171,7 +166,7 @@ class Annotation : public ZipkinBase { /** * @return true if the endpoint attribute is set, or false otherwise. */ - bool isSetEndpoint() const { return isset_endpoint_; } + bool isSetEndpoint() const { return endpoint_.valid(); } /** * Serializes the annotation as a Zipkin-compliant JSON representation as a string. @@ -183,9 +178,7 @@ class Annotation : public ZipkinBase { private: uint64_t timestamp_; std::string value_; - Endpoint endpoint_; - - bool isset_endpoint_; + Optional endpoint_; }; /** @@ -213,7 +206,7 @@ class BinaryAnnotation : public ZipkinBase { /** * Default constructor. Creates an empty binary annotation. */ - BinaryAnnotation() : key_(), value_(), annotation_type_(STRING), isset_endpoint_(false) {} + BinaryAnnotation() : key_(), value_(), annotation_type_(STRING) {} /** * Constructor that creates a binary annotation based on the given parameters. @@ -222,7 +215,7 @@ class BinaryAnnotation : public ZipkinBase { * @param value The value associated with the key. */ BinaryAnnotation(const std::string& key, const std::string& value) - : key_(key), value_(value), annotation_type_(STRING), isset_endpoint_(false) {} + : key_(key), value_(value), annotation_type_(STRING) {} /** * @return the type of the binary annotation. @@ -237,28 +230,22 @@ class BinaryAnnotation : public ZipkinBase { /** * @return the annotation's endpoint attribute. */ - const Endpoint& endpoint() const { return endpoint_; } + const Endpoint& endpoint() const { return endpoint_.value(); } /** * Sets the annotation's endpoint attribute (copy semantics). */ - void setEndpoint(const Endpoint& endpoint) { - endpoint_ = endpoint; - isset_endpoint_ = true; - } + void setEndpoint(const Endpoint& endpoint) { endpoint_.value(endpoint); } /** * Sets the annotation's endpoint attribute (move semantics). */ - void setEndpoint(const Endpoint&& endpoint) { - endpoint_ = endpoint; - isset_endpoint_ = true; - } + void setEndpoint(const Endpoint&& endpoint) { endpoint_.value(endpoint); } /** * @return true of the endpoint attribute has been set, or false otherwise. */ - bool isSetEndpoint() const { return isset_endpoint_; } + bool isSetEndpoint() const { return endpoint_.valid(); } /** * @return the key attribute. @@ -290,28 +277,10 @@ class BinaryAnnotation : public ZipkinBase { private: std::string key_; std::string value_; - Endpoint endpoint_; - + Optional endpoint_; AnnotationType annotation_type_; - - bool isset_endpoint_; }; -/** - * This struct identifies which of the optional attributes are set in a Span object. - * Each member is a one-bit boolean indicating whether or not the corresponding attribute is set. - */ -typedef struct SpanIsSet { - SpanIsSet() - : parent_id_(false), debug_(false), timestamp_(false), duration_(false), - trace_id_high_(false) {} - bool parent_id_ : 1; - bool debug_ : 1; - bool timestamp_ : 1; - bool duration_ : 1; - bool trace_id_high_ : 1; -} SpanIsSet; - typedef std::unique_ptr SpanPtr; /** @@ -333,8 +302,7 @@ class Span : public ZipkinBase { * Default constructor. Creates an empty span. */ Span() - : trace_id_(0), name_(), id_(0), parent_id_(0), timestamp_(0), duration_(0), - trace_id_high_(0), monotonic_start_time_(0), tracer_(nullptr) {} + : trace_id_(0), name_(), id_(0), debug_(false), monotonic_start_time_(0), tracer_(nullptr) {} /** * Sets the span's trace id attribute. @@ -354,10 +322,12 @@ class Span : public ZipkinBase { /** * Sets the span's parent id. */ - void setParentId(const uint64_t val) { - parent_id_ = val; - isset_.parent_id_ = true; - } + void setParentId(const uint64_t val) { parent_id_.value(val); } + + /** + * @return Whether or not the parent_id attribute is set. + */ + bool isSetParentId() const { return parent_id_.valid(); } /** * @return a vector with all annotations added to the span. @@ -397,32 +367,38 @@ class Span : public ZipkinBase { /** * Sets the span's debug attribute. */ - void setDebug() { isset_.debug_ = true; } + void setDebug() { debug_ = true; } /** * Sets the span's timestamp attribute. */ - void setTimestamp(const int64_t val) { - timestamp_ = val; - isset_.timestamp_ = true; - } + void setTimestamp(const int64_t val) { timestamp_.value(val); } + + /** + * @return Whether or not the timestamp attribute is set. + */ + bool isSetTimestamp() const { return timestamp_.valid(); } /** * Sets the span's duration attribute. */ - void setDuration(const int64_t val) { - duration_ = val; - isset_.duration_ = true; - } + void setDuration(const int64_t val) { duration_.value(val); } + + /** + * @return Whether or not the duration attribute is set. + */ + bool isSetDuration() const { return duration_.valid(); } /** * Sets the higher 64 bits of the span's 128-bit trace id. * Note that this is optional, since 64-bit trace ids are valid. */ - void setTraceIdHigh(const uint64_t val) { - trace_id_high_ = val; - isset_.trace_id_high_ = true; - } + void setTraceIdHigh(const uint64_t val) { trace_id_high_.value(val); } + + /** + * @return whether or not the trace_id_high attribute is set. + */ + bool isSetTraceIdHigh() const { return trace_id_high_.valid(); } /** * Sets the span start-time attribute (monotonic, used to calculate duration). @@ -442,7 +418,7 @@ class Span : public ZipkinBase { /** * @return the span's duration attribute. */ - int64_t duration() const { return duration_; } + int64_t duration() const { return duration_.value(); } /** * @return the span's id as an integer. @@ -452,12 +428,7 @@ class Span : public ZipkinBase { /** * @return the span's id as a hexadecimal string. */ - std::string idAsHexString() const { return Hex::uint64ToHex(id_); } - - /** - * @return a struct indicating which of the span's optional attributes are set. - */ - const SpanIsSet& isSet() const { return isset_; } + const std::string idAsHexString() const { return Hex::uint64ToHex(id_); } /** * @return the span's name. @@ -467,17 +438,24 @@ class Span : public ZipkinBase { /** * @return the span's parent id as an integer. */ - uint64_t parentId() const { return parent_id_; } + uint64_t parentId() const { return parent_id_.value(); } /** * @return the span's parent id as a hexadecimal string. */ - std::string parentIdAsHexString() const { return Hex::uint64ToHex(parent_id_); } + const std::string parentIdAsHexString() const { + return parent_id_.valid() ? Hex::uint64ToHex(parent_id_.value()) : EMPTY_HEX_STRING_; + } + + /** + * @return whether or not the debug attribute is set + */ + bool debug() const { return debug_; } /** * @return the span's timestamp (clock time for user presentation: microseconds since epoch). */ - int64_t timestamp() const { return timestamp_; } + int64_t timestamp() const { return timestamp_.value(); } /** * @return the span's trace id as an integer. @@ -487,12 +465,12 @@ class Span : public ZipkinBase { /** * @return the span's trace id as a hexadecimal string. */ - std::string traceIdAsHexString() const { return Hex::uint64ToHex(trace_id_); } + const std::string traceIdAsHexString() const { return Hex::uint64ToHex(trace_id_); } /** * @return the higher 64 bits of a 128-bit trace id. */ - uint64_t traceIdHigh() const { return trace_id_high_; } + uint64_t traceIdHigh() const { return trace_id_high_.value(); } /** * @return the span's start time (monotonic, used to calculate duration). @@ -541,17 +519,18 @@ class Span : public ZipkinBase { void setTag(const std::string& name, const std::string& value); private: + static const std::string EMPTY_HEX_STRING_; uint64_t trace_id_; std::string name_; uint64_t id_; - uint64_t parent_id_; + Optional parent_id_; + bool debug_; std::vector annotations_; std::vector binary_annotations_; - int64_t timestamp_; - int64_t duration_; - uint64_t trace_id_high_; + Optional timestamp_; + Optional duration_; + Optional trace_id_high_; int64_t monotonic_start_time_; TracerInterface* tracer_; - SpanIsSet isset_; }; } // Zipkin diff --git a/test/common/tracing/zipkin/span_context_test.cc b/test/common/tracing/zipkin/span_context_test.cc index f01b62a687959..198df0bc2b42b 100644 --- a/test/common/tracing/zipkin/span_context_test.cc +++ b/test/common/tracing/zipkin/span_context_test.cc @@ -146,9 +146,9 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { span_context_2.serializeToString()); // Test if we can handle 128-bit trace ids - EXPECT_FALSE(span.isSet().trace_id_high_); + EXPECT_FALSE(span.isSetTraceIdHigh()); span.setTraceIdHigh(9922130815203937912ULL); - EXPECT_TRUE(span.isSet().trace_id_high_); + EXPECT_TRUE(span.isSetTraceIdHigh()); SpanContext span_context_high_id(span); // We currently drop the high bits. So, we expect the same context as above EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c", diff --git a/test/common/tracing/zipkin/tracer_test.cc b/test/common/tracing/zipkin/tracer_test.cc index c147e5a1c7fd3..b2870ddba0146 100644 --- a/test/common/tracing/zipkin/tracer_test.cc +++ b/test/common/tracing/zipkin/tracer_test.cc @@ -40,7 +40,7 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_NE(0ULL, root_span->traceId()); // trace id must be set EXPECT_EQ(root_span->traceId(), root_span->id()); // span id and trace id must be the same - EXPECT_FALSE(root_span->isSet().parent_id_); // no parent set + EXPECT_FALSE(root_span->isSetParentId()); // no parent set EXPECT_NE(0LL, root_span->timestamp()); // span's timestamp must be set // A CS annotation must have been added @@ -56,7 +56,7 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_EQ(dynamic_cast(&tracer), root_span->tracer()); // Duration is not set at span-creation time - EXPECT_FALSE(root_span->isSet().duration_); + EXPECT_FALSE(root_span->isSetDuration()); // ============== // Test the creation of a shared-context span --> SR @@ -80,11 +80,10 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_EQ(root_span->id(), server_side_shared_context_span->id()); // The parent should be the same as in the CS side (none in this case) - EXPECT_FALSE(server_side_shared_context_span->isSet().parent_id_); + EXPECT_FALSE(server_side_shared_context_span->isSetParentId()); // span timestamp should not be set (it was set in the CS side) - EXPECT_EQ(0LL, server_side_shared_context_span->timestamp()); - EXPECT_FALSE(server_side_shared_context_span->isSet().timestamp_); + EXPECT_FALSE(server_side_shared_context_span->isSetTimestamp()); // An SR annotation must have been added EXPECT_EQ(1ULL, server_side_shared_context_span->annotations().size()); @@ -99,7 +98,7 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_EQ(dynamic_cast(&tracer), server_side_shared_context_span->tracer()); // Duration is not set at span-creation time - EXPECT_FALSE(server_side_shared_context_span->isSet().duration_); + EXPECT_FALSE(server_side_shared_context_span->isSetDuration()); // ============== // Test the creation of a child span --> CS @@ -121,7 +120,7 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_NE(child_span->traceId(), child_span->id()); // parent should be the previous span - EXPECT_TRUE(child_span->isSet().parent_id_); + EXPECT_TRUE(child_span->isSetParentId()); EXPECT_EQ(server_side_shared_context_span->id(), child_span->parentId()); // span's timestamp must be set @@ -140,7 +139,7 @@ TEST(ZipkinTracerTest, spanCreation) { EXPECT_EQ(dynamic_cast(&tracer), child_span->tracer()); // Duration is not set at span-creation time - EXPECT_FALSE(child_span->isSet().duration_); + EXPECT_FALSE(child_span->isSetDuration()); } TEST(ZipkinTracerTest, finishSpan) { diff --git a/test/common/tracing/zipkin/zipkin_core_types_test.cc b/test/common/tracing/zipkin/zipkin_core_types_test.cc index fff28999702dd..c19465d5bc4be 100644 --- a/test/common/tracing/zipkin/zipkin_core_types_test.cc +++ b/test/common/tracing/zipkin/zipkin_core_types_test.cc @@ -272,23 +272,19 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { Span span; EXPECT_EQ(0ULL, span.id()); - EXPECT_EQ(0ULL, span.parentId()); EXPECT_EQ(0ULL, span.traceId()); - EXPECT_EQ(0ULL, span.traceIdHigh()); EXPECT_EQ("", span.name()); EXPECT_EQ(0ULL, span.annotations().size()); EXPECT_EQ(0ULL, span.binaryAnnotations().size()); EXPECT_EQ("0000000000000000", span.idAsHexString()); EXPECT_EQ("0000000000000000", span.parentIdAsHexString()); EXPECT_EQ("0000000000000000", span.traceIdAsHexString()); - EXPECT_EQ(0LL, span.timestamp()); - EXPECT_EQ(0LL, span.duration()); EXPECT_EQ(0LL, span.startTime()); - EXPECT_FALSE(span.isSet().debug_); - EXPECT_FALSE(span.isSet().duration_); - EXPECT_FALSE(span.isSet().parent_id_); - EXPECT_FALSE(span.isSet().timestamp_); - EXPECT_FALSE(span.isSet().trace_id_high_); + EXPECT_FALSE(span.debug()); + EXPECT_FALSE(span.isSetDuration()); + EXPECT_FALSE(span.isSetParentId()); + EXPECT_FALSE(span.isSetTimestamp()); + EXPECT_FALSE(span.isSetTraceIdHigh()); EXPECT_EQ(R"({"traceId":"0000000000000000","name":"","id":"0000000000000000",)" R"("annotations":[],"binaryAnnotations":[]})", span.toJson()); @@ -304,7 +300,7 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { span.setParentId(id); EXPECT_EQ(id, span.parentId()); EXPECT_EQ(id_hex, span.parentIdAsHexString()); - EXPECT_TRUE(span.isSet().parent_id_); + EXPECT_TRUE(span.isSetParentId()); id = Util::generateRandom64(); id_hex = Hex::uint64ToHex(id); @@ -316,13 +312,13 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { id_hex = Hex::uint64ToHex(id); span.setTraceIdHigh(id); EXPECT_EQ(id, span.traceIdHigh()); - EXPECT_TRUE(span.isSet().trace_id_high_); + EXPECT_TRUE(span.isSetTraceIdHigh()); int64_t timestamp = std::chrono::duration_cast( ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); span.setTimestamp(timestamp); EXPECT_EQ(timestamp, span.timestamp()); - EXPECT_TRUE(span.isSet().timestamp_); + EXPECT_TRUE(span.isSetTimestamp()); int64_t start_time = std::chrono::duration_cast( @@ -332,13 +328,13 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { span.setDuration(3000LL); EXPECT_EQ(3000LL, span.duration()); - EXPECT_TRUE(span.isSet().duration_); + EXPECT_TRUE(span.isSetDuration()); span.setName("span_name"); EXPECT_EQ("span_name", span.name()); span.setDebug(); - EXPECT_TRUE(span.isSet().debug_); + EXPECT_TRUE(span.debug()); Endpoint endpoint; Annotation ann; @@ -449,7 +445,6 @@ TEST(ZipkinCoreTypesSpanTest, copyConstructor) { EXPECT_EQ(span.id(), span2.id()); EXPECT_EQ(span.parentId(), span2.parentId()); EXPECT_EQ(span.traceId(), span2.traceId()); - EXPECT_EQ(span.traceIdHigh(), span2.traceIdHigh()); EXPECT_EQ(span.name(), span2.name()); EXPECT_EQ(span.annotations().size(), span2.annotations().size()); EXPECT_EQ(span.binaryAnnotations().size(), span2.binaryAnnotations().size()); @@ -459,11 +454,11 @@ TEST(ZipkinCoreTypesSpanTest, copyConstructor) { EXPECT_EQ(span.timestamp(), span2.timestamp()); EXPECT_EQ(span.duration(), span2.duration()); EXPECT_EQ(span.startTime(), span2.startTime()); - EXPECT_EQ(span.isSet().debug_, span2.isSet().debug_); - EXPECT_EQ(span.isSet().duration_, span2.isSet().duration_); - EXPECT_EQ(span.isSet().parent_id_, span2.isSet().parent_id_); - EXPECT_EQ(span.isSet().timestamp_, span2.isSet().timestamp_); - EXPECT_EQ(span.isSet().trace_id_high_, span2.isSet().trace_id_high_); + EXPECT_EQ(span.debug(), span2.debug()); + EXPECT_EQ(span.isSetDuration(), span2.isSetDuration()); + EXPECT_EQ(span.isSetParentId(), span2.isSetParentId()); + EXPECT_EQ(span.isSetTimestamp(), span2.isSetTimestamp()); + EXPECT_EQ(span.isSetTraceIdHigh(), span2.isSetTraceIdHigh()); } TEST(ZipkinCoreTypesSpanTest, assignmentOperator) { @@ -485,7 +480,6 @@ TEST(ZipkinCoreTypesSpanTest, assignmentOperator) { EXPECT_EQ(span.id(), span2.id()); EXPECT_EQ(span.parentId(), span2.parentId()); EXPECT_EQ(span.traceId(), span2.traceId()); - EXPECT_EQ(span.traceIdHigh(), span2.traceIdHigh()); EXPECT_EQ(span.name(), span2.name()); EXPECT_EQ(span.annotations().size(), span2.annotations().size()); EXPECT_EQ(span.binaryAnnotations().size(), span2.binaryAnnotations().size()); @@ -495,11 +489,11 @@ TEST(ZipkinCoreTypesSpanTest, assignmentOperator) { EXPECT_EQ(span.timestamp(), span2.timestamp()); EXPECT_EQ(span.duration(), span2.duration()); EXPECT_EQ(span.startTime(), span2.startTime()); - EXPECT_EQ(span.isSet().debug_, span2.isSet().debug_); - EXPECT_EQ(span.isSet().duration_, span2.isSet().duration_); - EXPECT_EQ(span.isSet().parent_id_, span2.isSet().parent_id_); - EXPECT_EQ(span.isSet().timestamp_, span2.isSet().timestamp_); - EXPECT_EQ(span.isSet().trace_id_high_, span2.isSet().trace_id_high_); + EXPECT_EQ(span.debug(), span2.debug()); + EXPECT_EQ(span.isSetDuration(), span2.isSetDuration()); + EXPECT_EQ(span.isSetParentId(), span2.isSetParentId()); + EXPECT_EQ(span.isSetTimestamp(), span2.isSetTimestamp()); + EXPECT_EQ(span.isSetTraceIdHigh(), span2.isSetTraceIdHigh()); } TEST(ZipkinCoreTypesSpanTest, setTag) { From a06e89bbe1e26c1d7abd2b41fb694a1d2d862a41 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Thu, 27 Apr 2017 21:34:51 -0400 Subject: [PATCH 47/55] Fixed BUILD files. Removed CMakeLists files. (#430) --- source/CMakeLists.txt | 3 - source/common/CMakeLists.txt | 143 ------------------- source/common/tracing/zipkin/BUILD | 1 + source/exe/CMakeLists.txt | 31 ----- test/CMakeLists.txt | 211 ----------------------------- test/common/tracing/zipkin/BUILD | 28 ++++ 6 files changed, 29 insertions(+), 388 deletions(-) delete mode 100644 source/CMakeLists.txt delete mode 100644 source/common/CMakeLists.txt delete mode 100644 source/exe/CMakeLists.txt delete mode 100644 test/CMakeLists.txt create mode 100755 test/common/tracing/zipkin/BUILD diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt deleted file mode 100644 index 52e699aadb358..0000000000000 --- a/source/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -add_subdirectory(common) -add_subdirectory(exe) -add_subdirectory(server) diff --git a/source/common/CMakeLists.txt b/source/common/CMakeLists.txt deleted file mode 100644 index f101acb25c3ae..0000000000000 --- a/source/common/CMakeLists.txt +++ /dev/null @@ -1,143 +0,0 @@ -set(gen_git_sha_target ${CMAKE_CURRENT_BINARY_DIR}/version_generated.cc) -add_custom_target( - gen_git_sha ALL - COMMAND ${PROJECT_SOURCE_DIR}/tools/gen_git_sha.sh ${CMAKE_SOURCE_DIR} ${gen_git_sha_target}) - -add_custom_command( - OUTPUT ${gen_git_sha_target} - DEPENDS gen_git_sha) - -file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/generated) -add_custom_command( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated/ratelimit.pb.h ${CMAKE_CURRENT_BINARY_DIR}/generated/ratelimit.pb.cc - COMMAND ${ENVOY_PROTOBUF_PROTOC} -I=${CMAKE_CURRENT_SOURCE_DIR}/ratelimit/ - --cpp_out=${CMAKE_CURRENT_BINARY_DIR}/generated - ${CMAKE_CURRENT_SOURCE_DIR}/ratelimit/ratelimit.proto - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/ratelimit/ratelimit.proto -) - -set_source_files_properties(generated/ratelimit.pb.cc PROPERTIES COMPILE_FLAGS -Wno-unused-parameter) - -add_library( - envoy-common OBJECT - api/api_impl.cc - access_log/access_log_manager_impl.cc - buffer/buffer_impl.cc - common/base64.cc - common/hex.cc - common/logger.cc - common/thread.cc - common/utility.cc - common/version.cc - dynamo/dynamo_filter.cc - dynamo/dynamo_request_parser.cc - dynamo/dynamo_utility.cc - event/dispatcher_impl.cc - event/event_impl_base.cc - event/file_event_impl.cc - event/libevent.cc - event/signal_impl.cc - event/timer_impl.cc - filesystem/filesystem_impl.cc - filesystem/watcher_impl.cc - filter/auth/client_ssl.cc - filter/echo.cc - filter/ratelimit.cc - filter/tcp_proxy.cc - generated/ratelimit.pb.cc - grpc/codec.cc - grpc/common.cc - grpc/http1_bridge_filter.cc - grpc/rpc_channel_impl.cc - http/access_log/access_log_formatter.cc - http/access_log/access_log_impl.cc - http/async_client_impl.cc - http/codec_client.cc - http/codes.cc - http/conn_manager_impl.cc - http/conn_manager_utility.cc - http/date_provider_impl.cc - http/header_map_impl.cc - http/message_impl.cc - http/http1/codec_impl.cc - http/http1/conn_pool.cc - http/http2/codec_impl.cc - http/http2/conn_pool.cc - http/filter/buffer_filter.cc - http/filter/fault_filter.cc - http/filter/ratelimit.cc - http/rest_api_fetcher.cc - http/user_agent.cc - http/utility.cc - json/config_schemas.cc - json/json_loader.cc - memory/stats.cc - mongo/bson_impl.cc - mongo/codec_impl.cc - mongo/proxy.cc - mongo/utility.cc - network/address_impl.cc - network/cidr_range.cc - network/connection_impl.cc - network/dns_impl.cc - network/filter_manager_impl.cc - network/listener_impl.cc - network/listen_socket_impl.cc - network/proxy_protocol.cc - network/utility.cc - profiler/profiler.cc - ratelimit/ratelimit_impl.cc - redis/codec_impl.cc - redis/command_splitter_impl.cc - redis/conn_pool_impl.cc - redis/proxy_filter.cc - router/config_impl.cc - router/config_utility.cc - router/rds_impl.cc - router/retry_state_impl.cc - router/router.cc - router/router_ratelimit.cc - router/shadow_writer_impl.cc - runtime/runtime_impl.cc - runtime/uuid_util.cc - ssl/connection_impl.cc - ssl/context_config_impl.cc - ssl/context_impl.cc - ssl/context_manager_impl.cc - stats/stats_impl.cc - stats/statsd.cc - stats/thread_local_store.cc - thread_local/thread_local_impl.cc - tracing/http_tracer_impl.cc - tracing/lightstep_tracer_impl.cc - tracing/zipkin/span_buffer.cc - tracing/zipkin/span_context.cc - tracing/zipkin/tracer.cc - tracing/zipkin/util.cc - tracing/zipkin/zipkin_core_constants.cc - tracing/zipkin/zipkin_core_types.cc - tracing/zipkin/zipkin_json_field_names.cc - upstream/cds_api_impl.cc - upstream/cluster_manager_impl.cc - upstream/health_checker_impl.cc - upstream/host_utility.cc - upstream/load_balancer_impl.cc - upstream/logical_dns_cluster.cc - upstream/outlier_detection_impl.cc - upstream/ring_hash_lb.cc - upstream/sds.cc - upstream/upstream_impl.cc - ${gen_git_sha_target}) - -include_directories(SYSTEM ${ENVOY_HTTP_PARSER_INCLUDE_DIR}) -include_directories(${ENVOY_RAPIDJSON_INCLUDE_DIR}) - -if (NOT ENVOY_SANITIZE) - include_directories(${ENVOY_GPERFTOOLS_INCLUDE_DIR}) -endif() - -include_directories(${ENVOY_CARES_INCLUDE_DIR}) -include_directories(${ENVOY_LIBEVENT_INCLUDE_DIR}) -include_directories(${ENVOY_NGHTTP2_INCLUDE_DIR}) -include_directories(SYSTEM ${ENVOY_OPENSSL_INCLUDE_DIR}) -include_directories(SYSTEM ${ENVOY_LIGHTSTEP_TRACER_INCLUDE_DIR}) diff --git a/source/common/tracing/zipkin/BUILD b/source/common/tracing/zipkin/BUILD index 655ad362e2c6b..50943efa91221 100644 --- a/source/common/tracing/zipkin/BUILD +++ b/source/common/tracing/zipkin/BUILD @@ -25,6 +25,7 @@ envoy_cc_library( ], external_deps = ["rapidjson"], deps = [ + "//include/envoy/common:optional", "//include/envoy/common:time_interface", "//include/envoy/runtime:runtime_interface", "//source/common/common:hex_lib", diff --git a/source/exe/CMakeLists.txt b/source/exe/CMakeLists.txt deleted file mode 100644 index 0348c0226edf0..0000000000000 --- a/source/exe/CMakeLists.txt +++ /dev/null @@ -1,31 +0,0 @@ -add_executable(envoy hot_restart.cc main.cc $ $ ${ENVOY_EXE_EXTRA_OBJECTS}) - -if (ENVOY_TCMALLOC) - target_link_libraries(envoy tcmalloc_and_profiler) -endif() - -target_link_libraries(envoy event) -target_link_libraries(envoy event_pthreads) -target_link_libraries(envoy http_parser) -target_link_libraries(envoy ssl) -target_link_libraries(envoy crypto) -target_link_libraries(envoy nghttp2) -target_link_libraries(envoy lightstep_core_cxx11) -target_link_libraries(envoy cares) -target_link_libraries(envoy protobuf) -target_link_libraries(envoy pthread) -target_link_libraries(envoy anl) -target_link_libraries(envoy rt) -target_link_libraries(envoy dl) - -include_directories(SYSTEM ${ENVOY_OPENSSL_INCLUDE_DIR}) -include_directories(SYSTEM ${ENVOY_LIGHTSTEP_TRACER_INCLUDE_DIR}) -include_directories(SYSTEM ${ENVOY_CARES_INCLUDE_DIR}) - -if (ENVOY_STRIP) - add_custom_command(TARGET envoy POST_BUILD - COMMAND objcopy --only-keep-debug $ $.dbg - COMMAND strip $ - COMMAND objcopy --add-gnu-debuglink=$.dbg $ - ) -endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt deleted file mode 100644 index c452c6a034832..0000000000000 --- a/test/CMakeLists.txt +++ /dev/null @@ -1,211 +0,0 @@ -set(CMAKE_EXE_LINKER_FLAGS "${ENVOY_TEST_EXTRA_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS}") - -# TODO: Generic function for building protos -file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/generated) -add_custom_command( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated/helloworld.pb.h ${CMAKE_CURRENT_BINARY_DIR}/generated/helloworld.pb.cc - COMMAND ${ENVOY_PROTOBUF_PROTOC} -I=${CMAKE_CURRENT_SOURCE_DIR}/proto/ - --cpp_out=${CMAKE_CURRENT_BINARY_DIR}/generated - ${CMAKE_CURRENT_SOURCE_DIR}/proto/helloworld.proto - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/proto/helloworld.proto -) - -set_source_files_properties(generated/helloworld.pb.cc PROPERTIES COMPILE_FLAGS -Wno-unused-parameter) - -include_directories(${ENVOY_GMOCK_INCLUDE_DIR}) -include_directories(${ENVOY_GTEST_INCLUDE_DIR}) -include_directories(SYSTEM ${ENVOY_HTTP_PARSER_INCLUDE_DIR}) -include_directories(${PROJECT_SOURCE_DIR}) -include_directories(${PROJECT_BINARY_DIR}) -include_directories(SYSTEM ${ENVOY_OPENSSL_INCLUDE_DIR}) -include_directories(${ENVOY_NGHTTP2_INCLUDE_DIR}) -include_directories(SYSTEM ${ENVOY_CARES_INCLUDE_DIR}) -include_directories(SYSTEM ${ENVOY_LIGHTSTEP_TRACER_INCLUDE_DIR}) -include_directories(${ENVOY_LIBEVENT_INCLUDE_DIR}) - -add_executable(envoy-test - $ - $ - ${ENVOY_TEST_EXTRA_OBJECTS} - common/access_log/access_log_manager_impl_test.cc - common/api/api_impl_test.cc - common/common/base64_test.cc - common/common/hex_test.cc - common/common/optional_test.cc - common/common/utility_test.cc - common/event/dispatcher_impl_test.cc - common/event/file_event_impl_test.cc - common/filesystem/filesystem_impl_test.cc - common/filesystem/watcher_impl_test.cc - common/grpc/codec_test.cc - common/grpc/common_test.cc - common/grpc/http1_bridge_filter_test.cc - common/grpc/rpc_channel_impl_test.cc - common/http/access_log/access_log_formatter_test.cc - common/http/access_log/access_log_impl_test.cc - common/http/async_client_impl_test.cc - common/http/codec_client_test.cc - common/http/codes_test.cc - common/http/common.cc - common/http/conn_manager_impl_test.cc - common/http/conn_manager_utility_test.cc - common/http/date_provider_impl_test.cc - common/http/filter/buffer_filter_test.cc - common/http/filter/fault_filter_test.cc - common/http/filter/ratelimit_test.cc - common/http/header_map_impl_test.cc - common/http/http1/codec_impl_test.cc - common/http/http1/conn_pool_test.cc - common/http/http2/codec_impl_test.cc - common/http/http2/conn_pool_test.cc - common/http/user_agent_test.cc - common/http/utility_test.cc - common/filter/auth/client_ssl_test.cc - common/filter/ratelimit_test.cc - common/filter/tcp_proxy_test.cc - common/dynamo/dynamo_filter_test.cc - common/dynamo/dynamo_request_parser_test.cc - common/dynamo/dynamo_utility_test.cc - common/json/json_loader_test.cc - common/mongo/bson_impl_test.cc - common/mongo/codec_impl_test.cc - common/mongo/proxy_test.cc - common/mongo/utility_test.cc - common/network/address_impl_test.cc - common/network/cidr_range_test.cc - common/network/connection_impl_test.cc - common/network/dns_impl_test.cc - common/network/filter_manager_impl_test.cc - common/network/listener_impl_test.cc - common/network/listen_socket_impl_test.cc - common/network/proxy_protocol_test.cc - common/network/utility_test.cc - common/ratelimit/ratelimit_impl_test.cc - common/redis/codec_impl_test.cc - common/redis/command_splitter_impl_test.cc - common/redis/conn_pool_impl_test.cc - common/redis/proxy_filter_test.cc - common/router/config_impl_test.cc - common/router/rds_impl_test.cc - common/router/retry_state_impl_test.cc - common/router/router_test.cc - common/router/router_ratelimit_test.cc - common/router/shadow_writer_impl_test.cc - common/runtime/runtime_impl_test.cc - common/runtime/uuid_util_test.cc - common/ssl/connection_impl_test.cc - common/ssl/context_impl_test.cc - common/stats/stats_impl_test.cc - common/stats/statsd_test.cc - common/stats/thread_local_store_test.cc - common/tracing/http_tracer_impl_test.cc - common/tracing/lightstep_tracer_impl_test.cc - common/tracing/zipkin/span_buffer_test.cc - common/tracing/zipkin/span_context_test.cc - common/tracing/zipkin/util_test.cc - common/tracing/zipkin/tracer_test.cc - common/tracing/zipkin/zipkin_core_types_test.cc - common/upstream/cds_api_impl_test.cc - common/upstream/cluster_manager_impl_test.cc - common/upstream/health_checker_impl_test.cc - common/upstream/host_utility_test.cc - common/upstream/load_balancer_impl_test.cc - common/upstream/load_balancer_simulation_test.cc - common/upstream/logical_dns_cluster_test.cc - common/upstream/outlier_detection_impl_test.cc - common/upstream/resource_manager_impl_test.cc - common/upstream/ring_hash_lb_test.cc - common/upstream/sds_test.cc - common/upstream/upstream_impl_test.cc - generated/helloworld.pb.cc - integration/fake_upstream.cc - integration/http2_integration_test.cc - integration/http2_upstream_integration_test.cc - integration/integration.cc - integration/integration_test.cc - integration/integration_admin_test.cc - integration/proxy_proto_integration_test.cc - integration/ssl_integration_test.cc - integration/uds_integration_test.cc - integration/server.cc - integration/utility.cc - main.cc - mocks/access_log/mocks.cc - mocks/api/mocks.cc - mocks/common.cc - mocks/event/mocks.cc - mocks/filesystem/mocks.cc - mocks/grpc/mocks.cc - mocks/http/mocks.cc - mocks/init/mocks.cc - mocks/local_info/mocks.cc - mocks/network/mocks.cc - mocks/ratelimit/mocks.cc - mocks/redis/mocks.cc - mocks/router/mocks.cc - mocks/runtime/mocks.cc - mocks/server/mocks.cc - mocks/ssl/mocks.cc - mocks/stats/mocks.cc - mocks/thread_local/mocks.cc - mocks/tracing/mocks.cc - mocks/upstream/mocks.cc - server/config/http/config_test.cc - server/config/network/config_test.cc - server/config/network/http_connection_manager_test.cc - server/configuration_impl_test.cc - server/connection_handler_test.cc - server/drain_manager_impl_test.cc - server/guarddog_impl_test.cc - server/http/admin_test.cc - server/http/health_check_test.cc - server/options_impl_test.cc - server/server_test.cc - test_common/environment.cc - test_common/network_utility.cc - test_common/network_utility_test.cc - test_common/printers.cc - test_common/utility.cc) - -# The MOCK_METHOD* macros from gtest triggers this clang warning and it's hard -# to work around, so we just ignore it. -if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - set_target_properties(envoy-test PROPERTIES COMPILE_FLAGS "-Wno-inconsistent-missing-override") -endif() - -if (ENVOY_TCMALLOC) - target_link_libraries(envoy-test tcmalloc_and_profiler) -endif() - -target_link_libraries(envoy-test event) -target_link_libraries(envoy-test event_pthreads) -target_link_libraries(envoy-test http_parser) -target_link_libraries(envoy-test ssl) -target_link_libraries(envoy-test crypto) -target_link_libraries(envoy-test nghttp2) -target_link_libraries(envoy-test lightstep_core_cxx11) -target_link_libraries(envoy-test cares) -target_link_libraries(envoy-test protobuf) -target_link_libraries(envoy-test gmock) -target_link_libraries(envoy-test pthread) -target_link_libraries(envoy-test anl) -target_link_libraries(envoy-test dl) - -set(ENVOY_TEST_EXTRA_SETUP_SCRIPT "" CACHE STRING - "extra setup script to call from run_envoy_tests.sh after basic setup") - -add_custom_target( - ${PROJECT_NAME}.check ${PROJECT_SOURCE_DIR}/test/run_envoy_tests.sh ${PROJECT_SOURCE_DIR} - ${PROJECT_BINARY_DIR} ${ENVOY_TEST_EXTRA_SETUP_SCRIPT} - DEPENDS envoy envoy-test -) - -if (ENVOY_CODE_COVERAGE) -add_custom_target( - ${PROJECT_NAME}.check-coverage - DEPENDS envoy envoy-test - COMMAND ${PROJECT_SOURCE_DIR}/test/run_envoy_coverage.sh ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR} - ${PROJECT_SOURCE_DIR} ${PROJECT_BINARY_DIR} ${ENVOY_GCOVR} ${ENVOY_GCOVR_EXTRA_ARGS} - ${ENVOY_TEST_EXTRA_SETUP_SCRIPT} -) -endif() diff --git a/test/common/tracing/zipkin/BUILD b/test/common/tracing/zipkin/BUILD new file mode 100755 index 0000000000000..3d8769e6a1e5e --- /dev/null +++ b/test/common/tracing/zipkin/BUILD @@ -0,0 +1,28 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_test", + "envoy_package", +) + +envoy_package() + +envoy_cc_test( + name = "zipkin_test", + srcs = [ + "span_buffer_test.cc", + "span_context_test.cc", + "tracer_test.cc", + "util_test.cc", + "zipkin_core_types_test.cc" + ], + deps = [ + "//include/envoy/common:optional", + "//include/envoy/common:time_interface", + "//include/envoy/runtime:runtime_interface", + "//source/common/common:hex_lib", + "//source/common/common:utility_lib", + "//source/common/runtime:runtime_lib", + "//source/common/network:address_lib", + "//source/common/tracing/zipkin:zipkin_lib", + ], +) From be09c209ba593b116d02992294b2bdcf049b7015 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Thu, 27 Apr 2017 21:35:36 -0400 Subject: [PATCH 48/55] Fixed file permission (#430) --- test/common/tracing/zipkin/BUILD | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 test/common/tracing/zipkin/BUILD diff --git a/test/common/tracing/zipkin/BUILD b/test/common/tracing/zipkin/BUILD old mode 100755 new mode 100644 From cff6194d8c12ff0fc988e36e5d8833c0b42aba80 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Thu, 27 Apr 2017 21:56:45 -0400 Subject: [PATCH 49/55] Fixed format of BUILD file (#430) --- test/common/tracing/zipkin/BUILD | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/common/tracing/zipkin/BUILD b/test/common/tracing/zipkin/BUILD index 3d8769e6a1e5e..1cb7c2cd1e02b 100644 --- a/test/common/tracing/zipkin/BUILD +++ b/test/common/tracing/zipkin/BUILD @@ -13,7 +13,7 @@ envoy_cc_test( "span_context_test.cc", "tracer_test.cc", "util_test.cc", - "zipkin_core_types_test.cc" + "zipkin_core_types_test.cc", ], deps = [ "//include/envoy/common:optional", @@ -21,8 +21,8 @@ envoy_cc_test( "//include/envoy/runtime:runtime_interface", "//source/common/common:hex_lib", "//source/common/common:utility_lib", - "//source/common/runtime:runtime_lib", "//source/common/network:address_lib", + "//source/common/runtime:runtime_lib", "//source/common/tracing/zipkin:zipkin_lib", ], ) From d236cebb87060ca366865e555db323f6cd7e4be6 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Fri, 28 Apr 2017 17:52:23 -0400 Subject: [PATCH 50/55] Minor code improvements (#430) --- source/common/common/hex.cc | 3 +- source/common/tracing/zipkin/span_context.cc | 47 +++++++++++-------- source/common/tracing/zipkin/span_context.h | 17 +++++-- source/common/tracing/zipkin/util.cc | 6 +-- source/common/tracing/zipkin/util.h | 4 +- .../tracing/zipkin/zipkin_core_types.cc | 40 ++++++++-------- .../common/tracing/zipkin/zipkin_core_types.h | 13 ++--- test/common/tracing/zipkin/util_test.cc | 6 +-- 8 files changed, 77 insertions(+), 59 deletions(-) diff --git a/source/common/common/hex.cc b/source/common/common/hex.cc index b907eddda960d..158dac3d360ae 100644 --- a/source/common/common/hex.cc +++ b/source/common/common/hex.cc @@ -1,5 +1,6 @@ #include "common/common/hex.h" +#include #include #include #include @@ -45,7 +46,7 @@ std::vector Hex::decode(const std::string& hex_string) { } std::string Hex::uint64ToHex(uint64_t value) { - std::vector data(8); + std::array data; data[7] = (value & 0x00000000000000FF); data[6] = (value & 0x000000000000FF00) >> 8; diff --git a/source/common/tracing/zipkin/span_context.cc b/source/common/tracing/zipkin/span_context.cc index 623b40f1354ae..b8a2f595f4ea3 100644 --- a/source/common/tracing/zipkin/span_context.cc +++ b/source/common/tracing/zipkin/span_context.cc @@ -5,25 +5,34 @@ namespace Zipkin { -// String that separates the span-context fields in its string-serialized form. -const std::string SpanContext::FIELD_SEPARATOR_ = ";"; +// The static functions below are needed due to C++ inability to safely concatenate static strings +// belonging to different compilation units (the initialization order is not guaranteed). -// String value corresponding to an empty span context. -const std::string SpanContext::UNITIALIZED_SPAN_CONTEXT_ = "0000000000000000" + FIELD_SEPARATOR_ + - "0000000000000000" + FIELD_SEPARATOR_ + - "0000000000000000"; +const std::string& SpanContext::FIELD_SEPARATOR() { + static const std::string* field_separator = new std::string(";"); -// String with regular expression to match a 16-digit hexadecimal number. -const std::string SpanContext::HEX_DIGIT_GROUP_REGEX_STR_ = "([0-9,a-z]{16})"; + return *field_separator; +} -// The static functions below are needed due to C++ inability to safely concatenate static strings -// belonging to different compilation units (the initialization order is not guaranteed). +const std::string& SpanContext::UNITIALIZED_SPAN_CONTEXT() { + static const std::string* unitialized_span_context = + new std::string("0000000000000000" + FIELD_SEPARATOR() + "0000000000000000" + + FIELD_SEPARATOR() + "0000000000000000"); + + return *unitialized_span_context; +} + +const std::string& SpanContext::HEX_DIGIT_GROUP_REGEX_STR() { + static const std::string* hex_digit_group_regex_str = new std::string("([0-9,a-z]{16})"); + + return *hex_digit_group_regex_str; +} const std::string& SpanContext::SPAN_CONTEXT_REGEX_STR() { // ^([0-9,a-z]{16});([0-9,a-z]{16});([0-9,a-z]{16})((;(cs|sr|cr|ss))*)$ static const std::string* span_context_regex_str = new std::string( - "^" + HEX_DIGIT_GROUP_REGEX_STR_ + FIELD_SEPARATOR_ + HEX_DIGIT_GROUP_REGEX_STR_ + - FIELD_SEPARATOR_ + HEX_DIGIT_GROUP_REGEX_STR_ + "((" + FIELD_SEPARATOR_ + "(" + + "^" + HEX_DIGIT_GROUP_REGEX_STR() + FIELD_SEPARATOR() + HEX_DIGIT_GROUP_REGEX_STR() + + FIELD_SEPARATOR() + HEX_DIGIT_GROUP_REGEX_STR() + "((" + FIELD_SEPARATOR() + "(" + ZipkinCoreConstants::CLIENT_SEND + "|" + ZipkinCoreConstants::SERVER_RECV + "|" + ZipkinCoreConstants::CLIENT_RECV + "|" + ZipkinCoreConstants::SERVER_SEND + "))*)$"); @@ -61,24 +70,24 @@ SpanContext::SpanContext(const Span& span) { const std::string SpanContext::serializeToString() { if (!is_initialized_) { - return UNITIALIZED_SPAN_CONTEXT_; + return UNITIALIZED_SPAN_CONTEXT(); } std::string result; - result = traceIdAsHexString() + FIELD_SEPARATOR_ + idAsHexString() + FIELD_SEPARATOR_ + + result = traceIdAsHexString() + FIELD_SEPARATOR() + idAsHexString() + FIELD_SEPARATOR() + parentIdAsHexString(); if (annotation_values_.cr_) { - result += FIELD_SEPARATOR_ + ZipkinCoreConstants::CLIENT_RECV; + result += FIELD_SEPARATOR() + ZipkinCoreConstants::CLIENT_RECV; } if (annotation_values_.cs_) { - result += FIELD_SEPARATOR_ + ZipkinCoreConstants::CLIENT_SEND; + result += FIELD_SEPARATOR() + ZipkinCoreConstants::CLIENT_SEND; } if (annotation_values_.sr_) { - result += FIELD_SEPARATOR_ + ZipkinCoreConstants::SERVER_RECV; + result += FIELD_SEPARATOR() + ZipkinCoreConstants::SERVER_RECV; } if (annotation_values_.ss_) { - result += FIELD_SEPARATOR_ + ZipkinCoreConstants::SERVER_SEND; + result += FIELD_SEPARATOR() + ZipkinCoreConstants::SERVER_SEND; } return result; @@ -100,7 +109,7 @@ void SpanContext::populateFromString(const std::string& span_context_str) { std::string matched_annotations = match.str(4); if (matched_annotations.size() > 0) { std::vector annotation_value_strings = - StringUtil::split(matched_annotations, FIELD_SEPARATOR_); + StringUtil::split(matched_annotations, FIELD_SEPARATOR()); for (const std::string& annotation_value : annotation_value_strings) { if (annotation_value == ZipkinCoreConstants::CLIENT_RECV) { annotation_values_.cr_ = true; diff --git a/source/common/tracing/zipkin/span_context.h b/source/common/tracing/zipkin/span_context.h index 3e203b8128ebc..d98a00ee3a2d9 100644 --- a/source/common/tracing/zipkin/span_context.h +++ b/source/common/tracing/zipkin/span_context.h @@ -110,9 +110,20 @@ class SpanContext { AnnotationSet isSetAnnotation() const { return annotation_values_; } private: - static const std::string FIELD_SEPARATOR_; - static const std::string UNITIALIZED_SPAN_CONTEXT_; - static const std::string HEX_DIGIT_GROUP_REGEX_STR_; + /** + * @return String that separates the span-context fields in its string-serialized form. + */ + static const std::string& FIELD_SEPARATOR(); + + /** + * @return String value corresponding to an empty span context. + */ + static const std::string& UNITIALIZED_SPAN_CONTEXT(); + + /** + * @return String with regular expression to match a 16-digit hexadecimal number. + */ + static const std::string& HEX_DIGIT_GROUP_REGEX_STR(); /** * @return a string with a regular expression to match a valid string-serialized span context. diff --git a/source/common/tracing/zipkin/util.cc b/source/common/tracing/zipkin/util.cc index 6cbcee1281121..538537212e64d 100644 --- a/source/common/tracing/zipkin/util.cc +++ b/source/common/tracing/zipkin/util.cc @@ -30,15 +30,15 @@ void Util::mergeJsons(std::string& target, const std::string& source, target = sb.GetString(); } -void Util::addArrayToJson(std::string& target, const std::vector& json_array, +void Util::addArrayToJson(std::string& target, const std::vector& json_array, const std::string& field_name) { std::string stringified_json_array = "["; if (json_array.size() > 0) { - stringified_json_array += *(json_array[0]); + stringified_json_array += json_array[0]; for (auto it = json_array.begin() + 1; it != json_array.end(); it++) { stringified_json_array += ","; - stringified_json_array += **it; + stringified_json_array += *it; } } stringified_json_array += "]"; diff --git a/source/common/tracing/zipkin/util.h b/source/common/tracing/zipkin/util.h index 643b1dc1e3275..919de8cc721bf 100644 --- a/source/common/tracing/zipkin/util.h +++ b/source/common/tracing/zipkin/util.h @@ -28,11 +28,11 @@ class Util { * Merges a stringified JSON and a vector of stringified JSONs. * * @param target It will contain the resulting stringified JSON. - * @param json_array Vector of string pointers, where each element references a stringified JSON. + * @param json_array Vector of strings, where each element is a stringified JSON. * @param field_name The key name (added to target's JSON) whose value will be a stringified. * JSON array derived from json_array. */ - static void addArrayToJson(std::string& target, const std::vector& json_array, + static void addArrayToJson(std::string& target, const std::vector& json_array, const std::string& field_name); // ==== diff --git a/source/common/tracing/zipkin/zipkin_core_types.cc b/source/common/tracing/zipkin/zipkin_core_types.cc index c6e13ad15ecc5..7fd57c59d6c78 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.cc +++ b/source/common/tracing/zipkin/zipkin_core_types.cc @@ -24,7 +24,7 @@ Endpoint& Endpoint::operator=(const Endpoint& ep) { return *this; } -const std::string& Endpoint::toJson() { +const std::string Endpoint::toJson() { rapidjson::StringBuffer s; rapidjson::Writer writer(s); writer.StartObject(); @@ -48,9 +48,9 @@ const std::string& Endpoint::toJson() { writer.Key(ZipkinJsonFieldNames::ENDPOINT_SERVICE_NAME.c_str()); writer.String(service_name_.c_str()); writer.EndObject(); - json_string_ = s.GetString(); + std::string json_string = s.GetString(); - return json_string_; + return json_string; } Annotation::Annotation(const Annotation& ann) { @@ -71,7 +71,7 @@ Annotation& Annotation::operator=(const Annotation& ann) { return *this; } -const std::string& Annotation::toJson() { +const std::string Annotation::toJson() { rapidjson::StringBuffer s; rapidjson::Writer writer(s); writer.StartObject(); @@ -81,14 +81,14 @@ const std::string& Annotation::toJson() { writer.String(value_.c_str()); writer.EndObject(); - json_string_ = s.GetString(); + std::string json_string = s.GetString(); if (endpoint_.valid()) { - Util::mergeJsons(json_string_, static_cast(endpoint_.value()).toJson(), + Util::mergeJsons(json_string, static_cast(endpoint_.value()).toJson(), ZipkinJsonFieldNames::ANNOTATION_ENDPOINT.c_str()); } - return json_string_; + return json_string; } BinaryAnnotation::BinaryAnnotation(const BinaryAnnotation& ann) { @@ -111,7 +111,7 @@ BinaryAnnotation& BinaryAnnotation::operator=(const BinaryAnnotation& ann) { return *this; } -const std::string& BinaryAnnotation::toJson() { +const std::string BinaryAnnotation::toJson() { rapidjson::StringBuffer s; rapidjson::Writer writer(s); writer.StartObject(); @@ -121,14 +121,14 @@ const std::string& BinaryAnnotation::toJson() { writer.String(value_.c_str()); writer.EndObject(); - json_string_ = s.GetString(); + std::string json_string = s.GetString(); if (endpoint_.valid()) { - Util::mergeJsons(json_string_, static_cast(endpoint_.value()).toJson(), + Util::mergeJsons(json_string, static_cast(endpoint_.value()).toJson(), ZipkinJsonFieldNames::BINARY_ANNOTATION_ENDPOINT.c_str()); } - return json_string_; + return json_string; } const std::string Span::EMPTY_HEX_STRING_ = "0000000000000000"; @@ -181,7 +181,7 @@ Span& Span::operator=(const Span& span) { return *this; } -const std::string& Span::toJson() { +const std::string Span::toJson() { rapidjson::StringBuffer s; rapidjson::Writer writer(s); writer.StartObject(); @@ -209,24 +209,24 @@ const std::string& Span::toJson() { writer.EndObject(); - json_string_ = s.GetString(); + std::string json_string = s.GetString(); - std::vector annotation_json_vector; + std::vector annotation_json_vector; for (auto it = annotations_.begin(); it != annotations_.end(); it++) { - annotation_json_vector.push_back(&(it->toJson())); + annotation_json_vector.push_back(it->toJson()); } - Util::addArrayToJson(json_string_, annotation_json_vector, + Util::addArrayToJson(json_string, annotation_json_vector, ZipkinJsonFieldNames::SPAN_ANNOTATIONS.c_str()); - std::vector binary_annotation_json_vector; + std::vector binary_annotation_json_vector; for (auto it = binary_annotations_.begin(); it != binary_annotations_.end(); it++) { - binary_annotation_json_vector.push_back(&(it->toJson())); + binary_annotation_json_vector.push_back(it->toJson()); } - Util::addArrayToJson(json_string_, binary_annotation_json_vector, + Util::addArrayToJson(json_string, binary_annotation_json_vector, ZipkinJsonFieldNames::SPAN_BINARY_ANNOTATIONS.c_str()); - return json_string_; + return json_string; } void Span::finish() { diff --git a/source/common/tracing/zipkin/zipkin_core_types.h b/source/common/tracing/zipkin/zipkin_core_types.h index 139c4a646e544..c9cedde6d4781 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.h +++ b/source/common/tracing/zipkin/zipkin_core_types.h @@ -27,10 +27,7 @@ class ZipkinBase { * All classes defining Zipkin abstractions need to implement this method to convert * the corresponding abstraction to a Zipkin-compliant JSON. */ - virtual const std::string& toJson() PURE; - -protected: - std::string json_string_; + virtual const std::string toJson() PURE; }; /** @@ -88,7 +85,7 @@ class Endpoint : public ZipkinBase { * * @return a stringified JSON. */ - const std::string& toJson() override; + const std::string toJson() override; private: std::string service_name_; @@ -173,7 +170,7 @@ class Annotation : public ZipkinBase { * * @return a stringified JSON. */ - const std::string& toJson() override; + const std::string toJson() override; private: uint64_t timestamp_; @@ -272,7 +269,7 @@ class BinaryAnnotation : public ZipkinBase { * * @return a stringified JSON. */ - const std::string& toJson() override; + const std::string toJson() override; private: std::string key_; @@ -484,7 +481,7 @@ class Span : public ZipkinBase { * * @return a stringified JSON. */ - const std::string& toJson() override; + const std::string toJson() override; /** * Associates a Tracer object with the span. The tracer's reportSpan() method is invoked diff --git a/test/common/tracing/zipkin/util_test.cc b/test/common/tracing/zipkin/util_test.cc index ed664a201233a..23275edb78647 100644 --- a/test/common/tracing/zipkin/util_test.cc +++ b/test/common/tracing/zipkin/util_test.cc @@ -22,7 +22,7 @@ TEST(ZipkinUtilTest, utilTests) { // Test adding an array to a JSON - std::vector json_array; + std::vector json_array; Util::addArrayToJson(merged_json, json_array, "array_field"); expected_json = "{\"sub_json\":{\"field1\":\"val1\"},\"second_merge\":{\"sub_json\":{\"field1\":" "\"val1\"}},\"array_field\":[]}"; @@ -30,8 +30,8 @@ TEST(ZipkinUtilTest, utilTests) { std::string str1 = "{\"a1\":10}"; std::string str2 = "{\"a2\":\"10\"}"; - json_array.push_back(&str1); - json_array.push_back(&str2); + json_array.push_back(str1); + json_array.push_back(str2); Util::addArrayToJson(merged_json, json_array, "second_array"); expected_json = "{\"sub_json\":{\"field1\":\"val1\"},\"second_merge\":{\"sub_json\":{\"field1\":" "\"val1\"}},\"array_field\":[],\"second_array\":[{\"a1\":10},{\"a2\":\"10\"}]}"; From f0fda03762b5f6ed914b59feafd40d148903248d Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Fri, 28 Apr 2017 18:56:14 -0400 Subject: [PATCH 51/55] Fixed BUILD files (#430) --- source/common/tracing/zipkin/BUILD | 2 ++ test/common/tracing/zipkin/BUILD | 2 ++ 2 files changed, 4 insertions(+) diff --git a/source/common/tracing/zipkin/BUILD b/source/common/tracing/zipkin/BUILD index 50943efa91221..15945e8755c24 100644 --- a/source/common/tracing/zipkin/BUILD +++ b/source/common/tracing/zipkin/BUILD @@ -1,3 +1,5 @@ +licenses(["notice"]) # Apache 2 + package(default_visibility = ["//visibility:public"]) load("//bazel:envoy_build_system.bzl", "envoy_cc_library") diff --git a/test/common/tracing/zipkin/BUILD b/test/common/tracing/zipkin/BUILD index 1cb7c2cd1e02b..4782b9354741c 100644 --- a/test/common/tracing/zipkin/BUILD +++ b/test/common/tracing/zipkin/BUILD @@ -1,3 +1,5 @@ +licenses(["notice"]) # Apache 2 + load( "//bazel:envoy_build_system.bzl", "envoy_cc_test", From 4d424c989950a4268492323fc2f89cd8c359ee6b Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Mon, 1 May 2017 17:39:58 -0400 Subject: [PATCH 52/55] Minor code improvements (#430) --- source/common/common/hex.cc | 1 + source/common/tracing/zipkin/span_buffer.h | 2 +- source/common/tracing/zipkin/span_context.cc | 74 ++++++++----- source/common/tracing/zipkin/span_context.h | 34 +----- source/common/tracing/zipkin/tracer.cc | 5 +- .../tracing/zipkin/zipkin_core_types.cc | 4 +- .../tracing/zipkin/span_context_test.cc | 104 +++++++++--------- 7 files changed, 106 insertions(+), 118 deletions(-) diff --git a/source/common/common/hex.cc b/source/common/common/hex.cc index 158dac3d360ae..e615f711105a7 100644 --- a/source/common/common/hex.cc +++ b/source/common/common/hex.cc @@ -48,6 +48,7 @@ std::vector Hex::decode(const std::string& hex_string) { std::string Hex::uint64ToHex(uint64_t value) { std::array data; + // This is explicitly done for performance reasons data[7] = (value & 0x00000000000000FF); data[6] = (value & 0x000000000000FF00) >> 8; data[5] = (value & 0x0000000000FF0000) >> 16; diff --git a/source/common/tracing/zipkin/span_buffer.h b/source/common/tracing/zipkin/span_buffer.h index dd6249298bc4a..324965c75aeb9 100644 --- a/source/common/tracing/zipkin/span_buffer.h +++ b/source/common/tracing/zipkin/span_buffer.h @@ -21,7 +21,7 @@ class SpanBuffer { * * @param size The desired buffer size. */ - SpanBuffer(uint64_t size) { span_buffer_.reserve(size); } + SpanBuffer(uint64_t size) { allocateBuffer(size); } /** * Allocates space for an empty buffer or resizes a previously-allocated one. diff --git a/source/common/tracing/zipkin/span_context.cc b/source/common/tracing/zipkin/span_context.cc index b8a2f595f4ea3..33c7c37522cd7 100644 --- a/source/common/tracing/zipkin/span_context.cc +++ b/source/common/tracing/zipkin/span_context.cc @@ -5,45 +5,68 @@ namespace Zipkin { -// The static functions below are needed due to C++ inability to safely concatenate static strings -// belonging to different compilation units (the initialization order is not guaranteed). +namespace { +// The functions below are needed due to the "C++ static initialization order fiasco." -const std::string& SpanContext::FIELD_SEPARATOR() { +/** + * @return String that separates the span-context fields in its string-serialized form. + */ +static const std::string& fieldSeparator() { static const std::string* field_separator = new std::string(";"); return *field_separator; } -const std::string& SpanContext::UNITIALIZED_SPAN_CONTEXT() { +/** + * @return String value corresponding to an empty span context. + */ +static const std::string& unitializedSpanContext() { static const std::string* unitialized_span_context = - new std::string("0000000000000000" + FIELD_SEPARATOR() + "0000000000000000" + - FIELD_SEPARATOR() + "0000000000000000"); + new std::string("0000000000000000" + fieldSeparator() + "0000000000000000" + + fieldSeparator() + "0000000000000000"); return *unitialized_span_context; } -const std::string& SpanContext::HEX_DIGIT_GROUP_REGEX_STR() { +/** + * @return String with regular expression to match a 16-digit hexadecimal number. + */ +static const std::string& hexDigitGroupRegexStr() { static const std::string* hex_digit_group_regex_str = new std::string("([0-9,a-z]{16})"); return *hex_digit_group_regex_str; } -const std::string& SpanContext::SPAN_CONTEXT_REGEX_STR() { +/** + * @return a string with a regular expression to match a valid string-serialized span context. + * + * Note that a function is needed because we cannot concatenate static strings from + * different compilation units at initialization time (the initialization order is not + * guaranteed). In this case, the compilation units are ZipkinCoreConstants and SpanContext. + */ +static const std::string& spanContextRegexStr() { // ^([0-9,a-z]{16});([0-9,a-z]{16});([0-9,a-z]{16})((;(cs|sr|cr|ss))*)$ static const std::string* span_context_regex_str = new std::string( - "^" + HEX_DIGIT_GROUP_REGEX_STR() + FIELD_SEPARATOR() + HEX_DIGIT_GROUP_REGEX_STR() + - FIELD_SEPARATOR() + HEX_DIGIT_GROUP_REGEX_STR() + "((" + FIELD_SEPARATOR() + "(" + + "^" + hexDigitGroupRegexStr() + fieldSeparator() + hexDigitGroupRegexStr() + + fieldSeparator() + hexDigitGroupRegexStr() + "((" + fieldSeparator() + "(" + ZipkinCoreConstants::CLIENT_SEND + "|" + ZipkinCoreConstants::SERVER_RECV + "|" + ZipkinCoreConstants::CLIENT_RECV + "|" + ZipkinCoreConstants::SERVER_SEND + "))*)$"); return *span_context_regex_str; } -const std::regex& SpanContext::SPAN_CONTEXT_REGEX() { - static const std::regex span_context_regex(SPAN_CONTEXT_REGEX_STR()); +/** + * @return a regex to match a valid string-serialization of a span context. + * + * Note that a function is needed because the string used to build the regex + * cannot be initialized statically. + */ +static const std::regex& spanContextRegex() { + static const std::regex* span_context_regex = new std::regex(spanContextRegexStr()); - return span_context_regex; + return *span_context_regex; } +} // namespace SpanContext::SpanContext(const Span& span) { trace_id_ = span.traceId(); @@ -53,14 +76,11 @@ SpanContext::SpanContext(const Span& span) { for (const Annotation& annotation : span.annotations()) { if (annotation.value() == ZipkinCoreConstants::CLIENT_RECV) { annotation_values_.cr_ = true; - } - if (annotation.value() == ZipkinCoreConstants::CLIENT_SEND) { + } else if (annotation.value() == ZipkinCoreConstants::CLIENT_SEND) { annotation_values_.cs_ = true; - } - if (annotation.value() == ZipkinCoreConstants::SERVER_RECV) { + } else if (annotation.value() == ZipkinCoreConstants::SERVER_RECV) { annotation_values_.sr_ = true; - } - if (annotation.value() == ZipkinCoreConstants::SERVER_SEND) { + } else if (annotation.value() == ZipkinCoreConstants::SERVER_SEND) { annotation_values_.ss_ = true; } } @@ -70,24 +90,24 @@ SpanContext::SpanContext(const Span& span) { const std::string SpanContext::serializeToString() { if (!is_initialized_) { - return UNITIALIZED_SPAN_CONTEXT(); + return unitializedSpanContext(); } std::string result; - result = traceIdAsHexString() + FIELD_SEPARATOR() + idAsHexString() + FIELD_SEPARATOR() + + result = traceIdAsHexString() + fieldSeparator() + idAsHexString() + fieldSeparator() + parentIdAsHexString(); if (annotation_values_.cr_) { - result += FIELD_SEPARATOR() + ZipkinCoreConstants::CLIENT_RECV; + result += fieldSeparator() + ZipkinCoreConstants::CLIENT_RECV; } if (annotation_values_.cs_) { - result += FIELD_SEPARATOR() + ZipkinCoreConstants::CLIENT_SEND; + result += fieldSeparator() + ZipkinCoreConstants::CLIENT_SEND; } if (annotation_values_.sr_) { - result += FIELD_SEPARATOR() + ZipkinCoreConstants::SERVER_RECV; + result += fieldSeparator() + ZipkinCoreConstants::SERVER_RECV; } if (annotation_values_.ss_) { - result += FIELD_SEPARATOR() + ZipkinCoreConstants::SERVER_SEND; + result += fieldSeparator() + ZipkinCoreConstants::SERVER_SEND; } return result; @@ -100,7 +120,7 @@ void SpanContext::populateFromString(const std::string& span_context_str) { annotation_values_.cs_ = annotation_values_.cr_ = annotation_values_.ss_ = annotation_values_.sr_ = false; - if (std::regex_search(span_context_str, match, SPAN_CONTEXT_REGEX())) { + if (std::regex_search(span_context_str, match, spanContextRegex())) { // This is a valid string encoding of the context trace_id_ = std::stoull(match.str(1), nullptr, 16); id_ = std::stoull(match.str(2), nullptr, 16); @@ -109,7 +129,7 @@ void SpanContext::populateFromString(const std::string& span_context_str) { std::string matched_annotations = match.str(4); if (matched_annotations.size() > 0) { std::vector annotation_value_strings = - StringUtil::split(matched_annotations, FIELD_SEPARATOR()); + StringUtil::split(matched_annotations, fieldSeparator()); for (const std::string& annotation_value : annotation_value_strings) { if (annotation_value == ZipkinCoreConstants::CLIENT_RECV) { annotation_values_.cr_ = true; diff --git a/source/common/tracing/zipkin/span_context.h b/source/common/tracing/zipkin/span_context.h index d98a00ee3a2d9..10e5e5fdf2de6 100644 --- a/source/common/tracing/zipkin/span_context.h +++ b/source/common/tracing/zipkin/span_context.h @@ -107,41 +107,9 @@ class SpanContext { /** * @return a struct indicating which annotations are present in the span. */ - AnnotationSet isSetAnnotation() const { return annotation_values_; } + AnnotationSet annotationSet() const { return annotation_values_; } private: - /** - * @return String that separates the span-context fields in its string-serialized form. - */ - static const std::string& FIELD_SEPARATOR(); - - /** - * @return String value corresponding to an empty span context. - */ - static const std::string& UNITIALIZED_SPAN_CONTEXT(); - - /** - * @return String with regular expression to match a 16-digit hexadecimal number. - */ - static const std::string& HEX_DIGIT_GROUP_REGEX_STR(); - - /** - * @return a string with a regular expression to match a valid string-serialized span context. - * - * Note that a function is needed because we cannot concatenate static strings from - * different compilation units at initialization time (the initialization order is not - * guaranteed). In this case, the compilation units are ZipkinCoreConstants and SpanContext. - */ - static const std::string& SPAN_CONTEXT_REGEX_STR(); - - /** - * @return a regex to match a valid string-serialization of a span context. - * - * Note that a function is needed because the string used to build the regex - * cannot be initialized statically. - */ - static const std::regex& SPAN_CONTEXT_REGEX(); - uint64_t trace_id_; uint64_t id_; uint64_t parent_id_; diff --git a/source/common/tracing/zipkin/tracer.cc b/source/common/tracing/zipkin/tracer.cc index 17b86279de10a..df064827d64f6 100644 --- a/source/common/tracing/zipkin/tracer.cc +++ b/source/common/tracing/zipkin/tracer.cc @@ -51,7 +51,7 @@ SpanPtr Tracer::startSpan(const std::string& span_name, MonotonicTime start_time timestamp_micro = std::chrono::duration_cast( ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); - if ((previous_context.isSetAnnotation().sr_) && (!previous_context.isSetAnnotation().cs_)) { + if ((previous_context.annotationSet().sr_) && (!previous_context.annotationSet().cs_)) { // We need to create a new span that is a child of the previous span; no shared context // Create a new span id @@ -68,8 +68,7 @@ SpanPtr Tracer::startSpan(const std::string& span_name, MonotonicTime start_time // Set the timestamp globally for the span span_ptr->setTimestamp(timestamp_micro); - } else if ((previous_context.isSetAnnotation().cs_) && - (!previous_context.isSetAnnotation().sr_)) { + } else if ((previous_context.annotationSet().cs_) && (!previous_context.annotationSet().sr_)) { // We need to create a new span that will share context with the previous span // Initialize the shared context for the new span diff --git a/source/common/tracing/zipkin/zipkin_core_types.cc b/source/common/tracing/zipkin/zipkin_core_types.cc index 7fd57c59d6c78..5a52e238d2e65 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.cc +++ b/source/common/tracing/zipkin/zipkin_core_types.cc @@ -232,7 +232,7 @@ const std::string Span::toJson() { void Span::finish() { // Assumption: Span will have only one annotation when this method is called SpanContext context(*this); - if ((context.isSetAnnotation().sr_) && (!context.isSetAnnotation().ss_)) { + if ((context.annotationSet().sr_) && (!context.annotationSet().ss_)) { // Need to set the SS annotation Annotation ss; ss.setEndpoint(annotations_[0].endpoint()); @@ -240,7 +240,7 @@ void Span::finish() { ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count()); ss.setValue(ZipkinCoreConstants::SERVER_SEND); annotations_.push_back(std::move(ss)); - } else if ((context.isSetAnnotation().cs_) && (!context.isSetAnnotation().cr_)) { + } else if ((context.annotationSet().cs_) && (!context.annotationSet().cr_)) { // Need to set the CR annotation Annotation cr; uint64_t stop_timestamp = diff --git a/test/common/tracing/zipkin/span_context_test.cc b/test/common/tracing/zipkin/span_context_test.cc index 198df0bc2b42b..f4f3cb818a98c 100644 --- a/test/common/tracing/zipkin/span_context_test.cc +++ b/test/common/tracing/zipkin/span_context_test.cc @@ -15,10 +15,10 @@ TEST(ZipkinSpanContextTest, populateFromString) { EXPECT_EQ("0000000000000000", span_context.idAsHexString()); EXPECT_EQ(0ULL, span_context.parent_id()); EXPECT_EQ("0000000000000000", span_context.parentIdAsHexString()); - EXPECT_FALSE(span_context.isSetAnnotation().cr_); - EXPECT_FALSE(span_context.isSetAnnotation().cs_); - EXPECT_FALSE(span_context.isSetAnnotation().sr_); - EXPECT_FALSE(span_context.isSetAnnotation().ss_); + EXPECT_FALSE(span_context.annotationSet().cr_); + EXPECT_FALSE(span_context.annotationSet().cs_); + EXPECT_FALSE(span_context.annotationSet().sr_); + EXPECT_FALSE(span_context.annotationSet().ss_); EXPECT_EQ("0000000000000000;0000000000000000;0000000000000000", span_context.serializeToString()); // Span context populated with trace id, id, parent id, and no annotations @@ -29,10 +29,10 @@ TEST(ZipkinSpanContextTest, populateFromString) { EXPECT_EQ("56707c7b3e1092af", span_context.idAsHexString()); EXPECT_EQ(14164264937399213340ULL, span_context.parent_id()); EXPECT_EQ("c49193ea42335d1c", span_context.parentIdAsHexString()); - EXPECT_FALSE(span_context.isSetAnnotation().cr_); - EXPECT_FALSE(span_context.isSetAnnotation().cs_); - EXPECT_FALSE(span_context.isSetAnnotation().sr_); - EXPECT_FALSE(span_context.isSetAnnotation().ss_); + EXPECT_FALSE(span_context.annotationSet().cr_); + EXPECT_FALSE(span_context.annotationSet().cs_); + EXPECT_FALSE(span_context.annotationSet().sr_); + EXPECT_FALSE(span_context.annotationSet().ss_); EXPECT_EQ("25c6f38dd0600e79;56707c7b3e1092af;c49193ea42335d1c", span_context.serializeToString()); // Span context populated with trace id, id, parent id, and one annotation @@ -43,10 +43,10 @@ TEST(ZipkinSpanContextTest, populateFromString) { EXPECT_EQ("56707c7b3e1092af", span_context.idAsHexString()); EXPECT_EQ(14164264937399213340ULL, span_context.parent_id()); EXPECT_EQ("c49193ea42335d1c", span_context.parentIdAsHexString()); - EXPECT_FALSE(span_context.isSetAnnotation().cr_); - EXPECT_TRUE(span_context.isSetAnnotation().cs_); - EXPECT_FALSE(span_context.isSetAnnotation().sr_); - EXPECT_FALSE(span_context.isSetAnnotation().ss_); + EXPECT_FALSE(span_context.annotationSet().cr_); + EXPECT_TRUE(span_context.annotationSet().cs_); + EXPECT_FALSE(span_context.annotationSet().sr_); + EXPECT_FALSE(span_context.annotationSet().ss_); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cs", span_context.serializeToString()); @@ -58,10 +58,10 @@ TEST(ZipkinSpanContextTest, populateFromString) { EXPECT_EQ("56707c7b3e1092af", span_context.idAsHexString()); EXPECT_EQ(14164264937399213340ULL, span_context.parent_id()); EXPECT_EQ("c49193ea42335d1c", span_context.parentIdAsHexString()); - EXPECT_TRUE(span_context.isSetAnnotation().cr_); - EXPECT_TRUE(span_context.isSetAnnotation().cs_); - EXPECT_FALSE(span_context.isSetAnnotation().sr_); - EXPECT_FALSE(span_context.isSetAnnotation().ss_); + EXPECT_TRUE(span_context.annotationSet().cr_); + EXPECT_TRUE(span_context.annotationSet().cs_); + EXPECT_FALSE(span_context.annotationSet().sr_); + EXPECT_FALSE(span_context.annotationSet().ss_); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cr;cs", span_context.serializeToString()); @@ -73,10 +73,10 @@ TEST(ZipkinSpanContextTest, populateFromString) { EXPECT_EQ("56707c7b3e1092af", span_context.idAsHexString()); EXPECT_EQ(14164264937399213340ULL, span_context.parent_id()); EXPECT_EQ("c49193ea42335d1c", span_context.parentIdAsHexString()); - EXPECT_TRUE(span_context.isSetAnnotation().cr_); - EXPECT_TRUE(span_context.isSetAnnotation().cs_); - EXPECT_FALSE(span_context.isSetAnnotation().sr_); - EXPECT_TRUE(span_context.isSetAnnotation().ss_); + EXPECT_TRUE(span_context.annotationSet().cr_); + EXPECT_TRUE(span_context.annotationSet().cs_); + EXPECT_FALSE(span_context.annotationSet().sr_); + EXPECT_TRUE(span_context.annotationSet().ss_); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cr;cs;ss", span_context.serializeToString()); @@ -88,10 +88,10 @@ TEST(ZipkinSpanContextTest, populateFromString) { EXPECT_EQ("56707c7b3e1092af", span_context.idAsHexString()); EXPECT_EQ(14164264937399213340ULL, span_context.parent_id()); EXPECT_EQ("c49193ea42335d1c", span_context.parentIdAsHexString()); - EXPECT_TRUE(span_context.isSetAnnotation().cr_); - EXPECT_TRUE(span_context.isSetAnnotation().cs_); - EXPECT_TRUE(span_context.isSetAnnotation().sr_); - EXPECT_TRUE(span_context.isSetAnnotation().ss_); + EXPECT_TRUE(span_context.annotationSet().cr_); + EXPECT_TRUE(span_context.annotationSet().cs_); + EXPECT_TRUE(span_context.annotationSet().sr_); + EXPECT_TRUE(span_context.annotationSet().ss_); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cr;cs;sr;ss", span_context.serializeToString()); @@ -103,10 +103,10 @@ TEST(ZipkinSpanContextTest, populateFromString) { EXPECT_EQ("0000000000000000", span_context.idAsHexString()); EXPECT_EQ(0ULL, span_context.parent_id()); EXPECT_EQ("0000000000000000", span_context.parentIdAsHexString()); - EXPECT_FALSE(span_context.isSetAnnotation().cr_); - EXPECT_FALSE(span_context.isSetAnnotation().cs_); - EXPECT_FALSE(span_context.isSetAnnotation().sr_); - EXPECT_FALSE(span_context.isSetAnnotation().ss_); + EXPECT_FALSE(span_context.annotationSet().cr_); + EXPECT_FALSE(span_context.annotationSet().cs_); + EXPECT_FALSE(span_context.annotationSet().sr_); + EXPECT_FALSE(span_context.annotationSet().ss_); EXPECT_EQ("0000000000000000;0000000000000000;0000000000000000", span_context.serializeToString()); } @@ -121,10 +121,10 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { EXPECT_EQ("0000000000000000", span_context.idAsHexString()); EXPECT_EQ(0ULL, span_context.parent_id()); EXPECT_EQ("0000000000000000", span_context.parentIdAsHexString()); - EXPECT_FALSE(span_context.isSetAnnotation().cr_); - EXPECT_FALSE(span_context.isSetAnnotation().cs_); - EXPECT_FALSE(span_context.isSetAnnotation().sr_); - EXPECT_FALSE(span_context.isSetAnnotation().ss_); + EXPECT_FALSE(span_context.annotationSet().cr_); + EXPECT_FALSE(span_context.annotationSet().cs_); + EXPECT_FALSE(span_context.annotationSet().sr_); + EXPECT_FALSE(span_context.annotationSet().ss_); EXPECT_EQ("0000000000000000;0000000000000000;0000000000000000", span_context.serializeToString()); // Span context populated with trace id, id, parent id, and no annotations @@ -138,10 +138,10 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { EXPECT_EQ("56707c7b3e1092af", span_context_2.idAsHexString()); EXPECT_EQ(14164264937399213340ULL, span_context_2.parent_id()); EXPECT_EQ("c49193ea42335d1c", span_context_2.parentIdAsHexString()); - EXPECT_FALSE(span_context_2.isSetAnnotation().cr_); - EXPECT_FALSE(span_context_2.isSetAnnotation().cs_); - EXPECT_FALSE(span_context_2.isSetAnnotation().sr_); - EXPECT_FALSE(span_context_2.isSetAnnotation().ss_); + EXPECT_FALSE(span_context_2.annotationSet().cr_); + EXPECT_FALSE(span_context_2.annotationSet().cs_); + EXPECT_FALSE(span_context_2.annotationSet().sr_); + EXPECT_FALSE(span_context_2.annotationSet().ss_); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c", span_context_2.serializeToString()); @@ -165,10 +165,10 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { EXPECT_EQ("56707c7b3e1092af", span_context_3.idAsHexString()); EXPECT_EQ(14164264937399213340ULL, span_context_3.parent_id()); EXPECT_EQ("c49193ea42335d1c", span_context_3.parentIdAsHexString()); - EXPECT_FALSE(span_context_3.isSetAnnotation().cr_); - EXPECT_FALSE(span_context_3.isSetAnnotation().cs_); - EXPECT_TRUE(span_context_3.isSetAnnotation().sr_); - EXPECT_FALSE(span_context_3.isSetAnnotation().ss_); + EXPECT_FALSE(span_context_3.annotationSet().cr_); + EXPECT_FALSE(span_context_3.annotationSet().cs_); + EXPECT_TRUE(span_context_3.annotationSet().sr_); + EXPECT_FALSE(span_context_3.annotationSet().ss_); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;sr", span_context_3.serializeToString()); @@ -182,10 +182,10 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { EXPECT_EQ("56707c7b3e1092af", span_context_4.idAsHexString()); EXPECT_EQ(14164264937399213340ULL, span_context_4.parent_id()); EXPECT_EQ("c49193ea42335d1c", span_context_4.parentIdAsHexString()); - EXPECT_FALSE(span_context_4.isSetAnnotation().cr_); - EXPECT_FALSE(span_context_4.isSetAnnotation().cs_); - EXPECT_TRUE(span_context_4.isSetAnnotation().sr_); - EXPECT_TRUE(span_context_4.isSetAnnotation().ss_); + EXPECT_FALSE(span_context_4.annotationSet().cr_); + EXPECT_FALSE(span_context_4.annotationSet().cs_); + EXPECT_TRUE(span_context_4.annotationSet().sr_); + EXPECT_TRUE(span_context_4.annotationSet().ss_); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;sr;ss", span_context_4.serializeToString()); @@ -199,10 +199,10 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { EXPECT_EQ("56707c7b3e1092af", span_context_5.idAsHexString()); EXPECT_EQ(14164264937399213340ULL, span_context_5.parent_id()); EXPECT_EQ("c49193ea42335d1c", span_context_5.parentIdAsHexString()); - EXPECT_FALSE(span_context_5.isSetAnnotation().cr_); - EXPECT_TRUE(span_context_5.isSetAnnotation().cs_); - EXPECT_TRUE(span_context_5.isSetAnnotation().sr_); - EXPECT_TRUE(span_context_5.isSetAnnotation().ss_); + EXPECT_FALSE(span_context_5.annotationSet().cr_); + EXPECT_TRUE(span_context_5.annotationSet().cs_); + EXPECT_TRUE(span_context_5.annotationSet().sr_); + EXPECT_TRUE(span_context_5.annotationSet().ss_); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cs;sr;ss", span_context_5.serializeToString()); @@ -225,10 +225,10 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { EXPECT_EQ("56707c7b3e1092af", span_context_6.idAsHexString()); EXPECT_EQ(14164264937399213340ULL, span_context_6.parent_id()); EXPECT_EQ("c49193ea42335d1c", span_context_6.parentIdAsHexString()); - EXPECT_TRUE(span_context_6.isSetAnnotation().cr_); - EXPECT_TRUE(span_context_6.isSetAnnotation().cs_); - EXPECT_TRUE(span_context_6.isSetAnnotation().sr_); - EXPECT_TRUE(span_context_6.isSetAnnotation().ss_); + EXPECT_TRUE(span_context_6.annotationSet().cr_); + EXPECT_TRUE(span_context_6.annotationSet().cs_); + EXPECT_TRUE(span_context_6.annotationSet().sr_); + EXPECT_TRUE(span_context_6.annotationSet().ss_); EXPECT_EQ("25c6f38dd0600e78;56707c7b3e1092af;c49193ea42335d1c;cr;cs;sr;ss", span_context_6.serializeToString()); } From 11318bb069613ac357a588d5db679b8c02a278bd Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Mon, 1 May 2017 18:16:23 -0400 Subject: [PATCH 53/55] Use MockMonotonicTimeSource and MockRandomGenerator (#430) --- test/common/tracing/zipkin/BUILD | 3 ++- test/common/tracing/zipkin/tracer_test.cc | 12 ++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/test/common/tracing/zipkin/BUILD b/test/common/tracing/zipkin/BUILD index 4782b9354741c..fdd3829f93895 100644 --- a/test/common/tracing/zipkin/BUILD +++ b/test/common/tracing/zipkin/BUILD @@ -24,7 +24,8 @@ envoy_cc_test( "//source/common/common:hex_lib", "//source/common/common:utility_lib", "//source/common/network:address_lib", - "//source/common/runtime:runtime_lib", "//source/common/tracing/zipkin:zipkin_lib", + "//test/mocks:common_lib", + "//test/mocks/runtime:runtime_mocks", ], ) diff --git a/test/common/tracing/zipkin/tracer_test.cc b/test/common/tracing/zipkin/tracer_test.cc index b2870ddba0146..46bd9041e4c5e 100644 --- a/test/common/tracing/zipkin/tracer_test.cc +++ b/test/common/tracing/zipkin/tracer_test.cc @@ -1,10 +1,12 @@ #include "common/common/utility.h" #include "common/network/address_impl.h" -#include "common/runtime/runtime_impl.h" #include "common/tracing/zipkin/tracer.h" #include "common/tracing/zipkin/util.h" #include "common/tracing/zipkin/zipkin_core_constants.h" +#include "test/mocks/common.h" +#include "test/mocks/runtime/mocks.h" + #include "gtest/gtest.h" namespace Zipkin { @@ -25,7 +27,8 @@ TEST(ZipkinTracerTest, spanCreation) { Network::Address::InstanceConstSharedPtr addr = Network::Address::parseInternetAddressAndPort("127.0.0.1:9000"); Tracer tracer("my_service_name", addr); - MonotonicTime start_time = ProdMonotonicTimeSource::instance_.currentTime(); + MockMonotonicTimeSource mock_start_time; + MonotonicTime start_time = mock_start_time.currentTime(); // ============== // Test the creation of a root span --> CS @@ -146,8 +149,9 @@ TEST(ZipkinTracerTest, finishSpan) { Network::Address::InstanceConstSharedPtr addr = Network::Address::parseInternetAddressAndPort("127.0.0.1:9000"); Tracer tracer("my_service_name", addr); - tracer.setRandomGenerator(Runtime::RandomGeneratorPtr(new Runtime::RandomGeneratorImpl())); - MonotonicTime start_time = ProdMonotonicTimeSource::instance_.currentTime(); + tracer.setRandomGenerator(Runtime::RandomGeneratorPtr(new Runtime::MockRandomGenerator())); + MockMonotonicTimeSource mock_start_time; + MonotonicTime start_time = mock_start_time.currentTime(); // ============== // Test finishing a span containing a CS annotation From e27d5976028cbd76b53baf35a97dbda88c65fa64 Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Mon, 1 May 2017 18:46:09 -0400 Subject: [PATCH 54/55] Use the Singleton template for string constants. Minor improvements. (#430) --- source/common/tracing/zipkin/BUILD | 2 +- source/common/tracing/zipkin/tracer.cc | 4 +- .../tracing/zipkin/zipkin_core_types.cc | 53 +++++++++---------- .../tracing/zipkin/zipkin_json_field_names.cc | 26 --------- .../tracing/zipkin/zipkin_json_field_names.h | 49 +++++++++-------- 5 files changed, 56 insertions(+), 78 deletions(-) delete mode 100644 source/common/tracing/zipkin/zipkin_json_field_names.cc diff --git a/source/common/tracing/zipkin/BUILD b/source/common/tracing/zipkin/BUILD index 15945e8755c24..a674874beae0d 100644 --- a/source/common/tracing/zipkin/BUILD +++ b/source/common/tracing/zipkin/BUILD @@ -13,7 +13,6 @@ envoy_cc_library( "util.cc", "zipkin_core_constants.cc", "zipkin_core_types.cc", - "zipkin_json_field_names.cc", ], hdrs = [ "span_buffer.h", @@ -31,6 +30,7 @@ envoy_cc_library( "//include/envoy/common:time_interface", "//include/envoy/runtime:runtime_interface", "//source/common/common:hex_lib", + "//source/common/common:singleton", "//source/common/common:utility_lib", "//source/common/network:address_lib", ], diff --git a/source/common/tracing/zipkin/tracer.cc b/source/common/tracing/zipkin/tracer.cc index df064827d64f6..171f6b0d20a20 100644 --- a/source/common/tracing/zipkin/tracer.cc +++ b/source/common/tracing/zipkin/tracer.cc @@ -51,7 +51,7 @@ SpanPtr Tracer::startSpan(const std::string& span_name, MonotonicTime start_time timestamp_micro = std::chrono::duration_cast( ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); - if ((previous_context.annotationSet().sr_) && (!previous_context.annotationSet().cs_)) { + if (previous_context.annotationSet().sr_ && !previous_context.annotationSet().cs_) { // We need to create a new span that is a child of the previous span; no shared context // Create a new span id @@ -68,7 +68,7 @@ SpanPtr Tracer::startSpan(const std::string& span_name, MonotonicTime start_time // Set the timestamp globally for the span span_ptr->setTimestamp(timestamp_micro); - } else if ((previous_context.annotationSet().cs_) && (!previous_context.annotationSet().sr_)) { + } else if (previous_context.annotationSet().cs_ && !previous_context.annotationSet().sr_) { // We need to create a new span that will share context with the previous span // Initialize the shared context for the new span diff --git a/source/common/tracing/zipkin/zipkin_core_types.cc b/source/common/tracing/zipkin/zipkin_core_types.cc index 5a52e238d2e65..60b3a3d3d776a 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.cc +++ b/source/common/tracing/zipkin/zipkin_core_types.cc @@ -29,23 +29,23 @@ const std::string Endpoint::toJson() { rapidjson::Writer writer(s); writer.StartObject(); if (!address_) { - writer.Key(ZipkinJsonFieldNames::ENDPOINT_IPV4.c_str()); + writer.Key(ZipkinJsonFieldNames::get().ENDPOINT_IPV4.c_str()); writer.String(""); - writer.Key(ZipkinJsonFieldNames::ENDPOINT_PORT.c_str()); + writer.Key(ZipkinJsonFieldNames::get().ENDPOINT_PORT.c_str()); writer.Uint(0); } else { if (address_->ip()->version() == Network::Address::IpVersion::v4) { // IPv4 - writer.Key(ZipkinJsonFieldNames::ENDPOINT_IPV4.c_str()); + writer.Key(ZipkinJsonFieldNames::get().ENDPOINT_IPV4.c_str()); } else { // IPv6 - writer.Key(ZipkinJsonFieldNames::ENDPOINT_IPV6.c_str()); + writer.Key(ZipkinJsonFieldNames::get().ENDPOINT_IPV6.c_str()); } writer.String(address_->ip()->addressAsString().c_str()); - writer.Key(ZipkinJsonFieldNames::ENDPOINT_PORT.c_str()); + writer.Key(ZipkinJsonFieldNames::get().ENDPOINT_PORT.c_str()); writer.Uint(address_->ip()->port()); } - writer.Key(ZipkinJsonFieldNames::ENDPOINT_SERVICE_NAME.c_str()); + writer.Key(ZipkinJsonFieldNames::get().ENDPOINT_SERVICE_NAME.c_str()); writer.String(service_name_.c_str()); writer.EndObject(); std::string json_string = s.GetString(); @@ -75,9 +75,9 @@ const std::string Annotation::toJson() { rapidjson::StringBuffer s; rapidjson::Writer writer(s); writer.StartObject(); - writer.Key(ZipkinJsonFieldNames::ANNOTATION_TIMESTAMP.c_str()); + writer.Key(ZipkinJsonFieldNames::get().ANNOTATION_TIMESTAMP.c_str()); writer.Uint64(timestamp_); - writer.Key(ZipkinJsonFieldNames::ANNOTATION_VALUE.c_str()); + writer.Key(ZipkinJsonFieldNames::get().ANNOTATION_VALUE.c_str()); writer.String(value_.c_str()); writer.EndObject(); @@ -85,7 +85,7 @@ const std::string Annotation::toJson() { if (endpoint_.valid()) { Util::mergeJsons(json_string, static_cast(endpoint_.value()).toJson(), - ZipkinJsonFieldNames::ANNOTATION_ENDPOINT.c_str()); + ZipkinJsonFieldNames::get().ANNOTATION_ENDPOINT.c_str()); } return json_string; @@ -115,9 +115,9 @@ const std::string BinaryAnnotation::toJson() { rapidjson::StringBuffer s; rapidjson::Writer writer(s); writer.StartObject(); - writer.Key(ZipkinJsonFieldNames::BINARY_ANNOTATION_KEY.c_str()); + writer.Key(ZipkinJsonFieldNames::get().BINARY_ANNOTATION_KEY.c_str()); writer.String(key_.c_str()); - writer.Key(ZipkinJsonFieldNames::BINARY_ANNOTATION_VALUE.c_str()); + writer.Key(ZipkinJsonFieldNames::get().BINARY_ANNOTATION_VALUE.c_str()); writer.String(value_.c_str()); writer.EndObject(); @@ -125,7 +125,7 @@ const std::string BinaryAnnotation::toJson() { if (endpoint_.valid()) { Util::mergeJsons(json_string, static_cast(endpoint_.value()).toJson(), - ZipkinJsonFieldNames::BINARY_ANNOTATION_ENDPOINT.c_str()); + ZipkinJsonFieldNames::get().BINARY_ANNOTATION_ENDPOINT.c_str()); } return json_string; @@ -185,25 +185,25 @@ const std::string Span::toJson() { rapidjson::StringBuffer s; rapidjson::Writer writer(s); writer.StartObject(); - writer.Key(ZipkinJsonFieldNames::SPAN_TRACE_ID.c_str()); + writer.Key(ZipkinJsonFieldNames::get().SPAN_TRACE_ID.c_str()); writer.String(Hex::uint64ToHex(trace_id_).c_str()); - writer.Key(ZipkinJsonFieldNames::SPAN_NAME.c_str()); + writer.Key(ZipkinJsonFieldNames::get().SPAN_NAME.c_str()); writer.String(name_.c_str()); - writer.Key(ZipkinJsonFieldNames::SPAN_ID.c_str()); + writer.Key(ZipkinJsonFieldNames::get().SPAN_ID.c_str()); writer.String(Hex::uint64ToHex(id_).c_str()); if (parent_id_.valid() && parent_id_.value()) { - writer.Key(ZipkinJsonFieldNames::SPAN_PARENT_ID.c_str()); + writer.Key(ZipkinJsonFieldNames::get().SPAN_PARENT_ID.c_str()); writer.String(Hex::uint64ToHex(parent_id_.value()).c_str()); } if (timestamp_.valid()) { - writer.Key(ZipkinJsonFieldNames::SPAN_TIMESTAMP.c_str()); + writer.Key(ZipkinJsonFieldNames::get().SPAN_TIMESTAMP.c_str()); writer.Int64(timestamp_.value()); } if (duration_.valid()) { - writer.Key(ZipkinJsonFieldNames::SPAN_DURATION.c_str()); + writer.Key(ZipkinJsonFieldNames::get().SPAN_DURATION.c_str()); writer.Int64(duration_.value()); } @@ -217,14 +217,14 @@ const std::string Span::toJson() { annotation_json_vector.push_back(it->toJson()); } Util::addArrayToJson(json_string, annotation_json_vector, - ZipkinJsonFieldNames::SPAN_ANNOTATIONS.c_str()); + ZipkinJsonFieldNames::get().SPAN_ANNOTATIONS.c_str()); std::vector binary_annotation_json_vector; for (auto it = binary_annotations_.begin(); it != binary_annotations_.end(); it++) { binary_annotation_json_vector.push_back(it->toJson()); } Util::addArrayToJson(json_string, binary_annotation_json_vector, - ZipkinJsonFieldNames::SPAN_BINARY_ANNOTATIONS.c_str()); + ZipkinJsonFieldNames::get().SPAN_BINARY_ANNOTATIONS.c_str()); return json_string; } @@ -232,7 +232,7 @@ const std::string Span::toJson() { void Span::finish() { // Assumption: Span will have only one annotation when this method is called SpanContext context(*this); - if ((context.annotationSet().sr_) && (!context.annotationSet().ss_)) { + if (context.annotationSet().sr_ && !context.annotationSet().ss_) { // Need to set the SS annotation Annotation ss; ss.setEndpoint(annotations_[0].endpoint()); @@ -240,30 +240,29 @@ void Span::finish() { ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count()); ss.setValue(ZipkinCoreConstants::SERVER_SEND); annotations_.push_back(std::move(ss)); - } else if ((context.annotationSet().cs_) && (!context.annotationSet().cr_)) { + } else if (context.annotationSet().cs_ && !context.annotationSet().cr_) { // Need to set the CR annotation Annotation cr; - uint64_t stop_timestamp = + const uint64_t stop_timestamp = std::chrono::duration_cast( ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); cr.setEndpoint(annotations_[0].endpoint()); cr.setTimestamp(stop_timestamp); cr.setValue(ZipkinCoreConstants::CLIENT_RECV); annotations_.push_back(std::move(cr)); - int64_t monotonic_stop_time = + const int64_t monotonic_stop_time = std::chrono::duration_cast( ProdMonotonicTimeSource::instance_.currentTime().time_since_epoch()).count(); setDuration(monotonic_stop_time - monotonic_start_time_); } - auto t = tracer(); - if (t) { + if (auto t = tracer()) { t->reportSpan(std::move(*this)); } } void Span::setTag(const std::string& name, const std::string& value) { - if ((name.size() > 0) && (value.size() > 0)) { + if (name.size() > 0 && value.size() > 0) { addBinaryAnnotation(BinaryAnnotation(name, value)); } } diff --git a/source/common/tracing/zipkin/zipkin_json_field_names.cc b/source/common/tracing/zipkin/zipkin_json_field_names.cc deleted file mode 100644 index ad8a852a3a293..0000000000000 --- a/source/common/tracing/zipkin/zipkin_json_field_names.cc +++ /dev/null @@ -1,26 +0,0 @@ -#include "common/tracing/zipkin/zipkin_json_field_names.h" - -namespace Zipkin { - -const std::string ZipkinJsonFieldNames::SPAN_TRACE_ID = "traceId"; -const std::string ZipkinJsonFieldNames::SPAN_PARENT_ID = "parentId"; -const std::string ZipkinJsonFieldNames::SPAN_NAME = "name"; -const std::string ZipkinJsonFieldNames::SPAN_ID = "id"; -const std::string ZipkinJsonFieldNames::SPAN_TIMESTAMP = "timestamp"; -const std::string ZipkinJsonFieldNames::SPAN_DURATION = "duration"; -const std::string ZipkinJsonFieldNames::SPAN_ANNOTATIONS = "annotations"; -const std::string ZipkinJsonFieldNames::SPAN_BINARY_ANNOTATIONS = "binaryAnnotations"; - -const std::string ZipkinJsonFieldNames::ANNOTATION_ENDPOINT = "endpoint"; -const std::string ZipkinJsonFieldNames::ANNOTATION_TIMESTAMP = "timestamp"; -const std::string ZipkinJsonFieldNames::ANNOTATION_VALUE = "value"; - -const std::string ZipkinJsonFieldNames::BINARY_ANNOTATION_ENDPOINT = "endpoint"; -const std::string ZipkinJsonFieldNames::BINARY_ANNOTATION_KEY = "key"; -const std::string ZipkinJsonFieldNames::BINARY_ANNOTATION_VALUE = "value"; - -const std::string ZipkinJsonFieldNames::ENDPOINT_SERVICE_NAME = "serviceName"; -const std::string ZipkinJsonFieldNames::ENDPOINT_PORT = "port"; -const std::string ZipkinJsonFieldNames::ENDPOINT_IPV4 = "ipv4"; -const std::string ZipkinJsonFieldNames::ENDPOINT_IPV6 = "ipv6"; -} // Zipkin diff --git a/source/common/tracing/zipkin/zipkin_json_field_names.h b/source/common/tracing/zipkin/zipkin_json_field_names.h index cf49d5d67bedb..21d71ba313f0a 100644 --- a/source/common/tracing/zipkin/zipkin_json_field_names.h +++ b/source/common/tracing/zipkin/zipkin_json_field_names.h @@ -2,30 +2,35 @@ #include +#include "common/common/singleton.h" + namespace Zipkin { -class ZipkinJsonFieldNames { +class ZipkinJsonFieldNameValues { public: - static const std::string SPAN_TRACE_ID; - static const std::string SPAN_PARENT_ID; - static const std::string SPAN_NAME; - static const std::string SPAN_ID; - static const std::string SPAN_TIMESTAMP; - static const std::string SPAN_DURATION; - static const std::string SPAN_ANNOTATIONS; - static const std::string SPAN_BINARY_ANNOTATIONS; - - static const std::string ANNOTATION_ENDPOINT; - static const std::string ANNOTATION_TIMESTAMP; - static const std::string ANNOTATION_VALUE; - - static const std::string BINARY_ANNOTATION_ENDPOINT; - static const std::string BINARY_ANNOTATION_KEY; - static const std::string BINARY_ANNOTATION_VALUE; - - static const std::string ENDPOINT_SERVICE_NAME; - static const std::string ENDPOINT_PORT; - static const std::string ENDPOINT_IPV4; - static const std::string ENDPOINT_IPV6; + const std::string SPAN_TRACE_ID = "traceId"; + const std::string SPAN_PARENT_ID = "parentId"; + const std::string SPAN_NAME = "name"; + const std::string SPAN_ID = "id"; + const std::string SPAN_TIMESTAMP = "timestamp"; + const std::string SPAN_DURATION = "duration"; + const std::string SPAN_ANNOTATIONS = "annotations"; + const std::string SPAN_BINARY_ANNOTATIONS = "binaryAnnotations"; + + const std::string ANNOTATION_ENDPOINT = "endpoint"; + const std::string ANNOTATION_TIMESTAMP = "timestamp"; + const std::string ANNOTATION_VALUE = "value"; + + const std::string BINARY_ANNOTATION_ENDPOINT = "endpoint"; + const std::string BINARY_ANNOTATION_KEY = "key"; + const std::string BINARY_ANNOTATION_VALUE = "value"; + + const std::string ENDPOINT_SERVICE_NAME = "serviceName"; + const std::string ENDPOINT_PORT = "port"; + const std::string ENDPOINT_IPV4 = "ipv4"; + const std::string ENDPOINT_IPV6 = "ipv6"; }; + +typedef ConstSingleton ZipkinJsonFieldNames; + } // Zipkin From a157ad0c33ee076c20b0ce3471ecf0d8e0079fda Mon Sep 17 00:00:00 2001 From: "FABIO A. OLIVEIRA" Date: Tue, 2 May 2017 17:14:44 -0400 Subject: [PATCH 55/55] Use the Singleton template for string constants (#430) --- source/common/tracing/zipkin/BUILD | 1 - source/common/tracing/zipkin/span_context.cc | 29 +++++---- source/common/tracing/zipkin/tracer.cc | 6 +- .../tracing/zipkin/zipkin_core_constants.cc | 35 ---------- .../tracing/zipkin/zipkin_core_constants.h | 65 ++++++++++--------- .../tracing/zipkin/zipkin_core_types.cc | 4 +- .../tracing/zipkin/span_context_test.cc | 14 ++-- test/common/tracing/zipkin/tracer_test.cc | 14 ++-- .../tracing/zipkin/zipkin_core_types_test.cc | 28 ++++---- 9 files changed, 84 insertions(+), 112 deletions(-) delete mode 100644 source/common/tracing/zipkin/zipkin_core_constants.cc diff --git a/source/common/tracing/zipkin/BUILD b/source/common/tracing/zipkin/BUILD index a674874beae0d..d52284f57ba25 100644 --- a/source/common/tracing/zipkin/BUILD +++ b/source/common/tracing/zipkin/BUILD @@ -11,7 +11,6 @@ envoy_cc_library( "span_context.cc", "tracer.cc", "util.cc", - "zipkin_core_constants.cc", "zipkin_core_types.cc", ], hdrs = [ diff --git a/source/common/tracing/zipkin/span_context.cc b/source/common/tracing/zipkin/span_context.cc index 33c7c37522cd7..a9a2772b8a852 100644 --- a/source/common/tracing/zipkin/span_context.cc +++ b/source/common/tracing/zipkin/span_context.cc @@ -49,8 +49,9 @@ static const std::string& spanContextRegexStr() { static const std::string* span_context_regex_str = new std::string( "^" + hexDigitGroupRegexStr() + fieldSeparator() + hexDigitGroupRegexStr() + fieldSeparator() + hexDigitGroupRegexStr() + "((" + fieldSeparator() + "(" + - ZipkinCoreConstants::CLIENT_SEND + "|" + ZipkinCoreConstants::SERVER_RECV + "|" + - ZipkinCoreConstants::CLIENT_RECV + "|" + ZipkinCoreConstants::SERVER_SEND + "))*)$"); + ZipkinCoreConstants::get().CLIENT_SEND + "|" + ZipkinCoreConstants::get().SERVER_RECV + "|" + + ZipkinCoreConstants::get().CLIENT_RECV + "|" + ZipkinCoreConstants::get().SERVER_SEND + + "))*)$"); return *span_context_regex_str; } @@ -74,13 +75,13 @@ SpanContext::SpanContext(const Span& span) { parent_id_ = span.isSetParentId() ? span.parentId() : 0; for (const Annotation& annotation : span.annotations()) { - if (annotation.value() == ZipkinCoreConstants::CLIENT_RECV) { + if (annotation.value() == ZipkinCoreConstants::get().CLIENT_RECV) { annotation_values_.cr_ = true; - } else if (annotation.value() == ZipkinCoreConstants::CLIENT_SEND) { + } else if (annotation.value() == ZipkinCoreConstants::get().CLIENT_SEND) { annotation_values_.cs_ = true; - } else if (annotation.value() == ZipkinCoreConstants::SERVER_RECV) { + } else if (annotation.value() == ZipkinCoreConstants::get().SERVER_RECV) { annotation_values_.sr_ = true; - } else if (annotation.value() == ZipkinCoreConstants::SERVER_SEND) { + } else if (annotation.value() == ZipkinCoreConstants::get().SERVER_SEND) { annotation_values_.ss_ = true; } } @@ -98,16 +99,16 @@ const std::string SpanContext::serializeToString() { parentIdAsHexString(); if (annotation_values_.cr_) { - result += fieldSeparator() + ZipkinCoreConstants::CLIENT_RECV; + result += fieldSeparator() + ZipkinCoreConstants::get().CLIENT_RECV; } if (annotation_values_.cs_) { - result += fieldSeparator() + ZipkinCoreConstants::CLIENT_SEND; + result += fieldSeparator() + ZipkinCoreConstants::get().CLIENT_SEND; } if (annotation_values_.sr_) { - result += fieldSeparator() + ZipkinCoreConstants::SERVER_RECV; + result += fieldSeparator() + ZipkinCoreConstants::get().SERVER_RECV; } if (annotation_values_.ss_) { - result += fieldSeparator() + ZipkinCoreConstants::SERVER_SEND; + result += fieldSeparator() + ZipkinCoreConstants::get().SERVER_SEND; } return result; @@ -131,13 +132,13 @@ void SpanContext::populateFromString(const std::string& span_context_str) { std::vector annotation_value_strings = StringUtil::split(matched_annotations, fieldSeparator()); for (const std::string& annotation_value : annotation_value_strings) { - if (annotation_value == ZipkinCoreConstants::CLIENT_RECV) { + if (annotation_value == ZipkinCoreConstants::get().CLIENT_RECV) { annotation_values_.cr_ = true; - } else if (annotation_value == ZipkinCoreConstants::CLIENT_SEND) { + } else if (annotation_value == ZipkinCoreConstants::get().CLIENT_SEND) { annotation_values_.cs_ = true; - } else if (annotation_value == ZipkinCoreConstants::SERVER_RECV) { + } else if (annotation_value == ZipkinCoreConstants::get().SERVER_RECV) { annotation_values_.sr_ = true; - } else if (annotation_value == ZipkinCoreConstants::SERVER_SEND) { + } else if (annotation_value == ZipkinCoreConstants::get().SERVER_SEND) { annotation_values_.ss_ = true; } } diff --git a/source/common/tracing/zipkin/tracer.cc b/source/common/tracing/zipkin/tracer.cc index 171f6b0d20a20..c6e050a6c02f3 100644 --- a/source/common/tracing/zipkin/tracer.cc +++ b/source/common/tracing/zipkin/tracer.cc @@ -15,7 +15,7 @@ SpanPtr Tracer::startSpan(const std::string& span_name, MonotonicTime start_time // Build the CS annotation Annotation cs; cs.setEndpoint(std::move(ep)); - cs.setValue(ZipkinCoreConstants::CLIENT_SEND); + cs.setValue(ZipkinCoreConstants::get().CLIENT_SEND); // Create an all-new span, with no parent id SpanPtr span_ptr(new Span()); @@ -64,7 +64,7 @@ SpanPtr Tracer::startSpan(const std::string& span_name, MonotonicTime start_time span_ptr->setParentId(previous_context.id()); // Set the CS annotation value - annotation.setValue(ZipkinCoreConstants::CLIENT_SEND); + annotation.setValue(ZipkinCoreConstants::get().CLIENT_SEND); // Set the timestamp globally for the span span_ptr->setTimestamp(timestamp_micro); @@ -78,7 +78,7 @@ SpanPtr Tracer::startSpan(const std::string& span_name, MonotonicTime start_time } // Set the SR annotation value - annotation.setValue(ZipkinCoreConstants::SERVER_RECV); + annotation.setValue(ZipkinCoreConstants::get().SERVER_RECV); } else { return span_ptr; // return an empty span } diff --git a/source/common/tracing/zipkin/zipkin_core_constants.cc b/source/common/tracing/zipkin/zipkin_core_constants.cc deleted file mode 100644 index 2ea265b66a5d2..0000000000000 --- a/source/common/tracing/zipkin/zipkin_core_constants.cc +++ /dev/null @@ -1,35 +0,0 @@ -#include "common/tracing/zipkin/zipkin_core_constants.h" - -namespace Zipkin { - -const std::string ZipkinCoreConstants::CLIENT_SEND = "cs"; -const std::string ZipkinCoreConstants::CLIENT_RECV = "cr"; -const std::string ZipkinCoreConstants::SERVER_SEND = "ss"; -const std::string ZipkinCoreConstants::SERVER_RECV = "sr"; -const std::string ZipkinCoreConstants::WIRE_SEND = "ws"; -const std::string ZipkinCoreConstants::WIRE_RECV = "wr"; -const std::string ZipkinCoreConstants::CLIENT_SEND_FRAGMENT = "csf"; -const std::string ZipkinCoreConstants::CLIENT_RECV_FRAGMENT = "crf"; -const std::string ZipkinCoreConstants::SERVER_SEND_FRAGMENT = "ssf"; -const std::string ZipkinCoreConstants::SERVER_RECV_FRAGMENT = "srf"; - -const std::string ZipkinCoreConstants::HTTP_HOST = "http.host"; -const std::string ZipkinCoreConstants::HTTP_METHOD = "http.method"; -const std::string ZipkinCoreConstants::HTTP_PATH = "http.path"; -const std::string ZipkinCoreConstants::HTTP_URL = "http.url"; -const std::string ZipkinCoreConstants::HTTP_STATUS_CODE = "http.status_code"; -const std::string ZipkinCoreConstants::HTTP_REQUEST_SIZE = "http.request.size"; -const std::string ZipkinCoreConstants::HTTP_RESPONSE_SIZE = "http.response.size"; - -const std::string ZipkinCoreConstants::LOCAL_COMPONENT = "lc"; -const std::string ZipkinCoreConstants::ERROR = "error"; -const std::string ZipkinCoreConstants::CLIENT_ADDR = "ca"; -const std::string ZipkinCoreConstants::SERVER_ADDR = "sa"; - -// Zipkin B3 headers -const std::string ZipkinCoreConstants::X_B3_TRACE_ID = "X-B3-TraceId"; -const std::string ZipkinCoreConstants::X_B3_SPAN_ID = "X-B3-SpanId"; -const std::string ZipkinCoreConstants::X_B3_PARENT_SPAN_ID = "X-B3-ParentSpanId"; -const std::string ZipkinCoreConstants::X_B3_SAMPLED = "X-B3-Sampled"; -const std::string ZipkinCoreConstants::X_B3_FLAGS = "X-B3-Flags"; -} // Zipkin diff --git a/source/common/tracing/zipkin/zipkin_core_constants.h b/source/common/tracing/zipkin/zipkin_core_constants.h index 9021aa286c0f4..f932272779e05 100644 --- a/source/common/tracing/zipkin/zipkin_core_constants.h +++ b/source/common/tracing/zipkin/zipkin_core_constants.h @@ -2,37 +2,44 @@ #include +#include "common/common/singleton.h" + namespace Zipkin { -class ZipkinCoreConstants { +class ZipkinCoreConstantValues { public: - static const std::string CLIENT_SEND; - static const std::string CLIENT_RECV; - static const std::string SERVER_SEND; - static const std::string SERVER_RECV; - static const std::string WIRE_SEND; - static const std::string WIRE_RECV; - static const std::string CLIENT_SEND_FRAGMENT; - static const std::string CLIENT_RECV_FRAGMENT; - static const std::string SERVER_SEND_FRAGMENT; - static const std::string SERVER_RECV_FRAGMENT; - static const std::string HTTP_HOST; - static const std::string HTTP_METHOD; - static const std::string HTTP_PATH; - static const std::string HTTP_URL; - static const std::string HTTP_STATUS_CODE; - static const std::string HTTP_REQUEST_SIZE; - static const std::string HTTP_RESPONSE_SIZE; - static const std::string LOCAL_COMPONENT; - static const std::string ERROR; - static const std::string CLIENT_ADDR; - static const std::string SERVER_ADDR; - - // Zipkin B3 Headers - static const std::string X_B3_TRACE_ID; - static const std::string X_B3_SPAN_ID; - static const std::string X_B3_PARENT_SPAN_ID; - static const std::string X_B3_SAMPLED; - static const std::string X_B3_FLAGS; + const std::string CLIENT_SEND = "cs"; + const std::string CLIENT_RECV = "cr"; + const std::string SERVER_SEND = "ss"; + const std::string SERVER_RECV = "sr"; + const std::string WIRE_SEND = "ws"; + const std::string WIRE_RECV = "wr"; + const std::string CLIENT_SEND_FRAGMENT = "csf"; + const std::string CLIENT_RECV_FRAGMENT = "crf"; + const std::string SERVER_SEND_FRAGMENT = "ssf"; + const std::string SERVER_RECV_FRAGMENT = "srf"; + + const std::string HTTP_HOST = "http.host"; + const std::string HTTP_METHOD = "http.method"; + const std::string HTTP_PATH = "http.path"; + const std::string HTTP_URL = "http.url"; + const std::string HTTP_STATUS_CODE = "http.status_code"; + const std::string HTTP_REQUEST_SIZE = "http.request.size"; + const std::string HTTP_RESPONSE_SIZE = "http.response.size"; + + const std::string LOCAL_COMPONENT = "lc"; + const std::string ERROR = "error"; + const std::string CLIENT_ADDR = "ca"; + const std::string SERVER_ADDR = "sa"; + + // Zipkin B3 headers + const std::string X_B3_TRACE_ID = "X-B3-TraceId"; + const std::string X_B3_SPAN_ID = "X-B3-SpanId"; + const std::string X_B3_PARENT_SPAN_ID = "X-B3-ParentSpanId"; + const std::string X_B3_SAMPLED = "X-B3-Sampled"; + const std::string X_B3_FLAGS = "X-B3-Flags"; }; + +typedef ConstSingleton ZipkinCoreConstants; + } // Zipkin diff --git a/source/common/tracing/zipkin/zipkin_core_types.cc b/source/common/tracing/zipkin/zipkin_core_types.cc index 60b3a3d3d776a..b84497d1d2a39 100644 --- a/source/common/tracing/zipkin/zipkin_core_types.cc +++ b/source/common/tracing/zipkin/zipkin_core_types.cc @@ -238,7 +238,7 @@ void Span::finish() { ss.setEndpoint(annotations_[0].endpoint()); ss.setTimestamp(std::chrono::duration_cast( ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count()); - ss.setValue(ZipkinCoreConstants::SERVER_SEND); + ss.setValue(ZipkinCoreConstants::get().SERVER_SEND); annotations_.push_back(std::move(ss)); } else if (context.annotationSet().cs_ && !context.annotationSet().cr_) { // Need to set the CR annotation @@ -248,7 +248,7 @@ void Span::finish() { ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); cr.setEndpoint(annotations_[0].endpoint()); cr.setTimestamp(stop_timestamp); - cr.setValue(ZipkinCoreConstants::CLIENT_RECV); + cr.setValue(ZipkinCoreConstants::get().CLIENT_RECV); annotations_.push_back(std::move(cr)); const int64_t monotonic_stop_time = std::chrono::duration_cast( diff --git a/test/common/tracing/zipkin/span_context_test.cc b/test/common/tracing/zipkin/span_context_test.cc index f4f3cb818a98c..2d2a599978f69 100644 --- a/test/common/tracing/zipkin/span_context_test.cc +++ b/test/common/tracing/zipkin/span_context_test.cc @@ -156,7 +156,7 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { // Span context populated with trace id, id, parent id, and one annotation Annotation ann; - ann.setValue(ZipkinCoreConstants::SERVER_RECV); + ann.setValue(ZipkinCoreConstants::get().SERVER_RECV); span.addAnnotation(ann); SpanContext span_context_3(span); EXPECT_EQ(2722130815203937912ULL, span_context_3.trace_id()); @@ -173,7 +173,7 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { span_context_3.serializeToString()); // Span context populated with trace id, id, parent id, and two annotations - ann.setValue(ZipkinCoreConstants::SERVER_SEND); + ann.setValue(ZipkinCoreConstants::get().SERVER_SEND); span.addAnnotation(ann); SpanContext span_context_4(span); EXPECT_EQ(2722130815203937912ULL, span_context_4.trace_id()); @@ -190,7 +190,7 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { span_context_4.serializeToString()); // Span context populated with trace id, id, parent id, and three annotations - ann.setValue(ZipkinCoreConstants::CLIENT_SEND); + ann.setValue(ZipkinCoreConstants::get().CLIENT_SEND); span.addAnnotation(ann); SpanContext span_context_5(span); EXPECT_EQ(2722130815203937912ULL, span_context_5.trace_id()); @@ -209,13 +209,13 @@ TEST(ZipkinSpanContextTest, populateFromSpan) { // Span context populated with trace id, id, parent id, and four annotations std::vector annotations; annotations.push_back(ann); - ann.setValue(ZipkinCoreConstants::SERVER_SEND); + ann.setValue(ZipkinCoreConstants::get().SERVER_SEND); annotations.push_back(ann); - ann.setValue(ZipkinCoreConstants::SERVER_RECV); + ann.setValue(ZipkinCoreConstants::get().SERVER_RECV); annotations.push_back(ann); - ann.setValue(ZipkinCoreConstants::CLIENT_RECV); + ann.setValue(ZipkinCoreConstants::get().CLIENT_RECV); annotations.push_back(ann); - ann.setValue(ZipkinCoreConstants::CLIENT_SEND); + ann.setValue(ZipkinCoreConstants::get().CLIENT_SEND); annotations.push_back(ann); span.setAnnotations(annotations); SpanContext span_context_6(span); diff --git a/test/common/tracing/zipkin/tracer_test.cc b/test/common/tracing/zipkin/tracer_test.cc index 46bd9041e4c5e..039284a351e9f 100644 --- a/test/common/tracing/zipkin/tracer_test.cc +++ b/test/common/tracing/zipkin/tracer_test.cc @@ -49,7 +49,7 @@ TEST(ZipkinTracerTest, spanCreation) { // A CS annotation must have been added EXPECT_EQ(1ULL, root_span->annotations().size()); Annotation ann = root_span->annotations()[0]; - EXPECT_EQ(ZipkinCoreConstants::CLIENT_SEND, ann.value()); + EXPECT_EQ(ZipkinCoreConstants::get().CLIENT_SEND, ann.value()); EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); Endpoint endpoint = ann.endpoint(); @@ -91,7 +91,7 @@ TEST(ZipkinTracerTest, spanCreation) { // An SR annotation must have been added EXPECT_EQ(1ULL, server_side_shared_context_span->annotations().size()); ann = server_side_shared_context_span->annotations()[0]; - EXPECT_EQ(ZipkinCoreConstants::SERVER_RECV, ann.value()); + EXPECT_EQ(ZipkinCoreConstants::get().SERVER_RECV, ann.value()); EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); endpoint = ann.endpoint(); @@ -132,7 +132,7 @@ TEST(ZipkinTracerTest, spanCreation) { // A CS annotation must have been added EXPECT_EQ(1ULL, child_span->annotations().size()); ann = child_span->annotations()[0]; - EXPECT_EQ(ZipkinCoreConstants::CLIENT_SEND, ann.value()); + EXPECT_EQ(ZipkinCoreConstants::get().CLIENT_SEND, ann.value()); EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); endpoint = ann.endpoint(); @@ -166,7 +166,7 @@ TEST(ZipkinTracerTest, finishSpan) { // Check the CS annotation added at span-creation time Annotation ann = span->annotations()[0]; - EXPECT_EQ(ZipkinCoreConstants::CLIENT_SEND, ann.value()); + EXPECT_EQ(ZipkinCoreConstants::get().CLIENT_SEND, ann.value()); EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); Endpoint endpoint = ann.endpoint(); @@ -174,7 +174,7 @@ TEST(ZipkinTracerTest, finishSpan) { // Check the CR annotation added when ending the span ann = span->annotations()[1]; - EXPECT_EQ(ZipkinCoreConstants::CLIENT_RECV, ann.value()); + EXPECT_EQ(ZipkinCoreConstants::get().CLIENT_RECV, ann.value()); EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); endpoint = ann.endpoint(); @@ -201,7 +201,7 @@ TEST(ZipkinTracerTest, finishSpan) { // Check the SR annotation added at span-creation time ann = server_side->annotations()[0]; - EXPECT_EQ(ZipkinCoreConstants::SERVER_RECV, ann.value()); + EXPECT_EQ(ZipkinCoreConstants::get().SERVER_RECV, ann.value()); EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); endpoint = ann.endpoint(); @@ -209,7 +209,7 @@ TEST(ZipkinTracerTest, finishSpan) { // Check the SS annotation added when ending the span ann = server_side->annotations()[1]; - EXPECT_EQ(ZipkinCoreConstants::SERVER_SEND, ann.value()); + EXPECT_EQ(ZipkinCoreConstants::get().SERVER_SEND, ann.value()); EXPECT_NE(0ULL, ann.timestamp()); // annotation's timestamp must be set EXPECT_TRUE(ann.isSetEndpoint()); endpoint = ann.endpoint(); diff --git a/test/common/tracing/zipkin/zipkin_core_types_test.cc b/test/common/tracing/zipkin/zipkin_core_types_test.cc index c19465d5bc4be..01845b3fb9609 100644 --- a/test/common/tracing/zipkin/zipkin_core_types_test.cc +++ b/test/common/tracing/zipkin/zipkin_core_types_test.cc @@ -87,11 +87,11 @@ TEST(ZipkinCoreTypesAnnotationTest, defaultConstructor) { ann.setTimestamp(timestamp); EXPECT_EQ(timestamp, ann.timestamp()); - ann.setValue(ZipkinCoreConstants::CLIENT_SEND); - EXPECT_EQ(ZipkinCoreConstants::CLIENT_SEND, ann.value()); + ann.setValue(ZipkinCoreConstants::get().CLIENT_SEND); + EXPECT_EQ(ZipkinCoreConstants::get().CLIENT_SEND, ann.value()); std::string expected_json = R"({"timestamp":)" + std::to_string(timestamp) + R"(,"value":")" + - ZipkinCoreConstants::CLIENT_SEND + R"("})"; + ZipkinCoreConstants::get().CLIENT_SEND + R"("})"; EXPECT_EQ(expected_json, ann.toJson()); // Test the copy-semantics flavor of setEndpoint @@ -105,7 +105,7 @@ TEST(ZipkinCoreTypesAnnotationTest, defaultConstructor) { (const_cast(ann.endpoint())).toJson()); expected_json = R"({"timestamp":)" + std::to_string(timestamp) + R"(,"value":")" + - ZipkinCoreConstants::CLIENT_SEND + + ZipkinCoreConstants::get().CLIENT_SEND + R"(","endpoint":{"ipv4":)" R"("127.0.0.1","port":3306,"serviceName":"my_service"}})"; EXPECT_EQ(expected_json, ann.toJson()); @@ -120,7 +120,7 @@ TEST(ZipkinCoreTypesAnnotationTest, defaultConstructor) { (const_cast(ann.endpoint())).toJson()); expected_json = R"({"timestamp":)" + std::to_string(timestamp) + R"(,"value":")" + - ZipkinCoreConstants::CLIENT_SEND + + ZipkinCoreConstants::get().CLIENT_SEND + R"(","endpoint":{"ipv4":"192.168.1.1",)" R"("port":5555,"serviceName":"my_service_2"}})"; EXPECT_EQ(expected_json, ann.toJson()); @@ -133,10 +133,10 @@ TEST(ZipkinCoreTypesAnnotationTest, customConstructor) { uint64_t timestamp = std::chrono::duration_cast( ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); - Annotation ann(timestamp, ZipkinCoreConstants::CLIENT_SEND, ep); + Annotation ann(timestamp, ZipkinCoreConstants::get().CLIENT_SEND, ep); EXPECT_EQ(timestamp, ann.timestamp()); - EXPECT_EQ(ZipkinCoreConstants::CLIENT_SEND, ann.value()); + EXPECT_EQ(ZipkinCoreConstants::get().CLIENT_SEND, ann.value()); EXPECT_TRUE(ann.isSetEndpoint()); EXPECT_EQ("my_service", ann.endpoint().serviceName()); @@ -144,7 +144,7 @@ TEST(ZipkinCoreTypesAnnotationTest, customConstructor) { (const_cast(ann.endpoint())).toJson()); std::string expected_json = R"({"timestamp":)" + std::to_string(timestamp) + R"(,"value":")" + - ZipkinCoreConstants::CLIENT_SEND + + ZipkinCoreConstants::get().CLIENT_SEND + R"(","endpoint":{"ipv4":"127.0.0.1",)" R"("port":3306,"serviceName":"my_service"}})"; EXPECT_EQ(expected_json, ann.toJson()); @@ -157,7 +157,7 @@ TEST(ZipkinCoreTypesAnnotationTest, copyConstructor) { uint64_t timestamp = std::chrono::duration_cast( ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); - Annotation ann(timestamp, ZipkinCoreConstants::CLIENT_SEND, ep); + Annotation ann(timestamp, ZipkinCoreConstants::get().CLIENT_SEND, ep); Annotation ann2(ann); EXPECT_EQ(ann.value(), ann2.value()); @@ -174,7 +174,7 @@ TEST(ZipkinCoreTypesAnnotationTest, assignmentOperator) { uint64_t timestamp = std::chrono::duration_cast( ProdSystemTimeSource::instance_.currentTime().time_since_epoch()).count(); - Annotation ann(timestamp, ZipkinCoreConstants::CLIENT_SEND, ep); + Annotation ann(timestamp, ZipkinCoreConstants::get().CLIENT_SEND, ep); Annotation ann2 = ann; EXPECT_EQ(ann.value(), ann2.value()); @@ -347,7 +347,7 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { Network::Address::parseInternetAddressAndPort("192.168.1.2:3306"); endpoint.setAddress(addr); - ann.setValue(Zipkin::ZipkinCoreConstants::CLIENT_SEND); + ann.setValue(Zipkin::ZipkinCoreConstants::get().CLIENT_SEND); ann.setTimestamp(timestamp); ann.setEndpoint(endpoint); @@ -355,7 +355,7 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { span.setAnnotations(annotations); EXPECT_EQ(1ULL, span.annotations().size()); - bann.setKey(Zipkin::ZipkinCoreConstants::LOCAL_COMPONENT); + bann.setKey(Zipkin::ZipkinCoreConstants::get().LOCAL_COMPONENT); bann.setValue("my_component_name"); bann.setEndpoint(endpoint); @@ -378,7 +378,7 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { // Test the copy-semantics flavor of addAnnotation and addBinaryAnnotation - ann.setValue(Zipkin::ZipkinCoreConstants::SERVER_SEND); + ann.setValue(Zipkin::ZipkinCoreConstants::get().SERVER_SEND); span.addAnnotation(ann); bann.setKey("http.return_code"); bann.setValue("200"); @@ -389,7 +389,7 @@ TEST(ZipkinCoreTypesSpanTest, defaultConstructor) { // Test the move-semantics flavor of addAnnotation and addBinaryAnnotation - ann.setValue(Zipkin::ZipkinCoreConstants::SERVER_RECV); + ann.setValue(Zipkin::ZipkinCoreConstants::get().SERVER_RECV); span.addAnnotation(std::move(ann)); bann.setKey("http.return_code"); bann.setValue("400");