Skip to content

Commit

Permalink
Merge pull request open-telemetry#9 from Tianlin-Zhao/origin/propagators
Browse files Browse the repository at this point in the history
Origin/propagators
  • Loading branch information
Tianlin-Zhao authored Jul 30, 2020
2 parents 63d8c91 + 838c94e commit b09a788
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace propagators
// CompositeHTTPPropagator provides a mechanism for combining multiple
// propagators into a single one.
template <typename T>
class CompositeHTTPPropagator : public trace::propagation::HTTPTextFormat<T>
class CompositeHTTPPropagator : public trace::propagation::HTTPTextFormat<T> {
public:
// Rules that manages how context will be extracted from carrier.
using Getter = nostd::string_view(*)(const T &carrier, nostd::string_view trace_type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
}
Expand Down
25 changes: 11 additions & 14 deletions api/include/opentelemetry/trace/propagation/http_trace_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -174,7 +173,7 @@ class HttpTraceContext : public HTTPTextFormat<T> {
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."<<std::endl;
return SpanContext();
Expand Down Expand Up @@ -285,7 +284,6 @@ class HttpTraceContext : public HTTPTextFormat<T> {
}

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;
Expand All @@ -300,22 +298,21 @@ class HttpTraceContext : public HTTPTextFormat<T> {
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."<<std::endl;
return context_from_parent_header;
// return SpanContext.CreateFromRemoteParent(
// context_from_parent_header.GetTraceId(),
// context_from_parent_header.GetSpanId(),
// context_from_parent_header.GetTraceFlags(),
// TraceState.Builder().Build());
}
}
const nostd::string_view kTraceParent = "traceparent";
const nostd::string_view kTraceState = "tracestate";
const int kVersionBytes = 2;
const int kTraceIdBytes = 32;
const int kSpanIdBytes = 16;
const int kTraceFlagBytes = 2;
const int kTraceDelimiterBytes = 3;
const int kHeaderSize = kVersionBytes + kTraceIdBytes + kSpanIdBytes + kTraceFlagBytes + kTraceDelimiterBytes;
const int kTraceStateMaxMembers = 32;
const int kHeaderElementLengths[4] = {2,32,16,2};
};
}
} // namespace trace
Expand Down
61 changes: 25 additions & 36 deletions api/test/trace/propagation/http_text_format_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,36 @@ static void Setter(std::map<std::string,std::string> &carrier, nostd::string_vie

static trace::propagation::HttpTraceContext<std::map<std::string,std::string>> format = trace::propagation::HttpTraceContext<std::map<std::string,std::string>>();

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<std::map<std::string,std::string>>;
using MapHttpTraceContext = trace::propagation::HttpTraceContext<std::map<std::string,std::string>>;
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<std::string,std::string> carrier = {{"traceparent","00-4bf92f3577b34da6a3ce929d0e0e4736-0102030405060708-01"},{"tracestate","congo=congosSecondPosition,rojo=rojosFirstPosition"}};
nostd::shared_ptr<trace::Span> sp{new trace::DefaultSpan()};
context::Context ctx1 = context::Context("current-span",sp);
context::Context ctx1 = context::Context("current-span",nostd::shared_ptr<trace::Span>(new trace::DefaultSpan()));
context::Context ctx2 = format.Extract(Getter,carrier,ctx1);
std::map<std::string,std::string> 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());
}

Expand All @@ -74,8 +70,9 @@ TEST(HTTPTextFormatTest, NoTraceParentHeader)
// When trace context headers are not present, a new SpanContext
// should be created.
const std::map<std::string,std::string> carrier = {};
context::Context ctx = format.Extract(Getter, carrier, context::Context("current-span", nostd::shared_ptr<trace::Span>(new trace::DefaultSpan()));
trace::Span* span = map_http_trace_context::GetCurrentSpan(ctx2);
context::Context ctx1 = context::Context("current-span",nostd::shared_ptr<trace::Span>(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());
Expand All @@ -89,10 +86,9 @@ TEST(HTTPTextFormatTest, InvalidTraceId)
// Also ignore any trace state.
const std::map<std::string,std::string> carrier = { {"traceparent", "00-00000000000000000000000000000000-1234567890123456-00"},
{"tracestate", "foo=1,bar=2,foo=3"} };
nostd::shared_ptr<trace::Span> sp{new trace::DefaultSpan()};
context::Context ctx1 = context::Context("current-span",sp);
context::Context ctx1 = context::Context("current-span",nostd::shared_ptr<trace::Span>(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());
Expand All @@ -106,10 +102,9 @@ TEST(HTTPTextFormatTest, InvalidParentId)
// Also ignore any trace state.
const std::map<std::string,std::string> carrier = { {"traceparent", "00-00000000000000000000000000000000-0000000000000000-00"},
{"tracestate", "foo=1,bar=2,foo=3"} };
nostd::shared_ptr<trace::Span> sp{new trace::DefaultSpan()};
context::Context ctx1 = context::Context("current-span",sp);
context::Context ctx1 = context::Context("current-span",nostd::shared_ptr<trace::Span>(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());
Expand All @@ -120,8 +115,7 @@ TEST(HTTPTextFormatTest, NoSendEmptyTraceState)
{
// If the trace state is empty, do not set the header.
const std::map<std::string,std::string> carrier = {{"traceparent","00-4bf92f3577b34da6a3ce929d0e0e4736-0102030405060708-01"}};
nostd::shared_ptr<trace::Span> sp{new trace::DefaultSpan()};
context::Context ctx1 = context::Context("current-span",sp);
context::Context ctx1 = context::Context("current-span",nostd::shared_ptr<trace::Span>(new trace::DefaultSpan()));
context::Context ctx2 = format.Extract(Getter,carrier,ctx1);
std::map<std::string,std::string> c2 = {};
format.Inject(Setter,c2,ctx2);
Expand All @@ -135,10 +129,9 @@ TEST(HTTPTextFormatTest, FormatNotSupported)
// create a new trace context.
const std::map<std::string,std::string> carrier = { {"traceparent", "00-12345678901234567890123456789012-1234567890123456-00-residue"},
{"tracestate", "foo=1,bar=2,foo=3"} };
nostd::shared_ptr<trace::Span> sp{new trace::DefaultSpan()};
context::Context ctx1 = context::Context("current-span",sp);
context::Context ctx1 = context::Context("current-span",nostd::shared_ptr<trace::Span>(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());
Expand All @@ -150,9 +143,7 @@ TEST(HTTPTextFormatTest, PropagateInvalidContext)
{
// Do not propagate invalid trace context.
std::map<std::string,std::string> carrier = {};
nostd::shared_ptr<trace::Span> sp{new trace::DefaultSpan(trace::SpanContext::GetInvalid())};

context::Context ctx{"current-span",sp};
context::Context ctx{"current-span",nostd::shared_ptr<trace::Span>(new trace::DefaultSpan(trace::SpanContext::GetInvalid()))};
format.Inject(Setter, carrier, ctx);
EXPECT_TRUE(carrier.count("traceparent") == 0);
}
Expand All @@ -162,10 +153,9 @@ TEST(HTTPTextFormatTest, TraceStateHeaderWithTrailingComma)
// Do not propagate invalid trace context.
const std::map<std::string,std::string> carrier = { {"traceparent", "00-12345678901234567890123456789012-1234567890123456-00"},
{"tracestate", "foo=1,"} };
nostd::shared_ptr<trace::Span> sp{new trace::DefaultSpan()};
context::Context ctx1 = context::Context("current-span",sp);
context::Context ctx1 = context::Context("current-span",nostd::shared_ptr<trace::Span>(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"));
}
Expand All @@ -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<std::string,std::string> carrier = { {"traceparent", "00-12345678901234567890123456789012-1234567890123456-00"},
{"tracestate", trace_state_value} };
nostd::shared_ptr<trace::Span> sp{new trace::DefaultSpan()};
context::Context ctx1 = context::Context("current-span",sp);
context::Context ctx1 = context::Context("current-span",nostd::shared_ptr<trace::Span>(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"));
Expand Down

0 comments on commit b09a788

Please sign in to comment.