Skip to content

Commit

Permalink
Merge pull request open-telemetry#5 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 29, 2020
2 parents 28bbe45 + 52b91fc commit d826bb5
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 244 deletions.
2 changes: 2 additions & 0 deletions api/include/opentelemetry/trace/default_span.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class DefaultSpan: public Span {
return "DefaultSpan";
}

DefaultSpan() = default;

DefaultSpan(SpanContext span_context) {
this->span_context_ = span_context;
}
Expand Down
56 changes: 26 additions & 30 deletions api/include/opentelemetry/trace/propagation/http_trace_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/nostd/variant.h"
#include "opentelemetry/nostd/span.h"
//#include "opentelemetry/trace/default_span.h"
#include "opentelemetry/trace/default_span.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace trace
Expand Down Expand Up @@ -61,32 +61,28 @@ 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 = 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;
// }
SpanContext span_context = GetCurrentSpan(context)->GetContext();
if (!span_context.IsValid()) {
return;
}
InjectImpl(setter, carrier, span_context);
}

context::Context Extract(Getter getter, const T &carrier, context::Context &context) override {
trace::SpanContext span_context = ExtractImpl(getter,carrier);
SpanContext span_context = ExtractImpl(getter,carrier);
nostd::string_view span_key = "current-span";
nostd::shared_ptr<trace::Span> sp{new trace::Span(span_context)};
nostd::shared_ptr<Span> sp{new DefaultSpan(span_context)};
return context.SetValue(span_key,sp);
}

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

static void SpanContextToString(const trace::SpanContext &span_context, T &carrier, Setter setter) {
static void InjectTraceParent(const SpanContext &span_context, T &carrier, Setter setter) {
char trace_id[32];
TraceId(span_context.trace_id()).ToLowerBase16(trace_id);
char span_id[16];
Expand Down Expand Up @@ -145,7 +141,7 @@ class HttpTraceContext : public HTTPTextFormat<T> {
return TraceFlags(buf);
}

static nostd::string_view FormatTracestate(TraceState trace_state, T &carrier, Setter setter) {
static void InjectTraceState(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++) {
Expand All @@ -168,21 +164,21 @@ class HttpTraceContext : public HTTPTextFormat<T> {
}
}

static void InjectImpl(Setter setter, T &carrier, const trace::SpanContext &span_context) {
SpanContextToString(span_context, carrier, setter);
static void InjectImpl(Setter setter, T &carrier, const SpanContext &span_context) {
InjectTraceParent(span_context, carrier, setter);
if (!span_context.trace_state().empty()) {
FormatTracestate(span_context.trace_state(), carrier, setter);
InjectTraceState(span_context.trace_state(), carrier, setter);
}
}

static trace::SpanContext ExtractContextFromTraceParent(nostd::string_view trace_parent) {
static SpanContext ExtractContextFromTraceParent(nostd::string_view trace_parent) {
bool is_valid = trace_parent.length() == kHeaderSize
&& trace_parent[kVersionBytes] == '-'
&& trace_parent[kVersionBytes+kTraceIdBytes+1] == '-'
&& trace_parent[kVersionBytes+kTraceIdBytes+kParentIdBytes+2] == '-';
if (!is_valid) {
std::cout<<"Unparseable trace_parent header. Returning INVALID span context."<<std::endl;
return trace::SpanContext();
return SpanContext();
}
try {
nostd::string_view version;
Expand Down Expand Up @@ -220,20 +216,20 @@ class HttpTraceContext : public HTTPTextFormat<T> {
trace_flags = trace_parent.substr(start_pos,kHeaderElementLengths[elt_num]);

if (trace_id == "00000000000000000000000000000000" || span_id == "0000000000000000") {
return trace::SpanContext();
return SpanContext();
}
if (version == "ff") {
return trace::SpanContext();
return SpanContext();
}

TraceId trace_id_obj = GenerateTraceIdFromString(trace_id);
SpanId span_id_obj = GenerateSpanIdFromString(span_id);
TraceFlags trace_flags_obj = GenerateTraceFlagsFromString(trace_flags);
return trace::SpanContext(trace_id_obj,span_id_obj,trace_flags_obj,TraceState(),true);
// return trace::SpanContext.CreateFromRemoteParent(trace_id_obj, span_id_obj, trace_flags_obj, TraceState());
return SpanContext(trace_id_obj,span_id_obj,trace_flags_obj,TraceState(),true);
// return SpanContext.CreateFromRemoteParent(trace_id_obj, span_id_obj, trace_flags_obj, TraceState());
} catch (std::exception& e) {
std::cout<<"Unparseable trace_parent header. Returning INVALID span context."<<std::endl;
return trace::SpanContext();
return SpanContext();
}
}

Expand All @@ -250,7 +246,7 @@ class HttpTraceContext : public HTTPTextFormat<T> {
if (start_pos == -1 && end_pos == -1) continue;
element_num++;
list_member = trace_state_header.substr(start_pos,end_pos-start_pos+1);
AddNewMember(trace_state,list_member);
if (list_member!="") AddNewMember(trace_state,list_member);
end_pos = -1;
start_pos = -1;
} else {
Expand All @@ -260,7 +256,7 @@ class HttpTraceContext : public HTTPTextFormat<T> {
}
if (start_pos!=-1 && end_pos!=-1) {
list_member = trace_state_header.substr(start_pos,end_pos-start_pos+1);
AddNewMember(trace_state,list_member);
if (list_member!="") AddNewMember(trace_state,list_member);
element_num++;
}

Expand All @@ -279,12 +275,12 @@ class HttpTraceContext : public HTTPTextFormat<T> {
}
}

static trace::SpanContext ExtractImpl(Getter getter, const T &carrier) {
static SpanContext ExtractImpl(Getter getter, const T &carrier) {
nostd::string_view trace_parent = getter(carrier, kTraceParent);
if (trace_parent == "") {
return trace::SpanContext();
return SpanContext();
}
trace::SpanContext context_from_parent_header = ExtractContextFromTraceParent(trace_parent);
SpanContext context_from_parent_header = ExtractContextFromTraceParent(trace_parent);
if (!context_from_parent_header.IsValid()) {
return context_from_parent_header;
}
Expand All @@ -298,14 +294,14 @@ class HttpTraceContext : public HTTPTextFormat<T> {

try {
TraceState trace_state = ExtractTraceState(trace_state_header);
return trace::SpanContext(
return SpanContext(
context_from_parent_header.trace_id(),
context_from_parent_header.span_id(),
context_from_parent_header.trace_flags(),
trace_state,
true
);
// return trace::SpanContext.CreateFromRemoteParent(
// return SpanContext.CreateFromRemoteParent(
// context_from_parent_header.GetTraceId(),
// context_from_parent_header.GetSpanId(),
// context_from_parent_header.GetTraceFlags(),
Expand All @@ -314,7 +310,7 @@ class HttpTraceContext : public HTTPTextFormat<T> {
} catch (std::exception& e) {
std::cout<<"Unparseable tracestate header. Returning span context without state."<<std::endl;
return context_from_parent_header;
// return trace::SpanContext.CreateFromRemoteParent(
// return SpanContext.CreateFromRemoteParent(
// context_from_parent_header.GetTraceId(),
// context_from_parent_header.GetSpanId(),
// context_from_parent_header.GetTraceFlags(),
Expand Down
159 changes: 73 additions & 86 deletions api/include/opentelemetry/trace/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,96 +75,83 @@ 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;
// 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,
const common::AttributeValue &&value) noexcept = 0;

// Adds an event to the Span.
virtual void AddEvent(nostd::string_view name) noexcept = 0;

// Adds an event to the Span, with a custom timestamp.
virtual void AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept = 0;

// Adds an event to the Span, with a custom timestamp, and attributes.
virtual void AddEvent(nostd::string_view name,
core::SystemTimestamp timestamp,
const KeyValueIterable &attributes) noexcept = 0;

virtual void AddEvent(nostd::string_view name, const KeyValueIterable &attributes) noexcept
{
this->AddEvent(name, std::chrono::system_clock::now(), attributes);
}
const trace::SpanContext GetContext() {
return span_context_;

template <class T, nostd::enable_if_t<detail::is_key_value_iterable<T>::value> * = nullptr>
void AddEvent(nostd::string_view name,
core::SystemTimestamp timestamp,
const T &attributes) noexcept
{
this->AddEvent(name, timestamp, KeyValueIterableView<T>{attributes});
}

template <class T, nostd::enable_if_t<detail::is_key_value_iterable<T>::value> * = nullptr>
void AddEvent(nostd::string_view name, const T &attributes) noexcept
{
this->AddEvent(name, KeyValueIterableView<T>{attributes});
}

void SetContext(trace::SpanContext span_context) noexcept {
span_context_ = span_context;
void AddEvent(nostd::string_view name,
core::SystemTimestamp timestamp,
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>
attributes) noexcept
{
this->AddEvent(name, timestamp,
nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
attributes.begin(), attributes.end()});
}
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,
// const common::AttributeValue &&value) noexcept = 0;
//
// // Adds an event to the Span.
// virtual void AddEvent(nostd::string_view name) noexcept = 0;
//
// // Adds an event to the Span, with a custom timestamp.
// virtual void AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept = 0;
//
// // Adds an event to the Span, with a custom timestamp, and attributes.
// virtual void AddEvent(nostd::string_view name,
// core::SystemTimestamp timestamp,
// const KeyValueIterable &attributes) noexcept = 0;
//
// virtual void AddEvent(nostd::string_view name, const KeyValueIterable &attributes) noexcept
// {
// this->AddEvent(name, std::chrono::system_clock::now(), attributes);
// }
//
// template <class T, nostd::enable_if_t<detail::is_key_value_iterable<T>::value> * = nullptr>
// void AddEvent(nostd::string_view name,
// core::SystemTimestamp timestamp,
// const T &attributes) noexcept
// {
// this->AddEvent(name, timestamp, KeyValueIterableView<T>{attributes});
// }
//
// template <class T, nostd::enable_if_t<detail::is_key_value_iterable<T>::value> * = nullptr>
// void AddEvent(nostd::string_view name, const T &attributes) noexcept
// {
// this->AddEvent(name, KeyValueIterableView<T>{attributes});
// }
//
// void AddEvent(nostd::string_view name,
// core::SystemTimestamp timestamp,
// std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>
// attributes) noexcept
// {
// this->AddEvent(name, timestamp,
// nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
// attributes.begin(), attributes.end()});
// }
//
// void AddEvent(nostd::string_view name,
// std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>
// attributes) noexcept
// {
// this->AddEvent(name, std::chrono::system_clock::now(),
// nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
// attributes.begin(), attributes.end()});
// }
//
// // Sets the status of the span. The default status is OK. Only the value of the last call will be
// // recorded, and implementations are free to ignore previous calls.
// virtual void SetStatus(CanonicalCode code, nostd::string_view description) noexcept = 0;
//
// // Updates the name of the Span. If used, this will override the name provided
// // during creation.
// virtual void UpdateName(nostd::string_view name) noexcept = 0;
//
// /**
// * Mark the end of the Span.
// * Only the timing of the first End call for a given Span will be recorded,
// * and implementations are free to ignore all further calls.
// * @param options can be used to manually define span properties like the end
// * timestamp
// */
// virtual void End(const EndSpanOptions &options = {}) noexcept = 0;
//
// // TODO
// // SpanContext context() const noexcept = 0;
// virtual trace::SpanContext GetContext() const noexcept = 0;
// // Returns true if this Span is recording tracing events (e.g. SetAttribute,
// // AddEvent).
// virtual bool IsRecording() const noexcept = 0;

void AddEvent(nostd::string_view name,
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>
attributes) noexcept
{
this->AddEvent(name, std::chrono::system_clock::now(),
nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
attributes.begin(), attributes.end()});
}

// Sets the status of the span. The default status is OK. Only the value of the last call will be
// recorded, and implementations are free to ignore previous calls.
virtual void SetStatus(CanonicalCode code, nostd::string_view description) noexcept = 0;

// Updates the name of the Span. If used, this will override the name provided
// during creation.
virtual void UpdateName(nostd::string_view name) noexcept = 0;

/**
* Mark the end of the Span.
* Only the timing of the first End call for a given Span will be recorded,
* and implementations are free to ignore all further calls.
* @param options can be used to manually define span properties like the end
* timestamp
*/
virtual void End(const EndSpanOptions &options = {}) noexcept = 0;

// TODO
// SpanContext context() const noexcept = 0;
virtual trace::SpanContext GetContext() const noexcept = 0;
// Returns true if this Span is recording tracing events (e.g. SetAttribute,
// AddEvent).
virtual bool IsRecording() const noexcept = 0;

// virtual trace::Tracer &tracer() const noexcept = 0;
};
Expand Down
3 changes: 1 addition & 2 deletions api/include/opentelemetry/trace/span_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

//#include <cstdint>
//#include <cstring>
#include <iostream>
#include <map>
#include <exception>
#include "opentelemetry/nostd/unique_ptr.h"
#include "opentelemetry/trace/span_id.h"
#include "opentelemetry/trace/trace_flags.h"
Expand Down
2 changes: 2 additions & 0 deletions api/include/opentelemetry/trace/trace_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ class TraceState
tmp_map[key] = value;
}

bool operator==(const TraceState &that) const noexcept { return tmp_map == that.tmp_map; }

// Returns true if there are no keys.
bool empty() const noexcept { return tmp_map.size()==0; }

Expand Down
Loading

0 comments on commit d826bb5

Please sign in to comment.