From c5dfaaa5a1cd60565c229e10ffadd936c3f594a0 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 5 Feb 2021 22:21:57 +0530 Subject: [PATCH 1/3] fix --- .../trace/propagation/http_trace_context.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/api/include/opentelemetry/trace/propagation/http_trace_context.h b/api/include/opentelemetry/trace/propagation/http_trace_context.h index 19de20d523..c5296cb3e5 100644 --- a/api/include/opentelemetry/trace/propagation/http_trace_context.h +++ b/api/include/opentelemetry/trace/propagation/http_trace_context.h @@ -174,7 +174,7 @@ class HttpTraceContext : public HTTPTextFormat } } - static void InjectTraceParent(const SpanContext &span_context, T &carrier, Setter setter) + static void InjectTraceHeaders(const SpanContext &span_context, T &carrier, Setter setter) { char trace_id[32]; TraceId(span_context.trace_id()).ToLowerBase16(trace_id); @@ -199,11 +199,12 @@ class HttpTraceContext : public HTTPTextFormat hex_string.push_back(trace_flags[i]); } setter(carrier, kTraceParent, hex_string); + setter(carrier, kTraceState, span_context.trace_state().ToHeader()); } static void InjectImpl(Setter setter, T &carrier, const SpanContext &span_context) { - InjectTraceParent(span_context, carrier, setter); + InjectTraceHeaders(span_context, carrier, setter); } static bool IsValidHex(nostd::string_view string_view) @@ -217,7 +218,7 @@ class HttpTraceContext : public HTTPTextFormat return true; } - static SpanContext ExtractContextFromTraceParent(nostd::string_view trace_parent) + 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] != '-' || @@ -247,22 +248,25 @@ class HttpTraceContext : public HTTPTextFormat { return SpanContext(false, false); } - + 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_obj, span_id_obj, trace_flags_obj, true, opentelemetry::trace::TraceState::FromHeader(trace_state)); } static SpanContext ExtractImpl(Getter getter, const T &carrier) { nostd::string_view trace_parent = getter(carrier, kTraceParent); + nostd::string_view trace_state = getter(carrier, kTraceState); if (trace_parent == "") { return SpanContext(false, false); } - return ExtractContextFromTraceParent(trace_parent); + return ExtractContextFromTraceHeaders(trace_parent, trace_state); } }; } // namespace propagation From 3844b8a792bef5e0cf25d25451e62584271a0950 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 26 Feb 2021 13:24:41 +0530 Subject: [PATCH 2/3] fix --- .../trace/propagation/http_trace_context.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/api/include/opentelemetry/trace/propagation/http_trace_context.h b/api/include/opentelemetry/trace/propagation/http_trace_context.h index bad73ce5a6..13f454b3fe 100644 --- a/api/include/opentelemetry/trace/propagation/http_trace_context.h +++ b/api/include/opentelemetry/trace/propagation/http_trace_context.h @@ -199,7 +199,7 @@ class HttpTraceContext : public TextMapPropagator hex_string.push_back(trace_flags[i]); } setter(carrier, kTraceParent, hex_string); - setter(carrier, kTraceState, span_context.trace_state().ToHeader()); + setter(carrier, kTraceState, span_context.trace_state()->ToHeader()); } static void InjectImpl(Setter setter, T &carrier, const SpanContext &span_context) @@ -218,7 +218,8 @@ class HttpTraceContext : public TextMapPropagator return true; } - static SpanContext ExtractContextFromTraceHeaders(nostd::string_view trace_parent, nostd::string_view trace_state) + 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] != '-' || @@ -248,13 +249,13 @@ class HttpTraceContext : public TextMapPropagator { return SpanContext(false, false); } - + 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, opentelemetry::trace::TraceState::FromHeader(trace_state)); + return SpanContext(trace_id_obj, span_id_obj, trace_flags_obj, true, + opentelemetry::trace::TraceState::FromHeader(trace_state)); } static SpanContext ExtractImpl(Getter getter, const T &carrier) From abf0e08d7317845b983a63b69bb105ed53193d83 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 26 Feb 2021 14:17:46 +0530 Subject: [PATCH 3/3] add tests --- .../propagation/http_text_format_test.cc | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/api/test/trace/propagation/http_text_format_test.cc b/api/test/trace/propagation/http_text_format_test.cc index d7b2dc5601..7e354d8555 100644 --- a/api/test/trace/propagation/http_text_format_test.cc +++ b/api/test/trace/propagation/http_text_format_test.cc @@ -85,6 +85,23 @@ TEST(TextMapPropagatorTest, NoSendEmptyTraceState) EXPECT_FALSE(carrier.count("tracestate") > 0); } +TEST(TextMapPropagatorTest, PropogateTraceState) +{ + const std::map carrier = { + {"traceparent", "00-4bf92f3577b34da6a3ce929d0e0e4736-0102030405060708-01"}, + {"tracestate", "congo=t61rcWkgMzE"}}; + context::Context ctx1 = context::Context{ + "current-span", + nostd::shared_ptr(new trace::DefaultSpan(trace::SpanContext::GetInvalid()))}; + context::Context ctx2 = format.Extract(Getter, carrier, ctx1); + std::map c2 = {}; + format.Inject(Setter, c2, ctx2); + + EXPECT_TRUE(carrier.count("traceparent") > 0); + EXPECT_TRUE(carrier.count("tracestate") > 0); + EXPECT_EQ(c2["tracestate"], "congo=t61rcWkgMzE"); +} + TEST(TextMapPropagatorTest, PropagateInvalidContext) { // Do not propagate invalid trace context. @@ -94,6 +111,7 @@ TEST(TextMapPropagatorTest, PropagateInvalidContext) nostd::shared_ptr(new trace::DefaultSpan(trace::SpanContext::GetInvalid()))}; format.Inject(Setter, carrier, ctx); EXPECT_TRUE(carrier.count("traceparent") == 0); + EXPECT_TRUE(carrier.count("tracestate") == 0); } TEST(TextMapPropagatorTest, SetRemoteSpan) @@ -118,8 +136,10 @@ TEST(TextMapPropagatorTest, GetCurrentSpan) { constexpr uint8_t buf_span[] = {1, 2, 3, 4, 5, 6, 7, 8}; constexpr uint8_t buf_trace[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; + + auto trace_state = trace::TraceState::FromHeader("congo=t61rcWkgMzE"); trace::SpanContext span_context{trace::TraceId{buf_trace}, trace::SpanId{buf_span}, - trace::TraceFlags{true}, false}; + trace::TraceFlags{true}, false, trace_state}; nostd::shared_ptr sp{new trace::DefaultSpan{span_context}}; // Set `sp` as the currently active span, which must be used by `Inject`. @@ -128,4 +148,5 @@ TEST(TextMapPropagatorTest, GetCurrentSpan) std::map headers = {}; format.Inject(Setter, headers, context::RuntimeContext::GetCurrent()); EXPECT_EQ(headers["traceparent"], "00-0102030405060708090a0b0c0d0e0f10-0102030405060708-01"); + EXPECT_EQ(headers["tracestate"], "congo=t61rcWkgMzE"); }