Skip to content

Commit

Permalink
Merge pull request open-telemetry#4 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 28, 2020
2 parents a99f620 + 91d0c3a commit 28bbe45
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 34 deletions.
3 changes: 2 additions & 1 deletion api/include/opentelemetry/context/context_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
#include "opentelemetry/nostd/unique_ptr.h"
#include "opentelemetry/nostd/variant.h"
#include "opentelemetry/trace/span_context.h"
#include "opentelemetry/trace/span.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace context
{
using ContextValue =
nostd::variant<bool, int64_t, uint64_t, double, nostd::shared_ptr<trace::SpanContext>>;
nostd::variant<bool, int64_t, uint64_t, double, nostd::shared_ptr<trace::SpanContext>, nostd::shared_ptr<trace::Span>>;
} // namespace context
OPENTELEMETRY_END_NAMESPACE
53 changes: 26 additions & 27 deletions api/include/opentelemetry/trace/propagation/http_trace_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ class HttpTraceContext : public HTTPTextFormat<T> {
using Setter = void(*)(T &carrier, nostd::string_view trace_type,nostd::string_view trace_description);

void Inject(Setter setter, T &carrier, const context::Context &context) override {
trace::SpanContext span_context = GetCurrentSpanContext(context);
trace::SpanContext span_context = GetCurrentSpan(context)->GetContext();
// for (std::map<nostd::string_view,nostd::string_view>::iterator it = TraceState(span_context.trace_state()).tmp_map.begin();
// it != TraceState(span_context.trace_state()).tmp_map.end(); it++) {
// std::cout<<it->first<<" "<<it->second<<std::endl;
// }
if (!span_context.IsValid()) {
return;
}
Expand All @@ -71,19 +75,18 @@ class HttpTraceContext : public HTTPTextFormat<T> {
context::Context Extract(Getter getter, const T &carrier, context::Context &context) override {
trace::SpanContext span_context = ExtractImpl(getter,carrier);
nostd::string_view span_key = "current-span";
nostd::shared_ptr<trace::SpanContext> spc{new trace::SpanContext(span_context)};
return context.SetValue(span_key,spc);
nostd::shared_ptr<trace::Span> sp{new trace::Span(span_context)};
return context.SetValue(span_key,sp);
}

trace::SpanContext GetCurrentSpanContext(const context::Context &context) {
trace::Span* GetCurrentSpan(const context::Context &context) {
const nostd::string_view span_key = "current-span";
context::Context ctx(context);
nostd::shared_ptr<trace::SpanContext> span_context = nostd::get<nostd::shared_ptr<trace::SpanContext>>(ctx.GetValue(span_key));

return *(span_context.get());
nostd::shared_ptr<trace::Span> span = nostd::get<nostd::shared_ptr<trace::Span>>(ctx.GetValue(span_key));
return (span.get());
}

static nostd::string_view SpanContextToString(const trace::SpanContext &span_context) {
static void SpanContextToString(const trace::SpanContext &span_context, T &carrier, Setter setter) {
char trace_id[32];
TraceId(span_context.trace_id()).ToLowerBase16(trace_id);
char span_id[16];
Expand All @@ -103,7 +106,7 @@ class HttpTraceContext : public HTTPTextFormat<T> {
for (int i = 0; i < 2; i++) {
hex_string += trace_flags[i];
}
return nostd::string_view(hex_string);
setter(carrier, kTraceParent, hex_string);
}

static TraceId GenerateTraceIdFromString(nostd::string_view trace_id) {
Expand Down Expand Up @@ -141,6 +144,17 @@ class HttpTraceContext : public HTTPTextFormat<T> {
buf = CharToInt(trace_flags[0])*16+CharToInt(trace_flags[1]);
return TraceFlags(buf);
}

static nostd::string_view FormatTracestate(TraceState trace_state, T &carrier, Setter setter) {
std::string trace_state_string = "";
std::map<nostd::string_view,nostd::string_view> entries = trace_state.entries();
for (std::map<nostd::string_view,nostd::string_view>::const_iterator it = entries.begin(); it != entries.end(); it++) {
if (it != entries.begin()) trace_state_string += ",";
trace_state_string += std::string(it->first) + "=" + std::string(it->second);
}
setter(carrier, kTraceState, trace_state_string);
}

private:
static uint8_t CharToInt(char c) {
if (c >= '0' && c <= '9') {
Expand All @@ -155,24 +169,10 @@ class HttpTraceContext : public HTTPTextFormat<T> {
}

static void InjectImpl(Setter setter, T &carrier, const trace::SpanContext &span_context) {
nostd::string_view trace_parent = SpanContextToString(span_context);
setter(carrier, kTraceParent, trace_parent);
carrier[std::string(kTraceParent)] = std::string(trace_parent);
SpanContextToString(span_context, carrier, setter);
if (!span_context.trace_state().empty()) {
nostd::string_view trace_state = FormatTracestate(span_context.trace_state());
setter(carrier, kTraceState, trace_state);
carrier[std::string(kTraceState)] = std::string(trace_state);
}
}

static nostd::string_view FormatTracestate(TraceState trace_state) {
std::string res = "";
std::map<nostd::string_view,nostd::string_view> entries = trace_state.entries();
for (std::map<nostd::string_view,nostd::string_view>::iterator it = entries.begin(); it != entries.end(); it++) {
if (it != entries.begin()) res += ",";
res += std::string(it->first) + "=" + std::string(it->second);
FormatTracestate(span_context.trace_state(), carrier, setter);
}
return res;
}

static trace::SpanContext ExtractContextFromTraceParent(nostd::string_view trace_parent) {
Expand Down Expand Up @@ -281,7 +281,6 @@ class HttpTraceContext : public HTTPTextFormat<T> {

static trace::SpanContext ExtractImpl(Getter getter, const T &carrier) {
nostd::string_view trace_parent = getter(carrier, kTraceParent);
std::cout<<trace_parent<<std::endl;
if (trace_parent == "") {
return trace::SpanContext();
}
Expand Down Expand Up @@ -313,7 +312,7 @@ class HttpTraceContext : public HTTPTextFormat<T> {
// trace_state
// );
} catch (std::exception& e) {
// std::cout<<"Unparseable tracestate header. Returning span context without state."<<std::endl;
std::cout<<"Unparseable tracestate header. Returning span context without state."<<std::endl;
return context_from_parent_header;
// return trace::SpanContext.CreateFromRemoteParent(
// context_from_parent_header.GetTraceId(),
Expand Down
13 changes: 13 additions & 0 deletions api/include/opentelemetry/trace/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ class Span
Span &operator=(const Span &) = delete;
Span &operator=(Span &&) = delete;

// The methods field of span context below are work-around, not the official implementation
Span(SpanContext span_context) {
span_context_ = span_context;
}
const trace::SpanContext GetContext() {
return span_context_;
}

void SetContext(trace::SpanContext span_context) noexcept {
span_context_ = span_context;
}
private:
trace::SpanContext span_context_;
// // Sets an attribute on the Span. If the Span previously contained a mapping for
// // the key, the old value is replaced.
// virtual void SetAttribute(nostd::string_view key,
Expand Down
4 changes: 2 additions & 2 deletions api/include/opentelemetry/trace/span_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ class SpanContext final
trace_id_ = ctx.trace_id_;
span_id_ = ctx.span_id_;
trace_flags_ = ctx.trace_flags_;
trace_state_.reset(new TraceState());
trace_state_.reset(new TraceState(*(ctx.trace_state_.get())));
return *this;
};
SpanContext &operator=(SpanContext &&ctx) {
trace_id_ = ctx.trace_id_;
span_id_ = ctx.span_id_;
trace_flags_ = ctx.trace_flags_;
trace_state_.reset(new TraceState());
trace_state_.reset(new TraceState(*(ctx.trace_state_.get())));
return *this;
};
// TODO
Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/trace/trace_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class TraceState
bool empty() const noexcept { return tmp_map.size()==0; }

// Returns a span of all the entries. The TraceState object must outlive the span.
std::map<nostd::string_view,nostd::string_view> entries() noexcept { return tmp_map; }
std::map<nostd::string_view,nostd::string_view> entries() const noexcept { return tmp_map; }
// virtual nostd::span<Entry *> entries() const noexcept { return {}; }

// Key is opaque string up to 256 characters printable. It MUST begin with a lowercase letter, and
Expand Down
8 changes: 5 additions & 3 deletions api/test/trace/propagation/http_text_format_test.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "opentelemetry/trace/propagation/http_text_format.h"
#include "opentelemetry/trace/propagation/http_trace_context.h"
#include "opentelemetry/context/context.h"
//#include "opentelemetry/trace/span.h"
#include "opentelemetry/trace/span.h"
#include "opentelemetry/trace/default_span.h"
#include "opentelemetry/trace/span_context.h"
#include "opentelemetry/nostd/string_view.h"
Expand Down Expand Up @@ -68,11 +68,13 @@ TEST(HTTPTextFormatTest, TraceFlagsBufferGeneration)
TEST(HTTPTextFormatTest, NoSpanTest)
{
const std::map<std::string,std::string> carrier = {{"traceparent","00-4bf92f3577b34da6a3ce929d0e0e4736-0102030405060708-01"},{"tracestate","congo=congosSecondPosition,rojo=rojosFirstPosition"}};
nostd::shared_ptr<trace::SpanContext> spc{new trace::SpanContext()};
context::Context ctx1 = context::Context("current-span",spc);
nostd::shared_ptr<trace::Span> sp{new trace::Span()};
context::Context ctx1 = context::Context("current-span",sp);
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(carrier.size(),c2.size());
}

Expand Down

0 comments on commit 28bbe45

Please sign in to comment.