|
| 1 | +#include "tracer/propagation/cloud_trace_propagator.h" |
| 2 | + |
| 3 | +#include "common/hex_conversion.h" |
| 4 | + |
| 5 | +#include "tracer/propagation/binary_propagation.h" |
| 6 | +#include "tracer/propagation/utility.h" |
| 7 | + |
| 8 | +const opentracing::string_view PropagationSingleKey = "x-cloud-trace-context"; |
| 9 | +const opentracing::string_view PrefixBaggage = "ot-baggage-"; |
| 10 | + |
| 11 | +namespace lightstep { |
| 12 | +//-------------------------------------------------------------------------------------------------- |
| 13 | +// InjectSpanContext |
| 14 | +//-------------------------------------------------------------------------------------------------- |
| 15 | +opentracing::expected<void> CloudTracePropagator::InjectSpanContext( |
| 16 | + const opentracing::TextMapWriter& carrier, |
| 17 | + const TraceContext& trace_context, opentracing::string_view /*trace_state*/, |
| 18 | + const BaggageProtobufMap& /*baggage*/) const { |
| 19 | + return this->InjectSpanContextImpl(carrier, trace_context); |
| 20 | +} |
| 21 | + |
| 22 | +opentracing::expected<void> CloudTracePropagator::InjectSpanContext( |
| 23 | + const opentracing::TextMapWriter& carrier, |
| 24 | + const TraceContext& trace_context, opentracing::string_view /*trace_state*/, |
| 25 | + const BaggageFlatMap& /*baggage*/) const { |
| 26 | + return this->InjectSpanContextImpl(carrier, trace_context); |
| 27 | +} |
| 28 | +//-------------------------------------------------------------------------------------------------- |
| 29 | +// ExtractSpanContext |
| 30 | +//-------------------------------------------------------------------------------------------------- |
| 31 | +opentracing::expected<bool> CloudTracePropagator::ExtractSpanContext( |
| 32 | + const opentracing::TextMapReader& carrier, bool case_sensitive, |
| 33 | + TraceContext& trace_context, std::string& /*trace_state*/, |
| 34 | + BaggageProtobufMap& baggage) const { |
| 35 | + auto iequals = |
| 36 | + [](opentracing::string_view lhs, opentracing::string_view rhs) noexcept { |
| 37 | + return lhs.length() == rhs.length() && |
| 38 | + std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs), |
| 39 | + [](char a, char b) { |
| 40 | + return std::tolower(a) == std::tolower(b); |
| 41 | + }); |
| 42 | + }; |
| 43 | + opentracing::expected<bool> result; |
| 44 | + if (case_sensitive) { |
| 45 | + result = this->ExtractSpanContextImpl(carrier, trace_context, |
| 46 | + std::equal_to<opentracing::string_view>{}, baggage); |
| 47 | + } else { |
| 48 | + result = this->ExtractSpanContextImpl(carrier, trace_context, iequals, baggage); |
| 49 | + } |
| 50 | + if (!result || !*result) { |
| 51 | + return result; |
| 52 | + } |
| 53 | + |
| 54 | + return result; |
| 55 | +} |
| 56 | + |
| 57 | +//-------------------------------------------------------------------------------------------------- |
| 58 | +// InjectSpanContextImpl |
| 59 | +//-------------------------------------------------------------------------------------------------- |
| 60 | +opentracing::expected<void> CloudTracePropagator::InjectSpanContextImpl( |
| 61 | + const opentracing::TextMapWriter& carrier, |
| 62 | + const TraceContext& trace_context) const { |
| 63 | + std::array<char, CloudContextLength> buffer; |
| 64 | + auto data_length = this->SerializeCloudTrace(trace_context, buffer.data()); |
| 65 | + return carrier.Set(PropagationSingleKey, |
| 66 | + opentracing::string_view{buffer.data(), data_length}); |
| 67 | +} |
| 68 | + |
| 69 | +//-------------------------------------------------------------------------------------------------- |
| 70 | +// ExtractSpanContextImpl |
| 71 | +//-------------------------------------------------------------------------------------------------- |
| 72 | +template <class KeyCompare> |
| 73 | +opentracing::expected<bool> CloudTracePropagator::ExtractSpanContextImpl( |
| 74 | + const opentracing::TextMapReader& carrier, TraceContext& trace_context, const KeyCompare& key_compare, |
| 75 | + BaggageProtobufMap& baggage) const { |
| 76 | + bool parent_header_found = false; |
| 77 | + auto result = |
| 78 | + carrier.ForeachKey([&](opentracing::string_view key, |
| 79 | + opentracing::string_view |
| 80 | + value) noexcept->opentracing::expected<void> { |
| 81 | + if (key_compare(key, PropagationSingleKey)) { |
| 82 | + auto was_successful = this->ParseCloudTrace(value, trace_context); |
| 83 | + if (!was_successful) { |
| 84 | + return opentracing::make_unexpected(was_successful.error()); |
| 85 | + } |
| 86 | + parent_header_found = true; |
| 87 | + } else if (key.length() > PrefixBaggage.size() && |
| 88 | + key_compare(opentracing::string_view{key.data(), |
| 89 | + PrefixBaggage.size()}, |
| 90 | + PrefixBaggage)) { |
| 91 | + baggage.insert(BaggageProtobufMap::value_type( |
| 92 | + ToLower( |
| 93 | + opentracing::string_view{key.data() + PrefixBaggage.size(), |
| 94 | + key.size() - PrefixBaggage.size()}), |
| 95 | + value)); |
| 96 | + } |
| 97 | + return {}; |
| 98 | + }); |
| 99 | + if (!result) { |
| 100 | + return opentracing::make_unexpected(result.error()); |
| 101 | + } |
| 102 | + return parent_header_found; |
| 103 | +} |
| 104 | + |
| 105 | +opentracing::expected<void> CloudTracePropagator::ParseCloudTrace( |
| 106 | + opentracing::string_view s, TraceContext& trace_context) const noexcept { |
| 107 | + if (s.size() < Num128BitHexDigits) { |
| 108 | + return opentracing::make_unexpected( |
| 109 | + std::make_error_code(std::errc::invalid_argument)); |
| 110 | + } |
| 111 | + size_t offset = 0; |
| 112 | + |
| 113 | + // default sampled to on (this comes from the ;o=1 part of |
| 114 | + // x-cloud-trace-context; if it is not set we will default to sampling |
| 115 | + // this request) |
| 116 | + trace_context.trace_flags = SetTraceFlag<SampledFlagMask>(trace_context.trace_flags, true); |
| 117 | + |
| 118 | + // trace-id |
| 119 | + auto error_maybe = NormalizedHexToUint128( |
| 120 | + opentracing::string_view{s.data() + offset, Num128BitHexDigits}, |
| 121 | + trace_context.trace_id_high, trace_context.trace_id_low); |
| 122 | + if (!error_maybe) { |
| 123 | + return error_maybe; |
| 124 | + } |
| 125 | + |
| 126 | + offset += Num128BitHexDigits; |
| 127 | + if (s.size() - offset < 2) { |
| 128 | + // only a short form trace ID has been given (not a "trace id/span id") |
| 129 | + return {}; |
| 130 | + } |
| 131 | + |
| 132 | + if (s[offset] != '/') { |
| 133 | + return opentracing::make_unexpected( |
| 134 | + std::make_error_code(std::errc::invalid_argument)); |
| 135 | + } |
| 136 | + ++offset; |
| 137 | + |
| 138 | + std::array<char, Num64BitDecimalDigits + 1> parent_id; |
| 139 | + size_t i; |
| 140 | + for (i=0; i < Num64BitDecimalDigits; ++i) { |
| 141 | + if (offset == s.length() || std::isdigit(s[offset]) == 0) { |
| 142 | + break; |
| 143 | + } |
| 144 | + parent_id[i] = s[offset]; |
| 145 | + ++offset; |
| 146 | + } |
| 147 | + parent_id[i] = '\0'; |
| 148 | + |
| 149 | + // parent-id |
| 150 | + errno = 0; |
| 151 | + trace_context.parent_id = std::strtoull(parent_id.data(), nullptr, 10); |
| 152 | + if (errno == ERANGE) { |
| 153 | + return opentracing::make_unexpected( |
| 154 | + std::make_error_code(std::errc::result_out_of_range)); |
| 155 | + } |
| 156 | + |
| 157 | + if (s.size() - offset < 4) { |
| 158 | + // only a "trace ID/span ID" has been given (not a "trace id/span id;o=[0-1]") |
| 159 | + return {}; |
| 160 | + } |
| 161 | + |
| 162 | + if(opentracing::string_view(s.begin() + offset, 3) != opentracing::string_view(";o=")) { |
| 163 | + return opentracing::make_unexpected( |
| 164 | + std::make_error_code(std::errc::invalid_argument)); |
| 165 | + } |
| 166 | + |
| 167 | + offset += 3; |
| 168 | + |
| 169 | + // trace-flags |
| 170 | + if (s[offset] == '0') { |
| 171 | + // don't sample |
| 172 | + trace_context.trace_flags = SetTraceFlag<SampledFlagMask>(trace_context.trace_flags, false); |
| 173 | + } |
| 174 | + |
| 175 | + return {}; |
| 176 | +} |
| 177 | + |
| 178 | +size_t CloudTracePropagator::SerializeCloudTrace(const TraceContext& trace_context, |
| 179 | + char* s) const noexcept { |
| 180 | + size_t offset = 0; |
| 181 | + // trace-id |
| 182 | + Uint64ToHex(trace_context.trace_id_high, s); |
| 183 | + offset += Num64BitHexDigits; |
| 184 | + Uint64ToHex(trace_context.trace_id_low, s + offset); |
| 185 | + offset += Num64BitHexDigits; |
| 186 | + |
| 187 | + offset += snprintf(s + offset, CloudContextLength - offset, "/%lu;o=%d", trace_context.parent_id, IsTraceFlagSet<SampledFlagMask>(trace_context.trace_flags) ? 1 : 0); |
| 188 | + |
| 189 | + return offset; |
| 190 | +} |
| 191 | +} // namespace lightstep |
0 commit comments