From 07f4a96bfebe62d276aa087b6a650653766dca8c Mon Sep 17 00:00:00 2001 From: Siim Kallas Date: Thu, 18 Mar 2021 13:17:13 +0200 Subject: [PATCH] Simplify propagators (#608) --- .../trace/propagation/b3_propagator.h | 175 ++++---------- .../trace/propagation/detail/hex.h | 4 +- .../trace/propagation/http_trace_context.h | 218 ++++++------------ api/test/trace/propagation/BUILD | 13 ++ .../trace/propagation/b3_propagation_test.cc | 46 +--- .../propagation/http_text_format_test.cc | 71 +++--- 6 files changed, 170 insertions(+), 357 deletions(-) diff --git a/api/include/opentelemetry/trace/propagation/b3_propagator.h b/api/include/opentelemetry/trace/propagation/b3_propagator.h index fed1a19c14..f3063be157 100644 --- a/api/include/opentelemetry/trace/propagation/b3_propagator.h +++ b/api/include/opentelemetry/trace/propagation/b3_propagator.h @@ -1,20 +1,12 @@ #pragma once -#include -#include -#include -#include #include "detail/context.h" -#include "opentelemetry/common/key_value_iterable.h" -#include "opentelemetry/context/context.h" -#include "opentelemetry/nostd/shared_ptr.h" -#include "opentelemetry/nostd/span.h" -#include "opentelemetry/nostd/string_view.h" -#include "opentelemetry/nostd/variant.h" +#include "detail/hex.h" +#include "detail/string.h" #include "opentelemetry/trace/default_span.h" #include "opentelemetry/trace/propagation/text_map_propagator.h" -#include "opentelemetry/trace/span.h" -#include "opentelemetry/trace/span_context.h" + +#include OPENTELEMETRY_BEGIN_NAMESPACE namespace trace @@ -38,9 +30,8 @@ static const nostd::string_view kB3SampledHeader = "X-B3-Sampled"; X-B3-TraceId X-B3-SpanId X-B3-ParentSpanId (ignored) */ -static const int kTraceIdHexStrLength = 32; -static const int kSpanIdHexStrLength = 16; -static const int kTraceFlagHexStrLength = 1; +static const int kTraceIdHexStrLength = 32; +static const int kSpanIdHexStrLength = 16; // The B3PropagatorExtractor class provides an interface that enables extracting context from // headers of HTTP requests. HTTP frameworks and clients can integrate with B3Propagator by @@ -63,21 +54,21 @@ class B3PropagatorExtractor : public TextMapPropagator return context.SetValue(kSpanKey, sp); } - static TraceId GenerateTraceIdFromString(nostd::string_view trace_id) + static TraceId TraceIdFromHex(nostd::string_view trace_id) { uint8_t buf[kTraceIdHexStrLength / 2]; - GenerateBuffFromHexStrPad0(trace_id, sizeof(buf), buf); + detail::HexToBinary(trace_id, buf, sizeof(buf)); return TraceId(buf); } - static SpanId GenerateSpanIdFromString(nostd::string_view span_id) + static SpanId SpanIdFromHex(nostd::string_view span_id) { uint8_t buf[kSpanIdHexStrLength / 2]; - GenerateBuffFromHexStrPad0(span_id, sizeof(buf), buf); + detail::HexToBinary(span_id, buf, sizeof(buf)); return SpanId(buf); } - static TraceFlags GenerateTraceFlagsFromString(nostd::string_view trace_flags) + static TraceFlags TraceFlagsFromHex(nostd::string_view trace_flags) { if (trace_flags.length() != 1 || (trace_flags[0] != '1' && trace_flags[0] != 'd')) { // check for invalid length of flags and treat 'd' as sampled @@ -87,106 +78,48 @@ class B3PropagatorExtractor : public TextMapPropagator } private: - // Converts hex numbers (string_view) into bytes stored in a buffer and pads buffer with 0. - static void GenerateBuffFromHexStrPad0(nostd::string_view hexStr, int bufSize, uint8_t *buf) - { // we are doing this starting from "right" side for left-padding - nostd::string_view::size_type posInp = hexStr.length(); - int posOut = bufSize; - while (posOut--) - { - int val = 0; - if (posInp) - { - int hexDigit2 = HexToInt(hexStr[--posInp]); // low nibble - int hexDigit1 = 0; - if (posInp) - { - hexDigit1 = HexToInt(hexStr[--posInp]); - } - if (hexDigit1 < 0 || hexDigit2 < 0) - { // malformed hex sequence. Fill entire buffer with zeroes. - for (int j = 0; j < bufSize; j++) - { - buf[j] = 0; - } - return; - } - val = hexDigit1 * 16 + hexDigit2; - } - buf[posOut] = val; - } - } - - // Converts a single character to a corresponding integer (e.g. '1' to 1), return -1 - // if the character is not a valid number in hex. - static int8_t HexToInt(char c) - { - if (c >= '0' && c <= '9') - { - return (int8_t)(c - '0'); - } - else if (c >= 'a' && c <= 'f') - { - return (int8_t)(c - 'a' + 10); - } - else if (c >= 'A' && c <= 'F') - { - return (int8_t)(c - 'A' + 10); - } - else - { - return -1; - } - } - static SpanContext ExtractImpl(Getter getter, const T &carrier) { - // all these are hex values - nostd::string_view trace_id; - nostd::string_view span_id; - nostd::string_view trace_flags; + nostd::string_view trace_id_hex; + nostd::string_view span_id_hex; + nostd::string_view trace_flags_hex; // first let's try a single-header variant auto singleB3Header = getter(carrier, kB3CombinedHeader); if (!singleB3Header.empty()) { - // From: https://github.com/openzipkin/b3-propagation/blob/master/RATIONALE.md - // trace_id can be 16 or 32 chars - auto firstSep = singleB3Header.find('-'); - trace_id = singleB3Header.substr(0, firstSep); - if (firstSep != nostd::string_view::npos) - { // at least two fields are required - auto secondSep = singleB3Header.find('-', firstSep + 1); - if (secondSep != nostd::string_view::npos) - { // more than two fields - check also trace_flags - span_id = singleB3Header.substr(firstSep + 1, secondSep - firstSep - 1); - if (secondSep + 1 < singleB3Header.size()) - { - trace_flags = singleB3Header.substr(secondSep + 1, kTraceFlagHexStrLength); - } - } - else - { - span_id = singleB3Header.substr(firstSep + 1); - } + std::array fields{}; + // https://github.com/openzipkin/b3-propagation/blob/master/RATIONALE.md + if (detail::SplitString(singleB3Header, '-', fields.data(), 3) < 2) + { + return SpanContext::GetInvalid(); } + + trace_id_hex = fields[0]; + span_id_hex = fields[1]; + trace_flags_hex = fields[2]; } else { - trace_id = getter(carrier, kB3TraceIdHeader); - span_id = getter(carrier, kB3SpanIdHeader); - trace_flags = getter(carrier, kB3SampledHeader); + trace_id_hex = getter(carrier, kB3TraceIdHeader); + span_id_hex = getter(carrier, kB3SpanIdHeader); + trace_flags_hex = getter(carrier, kB3SampledHeader); } - // now convert hex to objects - TraceId trace_id_obj = GenerateTraceIdFromString(trace_id); - SpanId span_id_obj = GenerateSpanIdFromString(span_id); - if (!trace_id_obj.IsValid() || !span_id_obj.IsValid()) + if (!detail::IsValidHex(trace_id_hex) || !detail::IsValidHex(span_id_hex)) { - return SpanContext(false, false); + return SpanContext::GetInvalid(); } - TraceFlags trace_flags_obj = GenerateTraceFlagsFromString(trace_flags); - return SpanContext(trace_id_obj, span_id_obj, trace_flags_obj, true); + + TraceId trace_id = TraceIdFromHex(trace_id_hex); + SpanId span_id = SpanIdFromHex(span_id_hex); + + if (!trace_id.IsValid() || !span_id.IsValid()) + { + return SpanContext::GetInvalid(); + } + + return SpanContext(trace_id, span_id, TraceFlagsFromHex(trace_flags_hex), true); } }; @@ -208,26 +141,18 @@ class B3Propagator : public B3PropagatorExtractor { return; } - char trace_id[kTraceIdHexStrLength]; - TraceId(span_context.trace_id()).ToLowerBase16(trace_id); - char span_id[kSpanIdHexStrLength]; - SpanId(span_context.span_id()).ToLowerBase16(span_id); - char trace_flags[2]; - TraceFlags(span_context.trace_flags()).ToLowerBase16(trace_flags); - // Note: This is only temporary replacement for appendable string - std::string hex_string = ""; - for (int i = 0; i < 32; i++) - { - hex_string.push_back(trace_id[i]); - } - hex_string.push_back('-'); - for (int i = 0; i < 16; i++) - { - hex_string.push_back(span_id[i]); - } - hex_string.push_back('-'); - hex_string.push_back(trace_flags[1]); - setter(carrier, kB3CombinedHeader, hex_string); + + char trace_identity[kTraceIdHexStrLength + kSpanIdHexStrLength + 3]; + static_assert(sizeof(trace_identity) == 51, "b3 trace identity buffer size mismatch"); + span_context.trace_id().ToLowerBase16({&trace_identity[0], kTraceIdHexStrLength}); + trace_identity[kTraceIdHexStrLength] = '-'; + span_context.span_id().ToLowerBase16( + {&trace_identity[kTraceIdHexStrLength + 1], kSpanIdHexStrLength}); + trace_identity[kTraceIdHexStrLength + kSpanIdHexStrLength + 1] = '-'; + trace_identity[kTraceIdHexStrLength + kSpanIdHexStrLength + 2] = + span_context.trace_flags().IsSampled() ? '1' : '0'; + + setter(carrier, kB3CombinedHeader, nostd::string_view(trace_identity, sizeof(trace_identity))); } }; diff --git a/api/include/opentelemetry/trace/propagation/detail/hex.h b/api/include/opentelemetry/trace/propagation/detail/hex.h index 06ced34d83..ebce903c6f 100644 --- a/api/include/opentelemetry/trace/propagation/detail/hex.h +++ b/api/include/opentelemetry/trace/propagation/detail/hex.h @@ -43,13 +43,13 @@ bool IsValidHex(nostd::string_view s) */ bool HexToBinary(nostd::string_view hex, uint8_t *buffer, size_t buffer_size) { + std::memset(buffer, 0, buffer_size); + if (hex.size() > buffer_size * 2) { return false; } - std::memset(buffer, 0, buffer_size); - int64_t hex_size = int64_t(hex.size()); int64_t buffer_pos = int64_t(buffer_size) - (hex_size + 1) / 2; int64_t last_hex_pos = hex_size - 1; diff --git a/api/include/opentelemetry/trace/propagation/http_trace_context.h b/api/include/opentelemetry/trace/propagation/http_trace_context.h index 3127c2f2d4..b1cc53d215 100644 --- a/api/include/opentelemetry/trace/propagation/http_trace_context.h +++ b/api/include/opentelemetry/trace/propagation/http_trace_context.h @@ -1,4 +1,4 @@ -// Copyright 2020, OpenTelemetry Authors +// Copyright 2021, OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,21 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include -#include -#include -#include #include "detail/context.h" -#include "opentelemetry/common/key_value_iterable.h" -#include "opentelemetry/context/context.h" -#include "opentelemetry/nostd/shared_ptr.h" -#include "opentelemetry/nostd/span.h" -#include "opentelemetry/nostd/string_view.h" -#include "opentelemetry/nostd/variant.h" +#include "detail/hex.h" +#include "detail/string.h" #include "opentelemetry/trace/default_span.h" #include "opentelemetry/trace/propagation/text_map_propagator.h" -#include "opentelemetry/trace/span.h" -#include "opentelemetry/trace/span_context.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace trace @@ -35,23 +25,17 @@ namespace propagation { static const nostd::string_view kTraceParent = "traceparent"; static const nostd::string_view kTraceState = "tracestate"; -static const int kTraceDelimiterBytes = 3; -static const int kHeaderElementLengths[4] = { - 2, 32, 16, 2}; // 0: version, 1: trace id, 2: span id, 3: trace flags -static const int kHeaderSize = kHeaderElementLengths[0] + kHeaderElementLengths[1] + - kHeaderElementLengths[2] + kHeaderElementLengths[3] + - kTraceDelimiterBytes; -static const int kTraceStateMaxMembers = 32; -static const int kVersionBytes = 2; -static const int kTraceIdBytes = 32; -static const int kSpanIdBytes = 16; -static const int kTraceFlagBytes = 2; +static const size_t kVersionSize = 2; +static const size_t kTraceIdSize = 32; +static const size_t kSpanIdSize = 16; +static const size_t kTraceFlagsSize = 2; +static const size_t kTraceParentSize = 55; // The HttpTraceContext provides methods to extract and inject // context into headers of HTTP requests with traces. // Example: -// HttpTraceContext.inject(setter,&carrier,&context); -// HttpTraceContext.extract(getter,&carrier,&context); +// HttpTraceContext().inject(setter, carrier, context); +// HttpTraceContext().extract(getter, carrier, context); template class HttpTraceContext : public TextMapPropagator { @@ -83,168 +67,98 @@ class HttpTraceContext : public TextMapPropagator return context.SetValue(kSpanKey, sp); } - static TraceId GenerateTraceIdFromString(nostd::string_view trace_id) + static TraceId TraceIdFromHex(nostd::string_view trace_id) { - int trace_id_len = kHeaderElementLengths[1]; - uint8_t buf[kTraceIdBytes / 2]; - uint8_t *b_ptr = buf; - GenerateHexFromString(trace_id, trace_id_len, b_ptr); + uint8_t buf[kTraceIdSize / 2]; + detail::HexToBinary(trace_id, buf, sizeof(buf)); return TraceId(buf); } - static SpanId GenerateSpanIdFromString(nostd::string_view span_id) + static SpanId SpanIdFromHex(nostd::string_view span_id) { - int span_id_len = kHeaderElementLengths[2]; - uint8_t buf[kSpanIdBytes / 2]; - uint8_t *b_ptr = buf; - GenerateHexFromString(span_id, span_id_len, b_ptr); + uint8_t buf[kSpanIdSize / 2]; + detail::HexToBinary(span_id, buf, sizeof(buf)); return SpanId(buf); } - static TraceFlags GenerateTraceFlagsFromString(nostd::string_view trace_flags) + static TraceFlags TraceFlagsFromHex(nostd::string_view trace_flags) { - if (trace_flags.length() > 2) - { - return TraceFlags(0); // check for invalid length of flags - } - int tmp1 = HexToInt(trace_flags[0]); - int tmp2 = HexToInt(trace_flags[1]); - if (tmp1 < 0 || tmp2 < 0) - return TraceFlags(0); // check for invalid char - uint8_t buf = tmp1 * 16 + tmp2; - return TraceFlags(buf); + uint8_t flags; + detail::HexToBinary(trace_flags, &flags, sizeof(flags)); + return TraceFlags(flags); } private: - // Converts the hex numbers stored as strings into bytes stored in a buffer. - static void GenerateHexFromString(nostd::string_view string, int bytes, uint8_t *buf) + static constexpr uint8_t kInvalidVersion = 0xFF; + + static bool IsValidVersion(nostd::string_view version_hex) { - const char *str_id = string.data(); - for (int i = 0; i < bytes; i++) - { - int tmp = HexToInt(str_id[i]); - if (tmp < 0) - { - for (int j = 0; j < bytes / 2; j++) - { - buf[j] = 0; - } - return; - } - if (i % 2 == 0) - { - buf[i / 2] = tmp * 16; - } - else - { - buf[i / 2] += tmp; - } - } + uint8_t version; + detail::HexToBinary(version_hex, &version, sizeof(version)); + return version != kInvalidVersion; } - // Converts a single character to a corresponding integer (e.g. '1' to 1), return -1 - // if the character is not a valid number in hex. - static uint8_t HexToInt(char c) + static void InjectImpl(Setter setter, T &carrier, const SpanContext &span_context) { - if (c >= '0' && c <= '9') - { - return (int)(c - '0'); - } - else if (c >= 'a' && c <= 'f') - { - return (int)(c - 'a' + 10); - } - else if (c >= 'A' && c <= 'F') - { - return (int)(c - 'A' + 10); - } - else - { - return -1; - } + char trace_parent[kTraceParentSize]; + trace_parent[0] = '0'; + trace_parent[1] = '0'; + trace_parent[2] = '-'; + span_context.trace_id().ToLowerBase16({&trace_parent[3], kTraceIdSize}); + trace_parent[kTraceIdSize + 3] = '-'; + span_context.span_id().ToLowerBase16({&trace_parent[kTraceIdSize + 4], kSpanIdSize}); + trace_parent[kTraceIdSize + kSpanIdSize + 4] = '-'; + span_context.trace_flags().ToLowerBase16({&trace_parent[kTraceIdSize + kSpanIdSize + 5], 2}); + + setter(carrier, kTraceParent, nostd::string_view(trace_parent, sizeof(trace_parent))); + setter(carrier, kTraceState, span_context.trace_state()->ToHeader()); } - static void InjectTraceHeaders(const SpanContext &span_context, T &carrier, Setter setter) + static SpanContext ExtractContextFromTraceHeaders(nostd::string_view trace_parent, + nostd::string_view trace_state) { - char trace_id[32]; - TraceId(span_context.trace_id()).ToLowerBase16(trace_id); - char span_id[16]; - SpanId(span_context.span_id()).ToLowerBase16(span_id); - char trace_flags[2]; - TraceFlags(span_context.trace_flags()).ToLowerBase16(trace_flags); - // Note: This is only temporary replacement for appendable string - std::string hex_string = "00-"; - for (int i = 0; i < 32; i++) - { - hex_string.push_back(trace_id[i]); - } - hex_string.push_back('-'); - for (int i = 0; i < 16; i++) + if (trace_parent.size() != kTraceParentSize) { - hex_string.push_back(span_id[i]); + return SpanContext::GetInvalid(); } - hex_string.push_back('-'); - for (int i = 0; i < 2; i++) + + std::array fields{}; + if (detail::SplitString(trace_parent, '-', fields.data(), 4) != 4) { - hex_string.push_back(trace_flags[i]); + return SpanContext::GetInvalid(); } - setter(carrier, kTraceParent, hex_string); - setter(carrier, kTraceState, span_context.trace_state()->ToHeader()); - } - static void InjectImpl(Setter setter, T &carrier, const SpanContext &span_context) - { - InjectTraceHeaders(span_context, carrier, setter); - } + nostd::string_view version_hex = fields[0]; + nostd::string_view trace_id_hex = fields[1]; + nostd::string_view span_id_hex = fields[2]; + nostd::string_view trace_flags_hex = fields[3]; - static bool IsValidHex(nostd::string_view string_view) - { - for (int i = 0; i < string_view.length(); i++) + if (version_hex.size() != kVersionSize || trace_id_hex.size() != kTraceIdSize || + span_id_hex.size() != kSpanIdSize || trace_flags_hex.size() != kTraceFlagsSize) { - if (!(string_view[i] >= '0' && string_view[i] <= '9') && - !(string_view[i] >= 'a' && string_view[i] <= 'f')) - return false; + return SpanContext::GetInvalid(); } - return true; - } - static SpanContext ExtractContextFromTraceHeaders(nostd::string_view trace_parent, - nostd::string_view trace_state) - { - if (trace_parent.length() != kHeaderSize || trace_parent[kHeaderElementLengths[0]] != '-' || - trace_parent[kHeaderElementLengths[0] + kHeaderElementLengths[1] + 1] != '-' || - trace_parent[kHeaderElementLengths[0] + kHeaderElementLengths[1] + - kHeaderElementLengths[2] + 2] != '-') + if (!detail::IsValidHex(version_hex) || !detail::IsValidHex(trace_id_hex) || + !detail::IsValidHex(span_id_hex) || !detail::IsValidHex(trace_flags_hex)) { - std::cout << "Unparseable trace_parent header. Returning INVALID span context." << std::endl; - return SpanContext(false, false); + return SpanContext::GetInvalid(); } - nostd::string_view version = trace_parent.substr(0, kHeaderElementLengths[0]); - nostd::string_view trace_id = - trace_parent.substr(kHeaderElementLengths[0] + 1, kHeaderElementLengths[1]); - nostd::string_view span_id = trace_parent.substr( - kHeaderElementLengths[0] + kHeaderElementLengths[1] + 2, kHeaderElementLengths[2]); - nostd::string_view trace_flags = trace_parent.substr( - kHeaderElementLengths[0] + kHeaderElementLengths[1] + kHeaderElementLengths[2] + 3); - if (version == "ff" || trace_id == "00000000000000000000000000000000" || - span_id == "0000000000000000") + if (!IsValidVersion(version_hex)) { - return SpanContext(false, false); + return SpanContext::GetInvalid(); } - // validate ids - if (!IsValidHex(version) || !IsValidHex(trace_id) || !IsValidHex(span_id) || - !IsValidHex(trace_flags)) + TraceId trace_id = TraceIdFromHex(trace_id_hex); + SpanId span_id = SpanIdFromHex(span_id_hex); + + if (!trace_id.IsValid() || !span_id.IsValid()) { - return SpanContext(false, false); + return SpanContext::GetInvalid(); } - TraceId trace_id_obj = GenerateTraceIdFromString(trace_id); - SpanId span_id_obj = GenerateSpanIdFromString(span_id); - TraceFlags trace_flags_obj = GenerateTraceFlagsFromString(trace_flags); - - return SpanContext(trace_id_obj, span_id_obj, trace_flags_obj, true, + return SpanContext(trace_id, span_id, TraceFlagsFromHex(trace_flags_hex), true, opentelemetry::trace::TraceState::FromHeader(trace_state)); } @@ -254,7 +168,7 @@ class HttpTraceContext : public TextMapPropagator nostd::string_view trace_state = getter(carrier, kTraceState); if (trace_parent == "") { - return SpanContext(false, false); + return SpanContext::GetInvalid(); } return ExtractContextFromTraceHeaders(trace_parent, trace_state); diff --git a/api/test/trace/propagation/BUILD b/api/test/trace/propagation/BUILD index 4de46a7dae..da1fa659e2 100644 --- a/api/test/trace/propagation/BUILD +++ b/api/test/trace/propagation/BUILD @@ -4,6 +4,19 @@ cc_test( name = "http_text_format_test", srcs = [ "http_text_format_test.cc", + "util.h", + ], + deps = [ + "//api", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "b3_propagation_test", + srcs = [ + "b3_propagation_test.cc", + "util.h", ], deps = [ "//api", diff --git a/api/test/trace/propagation/b3_propagation_test.cc b/api/test/trace/propagation/b3_propagation_test.cc index 2a5be187e9..d1bacaec8c 100644 --- a/api/test/trace/propagation/b3_propagation_test.cc +++ b/api/test/trace/propagation/b3_propagation_test.cc @@ -1,34 +1,14 @@ -#include "opentelemetry/context/context.h" -#include "opentelemetry/nostd/shared_ptr.h" -#include "opentelemetry/nostd/span.h" -#include "opentelemetry/nostd/string_view.h" -#include "opentelemetry/trace/default_span.h" -#include "opentelemetry/trace/noop.h" -#include "opentelemetry/trace/span.h" -#include "opentelemetry/trace/span_context.h" -#include "opentelemetry/trace/trace_id.h" -#include "opentelemetry/trace/tracer.h" +#include "opentelemetry/context/runtime_context.h" +#include "opentelemetry/trace/propagation/b3_propagator.h" +#include "opentelemetry/trace/scope.h" +#include "util.h" #include -#include -#include #include -#include "opentelemetry/trace/default_span.h" -#include "opentelemetry/trace/propagation/b3_propagator.h" -#include "opentelemetry/trace/propagation/text_map_propagator.h" - using namespace opentelemetry; -template -static std::string Hex(const T &id_item) -{ - char buf[T::kSize * 2]; - id_item.ToLowerBase16(buf); - return std::string(buf, sizeof(buf)); -} - static nostd::string_view Getter(const std::map &carrier, nostd::string_view key) { @@ -56,24 +36,10 @@ using MapB3ContextMultiHeader = static MapB3ContextMultiHeader formatMultiHeader = MapB3ContextMultiHeader(); -TEST(B3PropagationTest, TraceIdBufferGeneration) -{ - constexpr uint8_t buf[] = {1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}; - EXPECT_EQ(MapB3Context::GenerateTraceIdFromString("01020304050607080807aabbccddeeff"), - trace::TraceId(buf)); -} - -TEST(B3PropagationTest, SpanIdBufferGeneration) -{ - constexpr uint8_t buf[] = {1, 2, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}; - EXPECT_EQ(MapB3Context::GenerateSpanIdFromString("0102aabbccddeeff"), trace::SpanId(buf)); -} - TEST(B3PropagationTest, TraceFlagsBufferGeneration) { - EXPECT_EQ(MapB3Context::GenerateTraceFlagsFromString("0"), trace::TraceFlags()); - EXPECT_EQ(MapB3Context::GenerateTraceFlagsFromString("1"), - trace::TraceFlags(trace::TraceFlags::kIsSampled)); + EXPECT_EQ(MapB3Context::TraceFlagsFromHex("0"), trace::TraceFlags()); + EXPECT_EQ(MapB3Context::TraceFlagsFromHex("1"), trace::TraceFlags(trace::TraceFlags::kIsSampled)); } TEST(B3PropagationTest, PropagateInvalidContext) diff --git a/api/test/trace/propagation/http_text_format_test.cc b/api/test/trace/propagation/http_text_format_test.cc index 7e354d8555..c0dff1c17f 100644 --- a/api/test/trace/propagation/http_text_format_test.cc +++ b/api/test/trace/propagation/http_text_format_test.cc @@ -1,34 +1,14 @@ -#include "opentelemetry/context/context.h" -#include "opentelemetry/nostd/shared_ptr.h" -#include "opentelemetry/nostd/span.h" -#include "opentelemetry/nostd/string_view.h" -#include "opentelemetry/trace/default_span.h" -#include "opentelemetry/trace/noop.h" -#include "opentelemetry/trace/span.h" -#include "opentelemetry/trace/span_context.h" -#include "opentelemetry/trace/trace_id.h" -#include "opentelemetry/trace/tracer.h" +#include "opentelemetry/context/runtime_context.h" +#include "opentelemetry/trace/propagation/http_trace_context.h" +#include "opentelemetry/trace/scope.h" +#include "util.h" #include -#include -#include #include -#include "opentelemetry/trace/default_span.h" -#include "opentelemetry/trace/propagation/http_trace_context.h" -#include "opentelemetry/trace/propagation/text_map_propagator.h" - using namespace opentelemetry; -template -static std::string Hex(const T &id_item) -{ - char buf[T::kSize * 2]; - id_item.ToLowerBase16(buf); - return std::string(buf, sizeof(buf)); -} - static nostd::string_view Getter(const std::map &carrier, nostd::string_view trace_type = "traceparent") { @@ -52,22 +32,9 @@ using MapHttpTraceContext = static MapHttpTraceContext format = MapHttpTraceContext(); -TEST(TextMapPropagatorTest, TraceIdBufferGeneration) -{ - constexpr uint8_t buf[] = {1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}; - EXPECT_EQ(MapHttpTraceContext::GenerateTraceIdFromString("01020304050607080807aabbccddeeff"), - trace::TraceId(buf)); -} - -TEST(TextMapPropagatorTest, SpanIdBufferGeneration) -{ - constexpr uint8_t buf[] = {1, 2, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}; - EXPECT_EQ(MapHttpTraceContext::GenerateSpanIdFromString("0102aabbccddeeff"), trace::SpanId(buf)); -} - TEST(TextMapPropagatorTest, TraceFlagsBufferGeneration) { - EXPECT_EQ(MapHttpTraceContext::GenerateTraceFlagsFromString("00"), trace::TraceFlags()); + EXPECT_EQ(MapHttpTraceContext::TraceFlagsFromHex("00"), trace::TraceFlags()); } TEST(TextMapPropagatorTest, NoSendEmptyTraceState) @@ -150,3 +117,31 @@ TEST(TextMapPropagatorTest, GetCurrentSpan) EXPECT_EQ(headers["traceparent"], "00-0102030405060708090a0b0c0d0e0f10-0102030405060708-01"); EXPECT_EQ(headers["tracestate"], "congo=t61rcWkgMzE"); } + +TEST(TextMapPropagatorTest, InvalidIdentitiesAreNotExtracted) +{ + std::vector traces = { + "ff-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01", + "00-0af7651916cd43dd8448eb211c80319c1-b9c7c989f97918e1-01", + "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e11-01", + "0-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01", + "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-0", + "00-0af7651916cd43dd8448eb211c8031-b9c7c989f97918e1-01", + "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97-01", + "00-1-1-00", + "00--b9c7c989f97918e1-01", + "00-0af7651916cd43dd8448eb211c80319c1--01", + "", + "---", + }; + + for (auto &trace : traces) + { + const std::map carrier = {{"traceparent", trace}}; + context::Context ctx1 = context::Context{}; + context::Context ctx2 = format.Extract(Getter, carrier, ctx1); + + auto span = trace::propagation::detail::GetCurrentSpan(ctx2); + EXPECT_FALSE(span.IsValid()); + } +}