diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e7f7995..b0e78e85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -230,6 +230,7 @@ set(LIGHTSTEP_SRCS src/common/utility.cpp src/tracer/propagation/trace_context_propagator.cpp src/tracer/propagation/lightstep_propagator.cpp src/tracer/propagation/multiheader_propagator.cpp + src/tracer/propagation/cloud_trace_propagator.cpp src/tracer/propagation/propagation.cpp src/tracer/propagation/propagation_options.cpp src/tracer/immutable_span_context.cpp diff --git a/include/lightstep/tracer.h b/include/lightstep/tracer.h index a78481bf..f9f51aac 100644 --- a/include/lightstep/tracer.h +++ b/include/lightstep/tracer.h @@ -26,7 +26,8 @@ enum class PropagationMode { lightstep = 1, b3 = 2, envoy = 3, - trace_context = 4 + trace_context = 4, + cloud_trace = 5 }; // DynamicConfigurationValue is used for configuration values that can diff --git a/src/tracer/json_options.cpp b/src/tracer/json_options.cpp index 88cf7a2b..1702dd71 100644 --- a/src/tracer/json_options.cpp +++ b/src/tracer/json_options.cpp @@ -47,6 +47,9 @@ static PropagationMode GetPropagationMode(opentracing::string_view s) { if (s == "trace_context") { return PropagationMode::trace_context; } + if (s == "cloud_trace") { + return PropagationMode::cloud_trace; + } std::ostringstream oss; oss << "invalid propagation mode " << s; throw std::runtime_error{oss.str()}; diff --git a/src/tracer/propagation/BUILD b/src/tracer/propagation/BUILD index 6675deb5..92c6dd98 100644 --- a/src/tracer/propagation/BUILD +++ b/src/tracer/propagation/BUILD @@ -167,6 +167,7 @@ lightstep_cc_library( ":envoy_propagator_lib", ":trace_context_propagator_lib", ":baggage_propagator_lib", + ":cloud_trace_propagator_lib", ], ) @@ -184,3 +185,18 @@ lightstep_cc_library( ":trace_context_lib", ], ) + +lightstep_cc_library( + name = "cloud_trace_propagator_lib", + private_hdrs = [ + "cloud_trace_propagator.h", + ], + srcs = [ + "cloud_trace_propagator.cpp", + ], + deps = [ + "//3rd_party/base64:base64_lib", + ":binary_propagation_lib", + ":utility_lib", + ], +) diff --git a/src/tracer/propagation/cloud_trace_propagator.cpp b/src/tracer/propagation/cloud_trace_propagator.cpp new file mode 100644 index 00000000..5219bceb --- /dev/null +++ b/src/tracer/propagation/cloud_trace_propagator.cpp @@ -0,0 +1,191 @@ +#include "tracer/propagation/cloud_trace_propagator.h" + +#include "common/hex_conversion.h" + +#include "tracer/propagation/binary_propagation.h" +#include "tracer/propagation/utility.h" + +const opentracing::string_view PropagationSingleKey = "x-cloud-trace-context"; +const opentracing::string_view PrefixBaggage = "ot-baggage-"; + +namespace lightstep { +//-------------------------------------------------------------------------------------------------- +// InjectSpanContext +//-------------------------------------------------------------------------------------------------- +opentracing::expected CloudTracePropagator::InjectSpanContext( + const opentracing::TextMapWriter& carrier, + const TraceContext& trace_context, opentracing::string_view /*trace_state*/, + const BaggageProtobufMap& /*baggage*/) const { + return this->InjectSpanContextImpl(carrier, trace_context); +} + +opentracing::expected CloudTracePropagator::InjectSpanContext( + const opentracing::TextMapWriter& carrier, + const TraceContext& trace_context, opentracing::string_view /*trace_state*/, + const BaggageFlatMap& /*baggage*/) const { + return this->InjectSpanContextImpl(carrier, trace_context); +} +//-------------------------------------------------------------------------------------------------- +// ExtractSpanContext +//-------------------------------------------------------------------------------------------------- +opentracing::expected CloudTracePropagator::ExtractSpanContext( + const opentracing::TextMapReader& carrier, bool case_sensitive, + TraceContext& trace_context, std::string& /*trace_state*/, + BaggageProtobufMap& baggage) const { + auto iequals = + [](opentracing::string_view lhs, opentracing::string_view rhs) noexcept { + return lhs.length() == rhs.length() && + std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs), + [](char a, char b) { + return std::tolower(a) == std::tolower(b); + }); + }; + opentracing::expected result; + if (case_sensitive) { + result = this->ExtractSpanContextImpl(carrier, trace_context, + std::equal_to{}, baggage); + } else { + result = this->ExtractSpanContextImpl(carrier, trace_context, iequals, baggage); + } + if (!result || !*result) { + return result; + } + + return result; +} + +//-------------------------------------------------------------------------------------------------- +// InjectSpanContextImpl +//-------------------------------------------------------------------------------------------------- +opentracing::expected CloudTracePropagator::InjectSpanContextImpl( + const opentracing::TextMapWriter& carrier, + const TraceContext& trace_context) const { + std::array buffer; + auto data_length = this->SerializeCloudTrace(trace_context, buffer.data()); + return carrier.Set(PropagationSingleKey, + opentracing::string_view{buffer.data(), data_length}); +} + +//-------------------------------------------------------------------------------------------------- +// ExtractSpanContextImpl +//-------------------------------------------------------------------------------------------------- +template +opentracing::expected CloudTracePropagator::ExtractSpanContextImpl( + const opentracing::TextMapReader& carrier, TraceContext& trace_context, const KeyCompare& key_compare, + BaggageProtobufMap& baggage) const { + bool parent_header_found = false; + auto result = + carrier.ForeachKey([&](opentracing::string_view key, + opentracing::string_view + value) noexcept->opentracing::expected { + if (key_compare(key, PropagationSingleKey)) { + auto was_successful = this->ParseCloudTrace(value, trace_context); + if (!was_successful) { + return opentracing::make_unexpected(was_successful.error()); + } + parent_header_found = true; + } else if (key.length() > PrefixBaggage.size() && + key_compare(opentracing::string_view{key.data(), + PrefixBaggage.size()}, + PrefixBaggage)) { + baggage.insert(BaggageProtobufMap::value_type( + ToLower( + opentracing::string_view{key.data() + PrefixBaggage.size(), + key.size() - PrefixBaggage.size()}), + value)); + } + return {}; + }); + if (!result) { + return opentracing::make_unexpected(result.error()); + } + return parent_header_found; +} + +opentracing::expected CloudTracePropagator::ParseCloudTrace( + opentracing::string_view s, TraceContext& trace_context) const noexcept { + if (s.size() < Num128BitHexDigits) { + return opentracing::make_unexpected( + std::make_error_code(std::errc::invalid_argument)); + } + size_t offset = 0; + + // default sampled to on (this comes from the ;o=1 part of + // x-cloud-trace-context; if it is not set we will default to sampling + // this request) + trace_context.trace_flags = SetTraceFlag(trace_context.trace_flags, true); + + // trace-id + auto error_maybe = NormalizedHexToUint128( + opentracing::string_view{s.data() + offset, Num128BitHexDigits}, + trace_context.trace_id_high, trace_context.trace_id_low); + if (!error_maybe) { + return error_maybe; + } + + offset += Num128BitHexDigits; + if (s.size() - offset < 2) { + // only a short form trace ID has been given (not a "trace id/span id") + return {}; + } + + if (s[offset] != '/') { + return opentracing::make_unexpected( + std::make_error_code(std::errc::invalid_argument)); + } + ++offset; + + std::array parent_id; + size_t i; + for (i=0; i < Num64BitDecimalDigits; ++i) { + if (offset == s.length() || std::isdigit(s[offset]) == 0) { + break; + } + parent_id[i] = s[offset]; + ++offset; + } + parent_id[i] = '\0'; + + // parent-id + errno = 0; + trace_context.parent_id = std::strtoull(parent_id.data(), nullptr, 10); + if (errno == ERANGE) { + return opentracing::make_unexpected( + std::make_error_code(std::errc::result_out_of_range)); + } + + if (s.size() - offset < 4) { + // only a "trace ID/span ID" has been given (not a "trace id/span id;o=[0-1]") + return {}; + } + + if(opentracing::string_view(s.begin() + offset, 3) != opentracing::string_view(";o=")) { + return opentracing::make_unexpected( + std::make_error_code(std::errc::invalid_argument)); + } + + offset += 3; + + // trace-flags + if (s[offset] == '0') { + // don't sample + trace_context.trace_flags = SetTraceFlag(trace_context.trace_flags, false); + } + + return {}; +} + +size_t CloudTracePropagator::SerializeCloudTrace(const TraceContext& trace_context, + char* s) const noexcept { + size_t offset = 0; + // trace-id + Uint64ToHex(trace_context.trace_id_high, s); + offset += Num64BitHexDigits; + Uint64ToHex(trace_context.trace_id_low, s + offset); + offset += Num64BitHexDigits; + + offset += snprintf(s + offset, CloudContextLength - offset, "/%lu;o=%d", trace_context.parent_id, IsTraceFlagSet(trace_context.trace_flags) ? 1 : 0); + + return offset; +} +} // namespace lightstep diff --git a/src/tracer/propagation/cloud_trace_propagator.h b/src/tracer/propagation/cloud_trace_propagator.h new file mode 100644 index 00000000..6d520e4f --- /dev/null +++ b/src/tracer/propagation/cloud_trace_propagator.h @@ -0,0 +1,45 @@ +#pragma once + +#include "tracer/propagation/propagator.h" + +namespace lightstep { + +const size_t CloudContextLength = 58; // max x-cloud-trace-context header + +const size_t Num64BitDecimalDigits = 20; + +class CloudTracePropagator final : public Propagator { + public: + // Propagator + opentracing::expected InjectSpanContext( + const opentracing::TextMapWriter& carrier, + const TraceContext& trace_context, opentracing::string_view trace_state, + const BaggageProtobufMap& baggage) const override; + + opentracing::expected InjectSpanContext( + const opentracing::TextMapWriter& carrier, + const TraceContext& trace_context, opentracing::string_view trace_state, + const BaggageFlatMap& baggage) const override; + + opentracing::expected ExtractSpanContext( + const opentracing::TextMapReader& carrier, bool case_sensitive, + TraceContext& trace_context, std::string& trace_state, + BaggageProtobufMap& baggage) const override; + + private: + opentracing::expected InjectSpanContextImpl( + const opentracing::TextMapWriter& carrier, + const TraceContext& trace_context) const; + + template + opentracing::expected ExtractSpanContextImpl( + const opentracing::TextMapReader& carrier, TraceContext& trace_context, + const KeyCompare& key_compare, BaggageProtobufMap& baggage) const; + + opentracing::expected ParseCloudTrace( + opentracing::string_view s, lightstep::TraceContext& trace_context) const noexcept; + + size_t SerializeCloudTrace(const TraceContext& trace_context, + char* s) const noexcept; +}; +} // namespace lightstep diff --git a/src/tracer/propagation/propagation_options.cpp b/src/tracer/propagation/propagation_options.cpp index 0dc4eba3..f36073dc 100644 --- a/src/tracer/propagation/propagation_options.cpp +++ b/src/tracer/propagation/propagation_options.cpp @@ -7,6 +7,7 @@ #include "tracer/propagation/envoy_propagator.h" #include "tracer/propagation/lightstep_propagator.h" #include "tracer/propagation/trace_context_propagator.h" +#include "tracer/propagation/cloud_trace_propagator.h" namespace lightstep { //-------------------------------------------------------------------------------------------------- @@ -76,6 +77,9 @@ static std::vector> MakePropagators( case PropagationMode::trace_context: result.emplace_back(new TraceContextPropagator{}); break; + case PropagationMode::cloud_trace: + result.emplace_back(new CloudTracePropagator{}); + break; } } return result; diff --git a/test/tracer/propagation/BUILD b/test/tracer/propagation/BUILD index 435cb54c..afbdd54b 100644 --- a/test/tracer/propagation/BUILD +++ b/test/tracer/propagation/BUILD @@ -141,3 +141,17 @@ lightstep_catch_test( "//src/tracer/propagation:propagation_lib", ], ) + +lightstep_catch_test( + name = "cloud_trace_propagation_test", + srcs = [ + "cloud_trace_propagation_test.cpp", + ], + deps = [ + "//:manual_tracer_lib", + "//test/recorder:in_memory_recorder_lib", + ":text_map_carrier_lib", + ":http_headers_carrier_lib", + ":utility_lib", + ], +) diff --git a/test/tracer/propagation/cloud_trace_propagation_test.cpp b/test/tracer/propagation/cloud_trace_propagation_test.cpp new file mode 100644 index 00000000..fbbce19e --- /dev/null +++ b/test/tracer/propagation/cloud_trace_propagation_test.cpp @@ -0,0 +1,162 @@ +#include + +#include "test/recorder/in_memory_recorder.h" +#include "test/tracer/propagation/http_headers_carrier.h" +#include "test/tracer/propagation/text_map_carrier.h" +#include "test/tracer/propagation/utility.h" +#include "tracer/legacy/legacy_tracer_impl.h" +#include "tracer/tracer_impl.h" + +#include "3rd_party/catch2/catch.hpp" +using namespace lightstep; + +TEST_CASE("cloud_trace propagation") { + LightStepTracerOptions tracer_options; + tracer_options.propagation_modes = {PropagationMode::cloud_trace}; + auto recorder = new InMemoryRecorder{}; + auto tracer = std::shared_ptr{ + new TracerImpl{MakePropagationOptions(tracer_options), + std::unique_ptr{recorder}}}; + std::unordered_map text_map; + TextMapCarrier text_map_carrier{text_map}; + HTTPHeadersCarrier http_headers_carrier{text_map}; + + SECTION("Inject, extract yields the same span context.") { + for (auto use_128bit_trace_ids : {true, false}) { + auto test_span_contexts = MakeTestSpanContexts(use_128bit_trace_ids); + for (auto& span_context : test_span_contexts) { + // text map carrier + CHECK_NOTHROW( + VerifyInjectExtract(*tracer, *span_context, text_map_carrier)); + text_map.clear(); + + // http headers carrier + CHECK_NOTHROW( + VerifyInjectExtract(*tracer, *span_context, http_headers_carrier)); + text_map.clear(); + } + } + } + + SECTION("Verify extraction against a long-form header with sampled set to true") { + text_map = {{"x-cloud-trace-context", "aef5705a090040838f1359ebafa5c0c6/12607106263893950595;o=1"}}; + auto span_context_maybe = tracer->Extract(http_headers_carrier); + REQUIRE(span_context_maybe); + auto span_context = + dynamic_cast(span_context_maybe->get()); + REQUIRE(span_context->trace_id_high() == 0xaef5705a09004083ul); + REQUIRE(span_context->trace_id_low() == 0x8f1359ebafa5c0c6ul); + REQUIRE(span_context->span_id() == 0xaef5705a09004083ul); + REQUIRE(span_context->sampled() == true); + } + + SECTION("Verify extraction against a long-form header when sampled is false") { + text_map = {{"x-cloud-trace-context", "aef5705a090040838f1359ebafa5c0c6/12607106263893950595;o=0"}}; + auto span_context_maybe = tracer->Extract(http_headers_carrier); + REQUIRE(span_context_maybe); + auto span_context = + dynamic_cast(span_context_maybe->get()); + REQUIRE(span_context->trace_id_high() == 0xaef5705a09004083ul); + REQUIRE(span_context->trace_id_low() == 0x8f1359ebafa5c0c6ul); + REQUIRE(span_context->span_id() == 0xaef5705a09004083ul); + REQUIRE(span_context->sampled() == false); + } + + SECTION("Verify error handling against a long-form header when sampled flag is invalid") { + text_map = {{"x-cloud-trace-context", "aef5705a090040838f1359ebafa5c0c6/12607106263893950595;__0"}}; + auto span_context_maybe = tracer->Extract(http_headers_carrier); + CHECK(!span_context_maybe); + CHECK(span_context_maybe.error() == + std::errc::invalid_argument); + } + + SECTION("Verify extraction against a medium-form header (trace id/span id)") { + text_map = {{"x-cloud-trace-context", "aef5705a090040838f1359ebafa5c0c6/12607106263893950595"}}; + auto span_context_maybe = tracer->Extract(http_headers_carrier); + REQUIRE(span_context_maybe); + auto span_context = + dynamic_cast(span_context_maybe->get()); + REQUIRE(span_context->trace_id_high() == 0xaef5705a09004083ul); + REQUIRE(span_context->trace_id_low() == 0x8f1359ebafa5c0c6ul); + REQUIRE(span_context->span_id() == 0xaef5705a09004083ul); + REQUIRE(span_context->sampled() == true); + } + + SECTION("Verify extraction against a medium-form header with a short span id(trace id/span id)") { + text_map = {{"x-cloud-trace-context", "aef5705a090040838f1359ebafa5c0c6/123"}}; + auto span_context_maybe = tracer->Extract(http_headers_carrier); + REQUIRE(span_context_maybe); + auto span_context = + dynamic_cast(span_context_maybe->get()); + REQUIRE(span_context->trace_id_high() == 0xaef5705a09004083ul); + REQUIRE(span_context->trace_id_low() == 0x8f1359ebafa5c0c6ul); + REQUIRE(span_context->span_id() == 0x7b); + REQUIRE(span_context->sampled() == true); + } + + SECTION("Verify error handling against a medium-form header with a bad separator (trace id/span id)") { + text_map = {{"x-cloud-trace-context", "aef5705a090040838f1359ebafa5c0c6_1"}}; + auto span_context_maybe = tracer->Extract(http_headers_carrier); + CHECK(!span_context_maybe); + CHECK(span_context_maybe.error() == + std::errc::invalid_argument); + } + + SECTION("Verify error handling against a medium-form header with a span ID that is out of range of a 64bit unsigned integer") { + text_map = {{"x-cloud-trace-context", "aef5705a090040838f1359ebafa5c0c6/99999999999999999999"}}; + auto span_context_maybe = tracer->Extract(http_headers_carrier); + CHECK(!span_context_maybe); + CHECK(span_context_maybe.error() == + std::errc::result_out_of_range); + } + + SECTION("Verify extraction against a short-form header (trace id)") { + text_map = {{"x-cloud-trace-context", "aef5705a090040838f1359ebafa5c0c6"}}; + auto span_context_maybe = tracer->Extract(http_headers_carrier); + REQUIRE(span_context_maybe); + auto span_context = + dynamic_cast(span_context_maybe->get()); + REQUIRE(span_context->trace_id_high() == 0xaef5705a09004083ul); + REQUIRE(span_context->trace_id_low() == 0x8f1359ebafa5c0c6ul); + REQUIRE(span_context->span_id() == 0x0); + REQUIRE(span_context->sampled() == true); + } + + SECTION("Verify error handling on too short a trace id") { + text_map = {{"x-cloud-trace-context", "AABB0011"}}; + auto span_context_maybe = tracer->Extract(http_headers_carrier); + CHECK(!span_context_maybe); + CHECK(span_context_maybe.error() == + std::errc::invalid_argument); + } + + SECTION("Verify error handling on invalid hex in trace id") { + text_map = {{"x-cloud-trace-context", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}}; + auto span_context_maybe = tracer->Extract(http_headers_carrier); + CHECK(!span_context_maybe); + CHECK(span_context_maybe.error() == + std::errc::invalid_argument); + } + + SECTION("A child keeps the same trace id as its parent") { + text_map = {{"x-cloud-trace-context", "aef5705a090040838f1359ebafa5c0c6/12607106263893950595;o=0"}}; + auto span_context_maybe = tracer->Extract(http_headers_carrier); + REQUIRE(span_context_maybe); + auto span = tracer->StartSpan( + "abc", {opentracing::ChildOf(span_context_maybe->get())}); + auto span_context1 = + dynamic_cast(span_context_maybe->get()); + auto span_context2 = + dynamic_cast(&span->context()); + REQUIRE(span_context1->trace_id_high() == span_context2->trace_id_high()); + REQUIRE(span_context1->trace_id_low() == span_context2->trace_id_low()); + } + + SECTION("The low part of 128-bit trace ids are sent to satellites") { + text_map = {{"x-cloud-trace-context", "aef5705a090040838f1359ebafa5c0c6/12607106263893950595;o=1"}}; + auto span_context_maybe = tracer->Extract(http_headers_carrier); + REQUIRE(span_context_maybe); + tracer->StartSpan("abc", {opentracing::ChildOf(span_context_maybe->get())}); + REQUIRE(recorder->top().span_context().trace_id() == 0x8f1359ebafa5c0c6ul); + } +} diff --git a/test/tracer/propagation/utility.cpp b/test/tracer/propagation/utility.cpp index 86c3d6a1..202f426b 100644 --- a/test/tracer/propagation/utility.cpp +++ b/test/tracer/propagation/utility.cpp @@ -17,6 +17,18 @@ static std::tuple GenerateTraceID(bool use_128bit_ids) { return {0, random_number_generator()}; } +//------------------------------------------------------------------------------ +// GenerateMaxTraceID +//------------------------------------------------------------------------------ +// Generates the maximum value a trace ID can possibly be +static std::tuple GenerateMaxTraceID(bool use_128bit_ids) { + if (use_128bit_ids) { + return {std::numeric_limits::max(), std::numeric_limits::max()}; + } + return {0, std::numeric_limits::max()}; +} + + //------------------------------------------------------------------------------ // MakeTestSpanContexts //------------------------------------------------------------------------------ @@ -54,6 +66,13 @@ std::vector> MakeTestSpanContexts( trace_id_high, trace_id_low, 456, false, std::unordered_map{}}}); + // max values span context + std::tie(trace_id_high, trace_id_low) = GenerateMaxTraceID(use_128bit_trace_ids); + result.push_back( + std::unique_ptr{new ImmutableSpanContext{ + trace_id_high, trace_id_low, std::numeric_limits::max(), true, + std::unordered_map{}}}); + return result; } } // namespace lightstep