diff --git a/api/include/opentelemetry/propagators/composite_http_propagator.h b/api/include/opentelemetry/propagators/composite_http_propagator.h index 75ba735e81..03ab39df60 100644 --- a/api/include/opentelemetry/propagators/composite_http_propagator.h +++ b/api/include/opentelemetry/propagators/composite_http_propagator.h @@ -24,7 +24,7 @@ namespace propagators // CompositeHTTPPropagator provides a mechanism for combining multiple // propagators into a single one. template - class CompositeHTTPPropagator : public trace::propagation::HTTPTextFormat + class CompositeHTTPPropagator : public trace::propagation::HTTPTextFormat { public: // Rules that manages how context will be extracted from carrier. using Getter = nostd::string_view(*)(const T &carrier, nostd::string_view trace_type); diff --git a/api/include/opentelemetry/trace/propagation/http_text_format.h b/api/include/opentelemetry/trace/propagation/http_text_format.h index 57302700b5..fde9597243 100644 --- a/api/include/opentelemetry/trace/propagation/http_text_format.h +++ b/api/include/opentelemetry/trace/propagation/http_text_format.h @@ -31,11 +31,6 @@ class HTTPTextFormat { // Sets the context for a HTTP header carrier with self defined rules. virtual void Inject(Setter set_from_carrier, T &carrier, const context::Context &context) = 0; - -// // Set the span in the given context. -// virtual context::Context SetSpanInContext(trace::Span* span, context::Context &context) = 0; -// // Retrieve the current span. -// virtual trace::Span* GetCurrentSpan(context::Context &context) = 0; }; } } diff --git a/api/include/opentelemetry/trace/propagation/http_trace_context.h b/api/include/opentelemetry/trace/propagation/http_trace_context.h index a5163e035c..fa5649edb6 100644 --- a/api/include/opentelemetry/trace/propagation/http_trace_context.h +++ b/api/include/opentelemetry/trace/propagation/http_trace_context.h @@ -36,7 +36,6 @@ namespace propagation { static const nostd::string_view kTraceParent = "traceparent"; static const nostd::string_view kTraceState = "tracestate"; -// Parameters no longer needed because the toString functions are resolved else where static const int kVersionBytes = 2; static const int kTraceIdBytes = 32; static const int kParentIdBytes = 16; @@ -174,7 +173,7 @@ class HttpTraceContext : public HTTPTextFormat { bool is_valid = trace_parent.length() == kHeaderSize && trace_parent[kVersionBytes] == '-' && trace_parent[kVersionBytes+kTraceIdBytes+1] == '-' - && trace_parent[kVersionBytes+kTraceIdBytes+kParentIdBytes+2] == '-'; + && trace_parent[kVersionBytes+kTraceIdBytes+kSpanIdBytes+2] == '-'; if (!is_valid) { std::cout<<"Unparseable trace_parent header. Returning INVALID span context."< { } nostd::string_view trace_state_header = getter(carrier, kTraceState); -// trace_state_header = nostd::string_view(carrier[std::string(kTraceState)]); if (trace_state_header == "" || trace_state_header.empty()) { return context_from_parent_header; @@ -300,22 +298,21 @@ class HttpTraceContext : public HTTPTextFormat { trace_state, true ); -// return SpanContext.CreateFromRemoteParent( -// context_from_parent_header.GetTraceId(), -// context_from_parent_header.GetSpanId(), -// context_from_parent_header.GetTraceFlags(), -// trace_state -// ); } catch (std::exception& e) { std::cout<<"Unparseable tracestate header. Returning span context without state."< &carrier, nostd::string_vie static trace::propagation::HttpTraceContext> format = trace::propagation::HttpTraceContext>(); -static nostd::string_view trace_id = "12345678901234567890123456789012"; -static nostd::string_view span_id = "1234567890123456"; +static const nostd::string_view trace_id = "12345678901234567890123456789012"; +static const nostd::string_view span_id = "1234567890123456"; -using map_http_trace_context = trace::propagation::HttpTraceContext>; +using MapHttpTraceContext = trace::propagation::HttpTraceContext>; TEST(HTTPTextFormatTest, TraceIdBufferGeneration) { -EXPECT_EQ(map_http_trace_context::GenerateTraceIdFromString( - "01020304050607080807aabbccddeeff"), - trace::TraceId({1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff})); + 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(HTTPTextFormatTest, SpanIdBufferGeneration) { constexpr uint8_t buf[] = {1, 2, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}; - trace::SpanId id(buf); - EXPECT_EQ(map_http_trace_context::GenerateSpanIdFromString("0102aabbccddeeff"),id); + EXPECT_EQ(MapHttpTraceContext::GenerateSpanIdFromString("0102aabbccddeeff"),trace::SpanId(buf)); } TEST(HTTPTextFormatTest, TraceFlagsBufferGeneration) { - trace::TraceFlags flags; - EXPECT_EQ(map_http_trace_context::GenerateTraceFlagsFromString("00"),flags); + EXPECT_EQ(MapHttpTraceContext::GenerateTraceFlagsFromString("00"),trace::TraceFlags()); } TEST(HTTPTextFormatTest, HeadersWithTraceState) { const std::map carrier = {{"traceparent","00-4bf92f3577b34da6a3ce929d0e0e4736-0102030405060708-01"},{"tracestate","congo=congosSecondPosition,rojo=rojosFirstPosition"}}; - nostd::shared_ptr sp{new trace::DefaultSpan()}; - context::Context ctx1 = context::Context("current-span",sp); + context::Context ctx1 = context::Context("current-span",nostd::shared_ptr(new trace::DefaultSpan())); context::Context ctx2 = format.Extract(Getter,carrier,ctx1); std::map c2 = {}; format.Inject(Setter,c2,ctx2); - EXPECT_EQ(std::string(c2["traceparent"]),"00-4bf92f3577b34da6a3ce929d0e0e4736-0102030405060708-01"); - EXPECT_EQ(std::string(c2["tracestate"]),"congo=congosSecondPosition,rojo=rojosFirstPosition"); + EXPECT_EQ(c2["traceparent"],"00-4bf92f3577b34da6a3ce929d0e0e4736-0102030405060708-01"); + EXPECT_EQ(c2["tracestate"],"congo=congosSecondPosition,rojo=rojosFirstPosition"); EXPECT_EQ(carrier.size(),c2.size()); } @@ -74,8 +70,9 @@ TEST(HTTPTextFormatTest, NoTraceParentHeader) // When trace context headers are not present, a new SpanContext // should be created. const std::map carrier = {}; - context::Context ctx = format.Extract(Getter, carrier, context::Context("current-span", nostd::shared_ptr(new trace::DefaultSpan())); - trace::Span* span = map_http_trace_context::GetCurrentSpan(ctx2); + context::Context ctx1 = context::Context("current-span",nostd::shared_ptr(new trace::DefaultSpan())); + context::Context ctx2 = format.Extract(Getter, carrier, ctx1); + trace::Span* span = MapHttpTraceContext::GetCurrentSpan(ctx2); EXPECT_EQ(span->GetContext().trace_id(),trace::SpanContext().trace_id()); EXPECT_EQ(span->GetContext().span_id(),trace::SpanContext().span_id()); EXPECT_EQ(span->GetContext().trace_flags(),trace::SpanContext().trace_flags()); @@ -89,10 +86,9 @@ TEST(HTTPTextFormatTest, InvalidTraceId) // Also ignore any trace state. const std::map carrier = { {"traceparent", "00-00000000000000000000000000000000-1234567890123456-00"}, {"tracestate", "foo=1,bar=2,foo=3"} }; - nostd::shared_ptr sp{new trace::DefaultSpan()}; - context::Context ctx1 = context::Context("current-span",sp); + context::Context ctx1 = context::Context("current-span",nostd::shared_ptr(new trace::DefaultSpan())); context::Context ctx2 = format.Extract(Getter, carrier, ctx1); - trace::Span* span = map_http_trace_context::GetCurrentSpan(ctx2); + trace::Span* span = MapHttpTraceContext::GetCurrentSpan(ctx2); EXPECT_EQ(span->GetContext().trace_id(),trace::SpanContext().trace_id()); EXPECT_EQ(span->GetContext().span_id(),trace::SpanContext().span_id()); EXPECT_EQ(span->GetContext().trace_flags(),trace::SpanContext().trace_flags()); @@ -106,10 +102,9 @@ TEST(HTTPTextFormatTest, InvalidParentId) // Also ignore any trace state. const std::map carrier = { {"traceparent", "00-00000000000000000000000000000000-0000000000000000-00"}, {"tracestate", "foo=1,bar=2,foo=3"} }; - nostd::shared_ptr sp{new trace::DefaultSpan()}; - context::Context ctx1 = context::Context("current-span",sp); + context::Context ctx1 = context::Context("current-span",nostd::shared_ptr(new trace::DefaultSpan())); context::Context ctx2 = format.Extract(Getter, carrier, ctx1); - trace::Span* span = map_http_trace_context::GetCurrentSpan(ctx2); + trace::Span* span = MapHttpTraceContext::GetCurrentSpan(ctx2); EXPECT_EQ(span->GetContext().trace_id(),trace::SpanContext().trace_id()); EXPECT_EQ(span->GetContext().span_id(),trace::SpanContext().span_id()); EXPECT_EQ(span->GetContext().trace_flags(),trace::SpanContext().trace_flags()); @@ -120,8 +115,7 @@ TEST(HTTPTextFormatTest, NoSendEmptyTraceState) { // If the trace state is empty, do not set the header. const std::map carrier = {{"traceparent","00-4bf92f3577b34da6a3ce929d0e0e4736-0102030405060708-01"}}; - nostd::shared_ptr sp{new trace::DefaultSpan()}; - context::Context ctx1 = context::Context("current-span",sp); + context::Context ctx1 = context::Context("current-span",nostd::shared_ptr(new trace::DefaultSpan())); context::Context ctx2 = format.Extract(Getter,carrier,ctx1); std::map c2 = {}; format.Inject(Setter,c2,ctx2); @@ -135,10 +129,9 @@ TEST(HTTPTextFormatTest, FormatNotSupported) // create a new trace context. const std::map carrier = { {"traceparent", "00-12345678901234567890123456789012-1234567890123456-00-residue"}, {"tracestate", "foo=1,bar=2,foo=3"} }; - nostd::shared_ptr sp{new trace::DefaultSpan()}; - context::Context ctx1 = context::Context("current-span",sp); + context::Context ctx1 = context::Context("current-span",nostd::shared_ptr(new trace::DefaultSpan())); context::Context ctx2 = format.Extract(Getter,carrier,ctx1); - trace::Span* span = map_http_trace_context::GetCurrentSpan(ctx2); + trace::Span* span = MapHttpTraceContext::GetCurrentSpan(ctx2); EXPECT_FALSE(span->GetContext().IsValid()); EXPECT_EQ(span->GetContext().trace_id(),trace::SpanContext().trace_id()); EXPECT_EQ(span->GetContext().span_id(),trace::SpanContext().span_id()); @@ -150,9 +143,7 @@ TEST(HTTPTextFormatTest, PropagateInvalidContext) { // Do not propagate invalid trace context. std::map carrier = {}; - nostd::shared_ptr sp{new trace::DefaultSpan(trace::SpanContext::GetInvalid())}; - - context::Context ctx{"current-span",sp}; + context::Context ctx{"current-span",nostd::shared_ptr(new trace::DefaultSpan(trace::SpanContext::GetInvalid()))}; format.Inject(Setter, carrier, ctx); EXPECT_TRUE(carrier.count("traceparent") == 0); } @@ -162,10 +153,9 @@ TEST(HTTPTextFormatTest, TraceStateHeaderWithTrailingComma) // Do not propagate invalid trace context. const std::map carrier = { {"traceparent", "00-12345678901234567890123456789012-1234567890123456-00"}, {"tracestate", "foo=1,"} }; - nostd::shared_ptr sp{new trace::DefaultSpan()}; - context::Context ctx1 = context::Context("current-span",sp); + context::Context ctx1 = context::Context("current-span",nostd::shared_ptr(new trace::DefaultSpan())); context::Context ctx2 = format.Extract(Getter,carrier,ctx1); - trace::Span* span = map_http_trace_context::GetCurrentSpan(ctx2); + trace::Span* span = MapHttpTraceContext::GetCurrentSpan(ctx2); trace::TraceState trace_state = span->GetContext().trace_state(); EXPECT_TRUE(trace_state.Get("foo", "1")); } @@ -176,10 +166,9 @@ TEST(HTTPTextFormatTest, TraceStateKeys) std::string trace_state_value = "1a-2f@foo=bar1,1a-_*/2b@foo=bar2,foo=bar3,foo-_*/bar=bar4"; const std::map carrier = { {"traceparent", "00-12345678901234567890123456789012-1234567890123456-00"}, {"tracestate", trace_state_value} }; - nostd::shared_ptr sp{new trace::DefaultSpan()}; - context::Context ctx1 = context::Context("current-span",sp); + context::Context ctx1 = context::Context("current-span",nostd::shared_ptr(new trace::DefaultSpan())); context::Context ctx2 = format.Extract(Getter,carrier,ctx1); - trace::Span* span = map_http_trace_context::GetCurrentSpan(ctx2); + trace::Span* span = MapHttpTraceContext::GetCurrentSpan(ctx2); trace::TraceState trace_state = span->GetContext().trace_state(); EXPECT_TRUE(trace_state.Get("1a-2f@foo", "bar1")); EXPECT_TRUE(trace_state.Get("1a-_*/2b@foo", "bar2"));