From 53d696fbc0e4493594440eac552d74d88ca4dcbe Mon Sep 17 00:00:00 2001 From: tz68 Date: Tue, 23 Jun 2020 16:47:25 -0400 Subject: [PATCH 01/15] Added HTTPTextFormat class header --- .idea/.gitignore | 2 ++ .idea/modules.xml | 8 +++++ .idea/opentelemetry-cpp.iml | 9 ++++++ .idea/vcs.xml | 6 ++++ .../trace/propagation/httptextformat.h | 31 +++++++++++++++++++ 5 files changed, 56 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/modules.xml create mode 100644 .idea/opentelemetry-cpp.iml create mode 100644 .idea/vcs.xml create mode 100644 api/include/opentelemetry/trace/propagation/httptextformat.h diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000..e7e9d11d4b --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000..4a4ccb0d91 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/opentelemetry-cpp.iml b/.idea/opentelemetry-cpp.iml new file mode 100644 index 0000000000..d6ebd48059 --- /dev/null +++ b/.idea/opentelemetry-cpp.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000..35eb1ddfbb --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/api/include/opentelemetry/trace/propagation/httptextformat.h b/api/include/opentelemetry/trace/propagation/httptextformat.h new file mode 100644 index 0000000000..680ebce70b --- /dev/null +++ b/api/include/opentelemetry/trace/propagation/httptextformat.h @@ -0,0 +1,31 @@ +#pragma once + +#include +#include "opentelemetry/context/context.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace trace +{ +namespace propagation +{ + +virtual static Context SetSpanInContext(Span span, Context context = NULL) +virtual static Span GetCurrentSpan(Context context = NULL) + +template +class HTTPTextFormat { + /** This class provides an interface that enables extracting and injecting + context into headers of HTTP requests. HTTP frameworks and clients + can integrate with HTTPTextFormat by providing the object containing the + headers, and a getter and setter function for the extraction and + injection of values, respectively.*/ + public: + using Setter = nostd::string_view(*)(nostd::string_view,nostd::string_view); + using Getter = void(*)(T&,nostd::string_view,nostd::string); + virtual Context extract(Setter get_from_carrier, const T &carrier, Context &context) + virtual void inject(Getter set_from_carrier, T &carrier, const Context &context) +}; +} +} +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file From a91446542bfbd2017f5097012e40a844391674bf Mon Sep 17 00:00:00 2001 From: tz68 Date: Wed, 24 Jun 2020 23:27:46 -0400 Subject: [PATCH 02/15] Added example implementation of httptextformat.h as HttpTraceContext.cc. Some dependencies and data structure questions are raised in the form of comments -- i.e. no explanatory comments but questions. --- .../trace/propagation/HttpTraceContext.cc | 129 ++++++++++++++++++ .../trace/propagation/httptextformat.h | 13 +- 2 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 api/include/opentelemetry/trace/propagation/HttpTraceContext.cc diff --git a/api/include/opentelemetry/trace/propagation/HttpTraceContext.cc b/api/include/opentelemetry/trace/propagation/HttpTraceContext.cc new file mode 100644 index 0000000000..1411641ffa --- /dev/null +++ b/api/include/opentelemetry/trace/propagation/HttpTraceContext.cc @@ -0,0 +1,129 @@ +#include + +namespace trace +{ +namespace propagation +{ +namespace +{ +class HttpTraceContext : public HTTPTextFormat +{ + + private: + static const nostd::string_view TRACE_PARENT = "traceparent"; + static const nostd::string_view TRACE_STATE = "tracestate"; + static const std::vector FIELDS = {TRACE_PARENT, TRACE_STATE)}; + static const int VERSION_BYTES = 2; + static const int TRACE_ID_BYTES = 32; + static const int PARENT_ID_BYTES = 16; + static const int TRACE_FLAG_BYTES = 2; + static const int TRACE_DELIMITER_BYTES = 3; + static const int HEADER_SIZE = VERSION_BYTES + TRACE_ID_BYTES + PARENT_ID_BYTES + TRACE_FLAG_BYTES + TRACE_DELIMITER_BYTES; + + static T checkNotNull(T arg, nostd::string_view errorMessage) { + if (arg == NULL) { + throw new NullPointerException(errorMessage); + } + return arg; + } + + static void injectImpl(Setter setter, T &carrier, const SpanContext spanContext) { + char hex_string[HEADER_SIZE]; + sprintf(hex_string, "00-%032x-%016x-%02x",span_context.trace_id,span_context.span_id,span_context.trace_flags); + nostd::string_view traceparent_string(hex_string); // I don't know if the string view has a function that takes in an char array convert it to string though. + setter(carrier, TRACE_PARENT, traceparent_string); + if (spanContext.getTraceState()) { + nostd::string_view tracestate_string = formatTracestate(span_context.getTraceState()); // I need the definition for the type of TraceState(Dictionary or something else) + setter(carrier, TRACE_STATE, tracestate_string); + } + } + + static SpanContext extractContextFromTraceParent(nostd::string_view traceparent) { + bool isValid = traceparent.length() == HEADER_SIZE + && traceparent[VERSION_BYTES] == "-" + && traceparent[VERSION_BYTES+TRACE_ID_BYTES+1] == "-" + && traceparent[VERSION_BYTES+TRACE_ID_BYTES+PARENT_ID_BYTES+2] == "-"; + if (!isValid) { + std::cout<<"Unparseable traceparent header. Returning INVALID span context."< fields() { + return FIELDS; + } + + void inject(Setter setter, T &carrier, const Context &context) { + checkNotNull(context, "context"); + checkNotNull(setter, "setter"); + + Span span = GetCurrentSpan(context); + if (span == null || !span.getContext().isValid()) { + // We don't have span.getContext() in span.h, should we just use span? As well as acquiring validity. (I do know how to implement them though) + return; + } + injectImpl(span.getContext(), carrier, setter); // span.getContext() function needed + } + + Context extract(Getter get_from_carrier, const T &carrier, Context &context) { + checkNotNull(context, "context"); + checkNotNull(carrier, "carrier"); + checkNotNull(getter, "getter"); + + SpanContext spanContext = extractImpl(carrier, getter); + return SetSpanInContext(trace.DefaultSpan(spanContext), context); // I actually need a default span class (implemented) here, I don't know who to ask for though. But both python and java version have default span classes though. + } +} +} +} +} \ No newline at end of file diff --git a/api/include/opentelemetry/trace/propagation/httptextformat.h b/api/include/opentelemetry/trace/propagation/httptextformat.h index 680ebce70b..f3eabae7af 100644 --- a/api/include/opentelemetry/trace/propagation/httptextformat.h +++ b/api/include/opentelemetry/trace/propagation/httptextformat.h @@ -2,6 +2,7 @@ #include #include "opentelemetry/context/context.h" +#include "opentelemetry/nostd/string_view.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -10,8 +11,8 @@ namespace trace namespace propagation { -virtual static Context SetSpanInContext(Span span, Context context = NULL) -virtual static Span GetCurrentSpan(Context context = NULL) +virtual static Context SetSpanInContext(Span span, Context &context = NULL) +virtual static Span GetCurrentSpan(Context &context = NULL) template class HTTPTextFormat { @@ -21,10 +22,10 @@ class HTTPTextFormat { headers, and a getter and setter function for the extraction and injection of values, respectively.*/ public: - using Setter = nostd::string_view(*)(nostd::string_view,nostd::string_view); - using Getter = void(*)(T&,nostd::string_view,nostd::string); - virtual Context extract(Setter get_from_carrier, const T &carrier, Context &context) - virtual void inject(Getter set_from_carrier, T &carrier, const Context &context) + using Getter = nostd::string_view(*)(nostd::string_view,nostd::string_view); + using Setter = void(*)(T&,nostd::string_view,nostd::string); + virtual Context extract(Getter get_from_carrier, const T &carrier, Context &context) + virtual void inject(Setter set_from_carrier, T &carrier, const Context &context) }; } } From ad7faa5ee97f8fe8e1f08bc30bc259979fd463b3 Mon Sep 17 00:00:00 2001 From: Tianlin-Zhao <66151597+Tianlin-Zhao@users.noreply.github.com> Date: Wed, 24 Jun 2020 23:33:12 -0400 Subject: [PATCH 03/15] Delete .gitignore --- .idea/.gitignore | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 .idea/.gitignore diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index e7e9d11d4b..0000000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# Default ignored files -/workspace.xml From 9073a7a4a92bd9feb1cbee5157309df2ab53ebec Mon Sep 17 00:00:00 2001 From: Tianlin-Zhao <66151597+Tianlin-Zhao@users.noreply.github.com> Date: Wed, 24 Jun 2020 23:33:22 -0400 Subject: [PATCH 04/15] Delete modules.xml --- .idea/modules.xml | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 .idea/modules.xml diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 4a4ccb0d91..0000000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file From f0922b671a44970a875f4daef62294261728d32f Mon Sep 17 00:00:00 2001 From: Tianlin-Zhao <66151597+Tianlin-Zhao@users.noreply.github.com> Date: Wed, 24 Jun 2020 23:33:32 -0400 Subject: [PATCH 05/15] Delete opentelemetry-cpp.iml --- .idea/opentelemetry-cpp.iml | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 .idea/opentelemetry-cpp.iml diff --git a/.idea/opentelemetry-cpp.iml b/.idea/opentelemetry-cpp.iml deleted file mode 100644 index d6ebd48059..0000000000 --- a/.idea/opentelemetry-cpp.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file From f5c038bfb72daaca429c8e024e1438f8af517748 Mon Sep 17 00:00:00 2001 From: Tianlin-Zhao <66151597+Tianlin-Zhao@users.noreply.github.com> Date: Wed, 24 Jun 2020 23:33:39 -0400 Subject: [PATCH 06/15] Delete vcs.xml --- .idea/vcs.xml | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1ddfbb..0000000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From f409c6726986526fee59fa1c3949cf67d8164041 Mon Sep 17 00:00:00 2001 From: tz68 Date: Wed, 24 Jun 2020 23:35:46 -0400 Subject: [PATCH 07/15] Added example implementation of httptextformat.h as HttpTraceContext.cc. Some dependencies and data structure questions are raised in the form of comments -- i.e. no explanatory comments but questions. --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index ae5b7926b2..d1e2d69f2e 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,8 @@ # Bazel files /bazel-* +/.idea/ +/.idea/.gitignore +/.idea/modules.xml +/.idea/opentelemetry-cpp.iml +/.idea/vcs.xml From 3088385c1ecb294ba0c62ead2f12a313eb9c1143 Mon Sep 17 00:00:00 2001 From: tz68 Date: Wed, 24 Jun 2020 23:43:29 -0400 Subject: [PATCH 08/15] Changed comment position, and added names to getter and setter arguments --- .../trace/propagation/httptextformat.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/api/include/opentelemetry/trace/propagation/httptextformat.h b/api/include/opentelemetry/trace/propagation/httptextformat.h index f3eabae7af..51b9ca870f 100644 --- a/api/include/opentelemetry/trace/propagation/httptextformat.h +++ b/api/include/opentelemetry/trace/propagation/httptextformat.h @@ -14,16 +14,16 @@ namespace propagation virtual static Context SetSpanInContext(Span span, Context &context = NULL) virtual static Span GetCurrentSpan(Context &context = NULL) +// The HTTPTextFormat class provides an interface that enables extracting and injecting +// context into headers of HTTP requests. HTTP frameworks and clients +// can integrate with HTTPTextFormat by providing the object containing the +// headers, and a getter and setter function for the extraction and +// injection of values, respectively. template class HTTPTextFormat { - /** This class provides an interface that enables extracting and injecting - context into headers of HTTP requests. HTTP frameworks and clients - can integrate with HTTPTextFormat by providing the object containing the - headers, and a getter and setter function for the extraction and - injection of values, respectively.*/ public: - using Getter = nostd::string_view(*)(nostd::string_view,nostd::string_view); - using Setter = void(*)(T&,nostd::string_view,nostd::string); + using Getter = nostd::string_view(*)(T &carrier, nostd::string_view trace_type); + using Setter = void(*)(T &carrier, nostd::string_view trace_type,nostd::string trace_description); virtual Context extract(Getter get_from_carrier, const T &carrier, Context &context) virtual void inject(Setter set_from_carrier, T &carrier, const Context &context) }; From 0dd2dc2bc63b9991da601a7c388056666115c319 Mon Sep 17 00:00:00 2001 From: Tianlin-Zhao <66151597+Tianlin-Zhao@users.noreply.github.com> Date: Thu, 25 Jun 2020 13:51:49 -0400 Subject: [PATCH 09/15] Update api/include/opentelemetry/trace/propagation/HttpTraceContext.cc Co-authored-by: Kirk Kelsey --- api/include/opentelemetry/trace/propagation/HttpTraceContext.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/include/opentelemetry/trace/propagation/HttpTraceContext.cc b/api/include/opentelemetry/trace/propagation/HttpTraceContext.cc index 1411641ffa..fe02e801a7 100644 --- a/api/include/opentelemetry/trace/propagation/HttpTraceContext.cc +++ b/api/include/opentelemetry/trace/propagation/HttpTraceContext.cc @@ -126,4 +126,4 @@ class HttpTraceContext : public HTTPTextFormat } } } -} \ No newline at end of file +} // namespace trace From 376f446f5243565feea4e17fecdd2a72b0efd463 Mon Sep 17 00:00:00 2001 From: Tianlin-Zhao <66151597+Tianlin-Zhao@users.noreply.github.com> Date: Thu, 25 Jun 2020 13:51:56 -0400 Subject: [PATCH 10/15] Update api/include/opentelemetry/trace/propagation/httptextformat.h Co-authored-by: Kirk Kelsey --- api/include/opentelemetry/trace/propagation/httptextformat.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/include/opentelemetry/trace/propagation/httptextformat.h b/api/include/opentelemetry/trace/propagation/httptextformat.h index 51b9ca870f..70eab34ad2 100644 --- a/api/include/opentelemetry/trace/propagation/httptextformat.h +++ b/api/include/opentelemetry/trace/propagation/httptextformat.h @@ -28,5 +28,5 @@ class HTTPTextFormat { virtual void inject(Setter set_from_carrier, T &carrier, const Context &context) }; } -} -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE +OPENTELEMETRY_END_NAMESPACE From e1fbbeede4d64d731a63c1414c8b633df759b124 Mon Sep 17 00:00:00 2001 From: Tianlin-Zhao <66151597+Tianlin-Zhao@users.noreply.github.com> Date: Thu, 25 Jun 2020 14:46:24 -0400 Subject: [PATCH 11/15] Update api/include/opentelemetry/trace/propagation/httptextformat.h Co-authored-by: Kirk Kelsey --- api/include/opentelemetry/trace/propagation/httptextformat.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/include/opentelemetry/trace/propagation/httptextformat.h b/api/include/opentelemetry/trace/propagation/httptextformat.h index 70eab34ad2..922aedb670 100644 --- a/api/include/opentelemetry/trace/propagation/httptextformat.h +++ b/api/include/opentelemetry/trace/propagation/httptextformat.h @@ -24,8 +24,8 @@ class HTTPTextFormat { public: using Getter = nostd::string_view(*)(T &carrier, nostd::string_view trace_type); using Setter = void(*)(T &carrier, nostd::string_view trace_type,nostd::string trace_description); - virtual Context extract(Getter get_from_carrier, const T &carrier, Context &context) - virtual void inject(Setter set_from_carrier, T &carrier, const Context &context) + virtual Context extract(Getter get_from_carrier, const T &carrier, Context &context) = 0; + virtual void inject(Setter set_from_carrier, T &carrier, const Context &context) = 0; }; } OPENTELEMETRY_END_NAMESPACE From 1885a3be1b46c36906682e9fb78fc5bdc31c9565 Mon Sep 17 00:00:00 2001 From: TianlinZhao Date: Thu, 25 Jun 2020 15:02:55 -0400 Subject: [PATCH 12/15] Resolved issues on PR --- .../trace/propagation/HttpTraceContext.cc | 62 +++++++++---------- .../trace/propagation/httptextformat.h | 9 ++- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/api/include/opentelemetry/trace/propagation/HttpTraceContext.cc b/api/include/opentelemetry/trace/propagation/HttpTraceContext.cc index fe02e801a7..20ce421570 100644 --- a/api/include/opentelemetry/trace/propagation/HttpTraceContext.cc +++ b/api/include/opentelemetry/trace/propagation/HttpTraceContext.cc @@ -1,18 +1,42 @@ #include +OPENTELEMETRY_BEGIN_NAMESPACE namespace trace { namespace propagation { namespace { +// The HttpTraceContext provides methods to extract and inject +// context into headers of HTTP requests with traces. +// Example: +// HttpTraceContext.inject(setter,&carrier,&context); +// HttpTraceContext.extract(getter,&carrier,&context); class HttpTraceContext : public HTTPTextFormat { + public: + List fields() { + static const auto* FIELDS = new std::vector({TRACE_PARENT, TRACE_STATE}); + return FIELDS; + } + + void inject(Setter setter, T &carrier, const Context &context) override { + Span span = GetCurrentSpan(context); + if (span == null || !span.getContext().isValid()) { + // We don't have span.getContext() in span.h, should we just use span? As well as acquiring validity. (I do know how to implement them though) + return; + } + injectImpl(span.getContext(), carrier, setter); // span.getContext() function needed + } + + Context extract(Getter getter, const T &carrier, Context &context) override { + SpanContext spanContext = extractImpl(carrier, getter); + return SetSpanInContext(trace.DefaultSpan(spanContext), context); // I actually need a default span class (implemented) here, I don't know who to ask for though. But both python and java version have default span classes though. + } private: static const nostd::string_view TRACE_PARENT = "traceparent"; static const nostd::string_view TRACE_STATE = "tracestate"; - static const std::vector FIELDS = {TRACE_PARENT, TRACE_STATE)}; static const int VERSION_BYTES = 2; static const int TRACE_ID_BYTES = 32; static const int PARENT_ID_BYTES = 16; @@ -20,17 +44,17 @@ class HttpTraceContext : public HTTPTextFormat static const int TRACE_DELIMITER_BYTES = 3; static const int HEADER_SIZE = VERSION_BYTES + TRACE_ID_BYTES + PARENT_ID_BYTES + TRACE_FLAG_BYTES + TRACE_DELIMITER_BYTES; - static T checkNotNull(T arg, nostd::string_view errorMessage) { + static Context checkNotNull(Context &arg, nostd::string_view errorMessage) { if (arg == NULL) { - throw new NullPointerException(errorMessage); + throw new NullPointerException("error: null " + errorMessage); } return arg; } - static void injectImpl(Setter setter, T &carrier, const SpanContext spanContext) { + static void injectImpl(Setter setter, T &carrier, const SpanContext &spanContext) { char hex_string[HEADER_SIZE]; sprintf(hex_string, "00-%032x-%016x-%02x",span_context.trace_id,span_context.span_id,span_context.trace_flags); - nostd::string_view traceparent_string(hex_string); // I don't know if the string view has a function that takes in an char array convert it to string though. + nostd::string_view traceparent_string = nostd::string_view(hex_string, HEADER_SIZE); setter(carrier, TRACE_PARENT, traceparent_string); if (spanContext.getTraceState()) { nostd::string_view tracestate_string = formatTracestate(span_context.getTraceState()); // I need the definition for the type of TraceState(Dictionary or something else) @@ -38,7 +62,7 @@ class HttpTraceContext : public HTTPTextFormat } } - static SpanContext extractContextFromTraceParent(nostd::string_view traceparent) { + static SpanContext extractContextFromTraceParent(nostd::string_view &traceparent) { bool isValid = traceparent.length() == HEADER_SIZE && traceparent[VERSION_BYTES] == "-" && traceparent[VERSION_BYTES+TRACE_ID_BYTES+1] == "-" @@ -98,32 +122,8 @@ class HttpTraceContext : public HTTPTextFormat return contextFromParentHeader; } } - public: - List fields() { - return FIELDS; - } - - void inject(Setter setter, T &carrier, const Context &context) { - checkNotNull(context, "context"); - checkNotNull(setter, "setter"); - - Span span = GetCurrentSpan(context); - if (span == null || !span.getContext().isValid()) { - // We don't have span.getContext() in span.h, should we just use span? As well as acquiring validity. (I do know how to implement them though) - return; - } - injectImpl(span.getContext(), carrier, setter); // span.getContext() function needed - } - - Context extract(Getter get_from_carrier, const T &carrier, Context &context) { - checkNotNull(context, "context"); - checkNotNull(carrier, "carrier"); - checkNotNull(getter, "getter"); - - SpanContext spanContext = extractImpl(carrier, getter); - return SetSpanInContext(trace.DefaultSpan(spanContext), context); // I actually need a default span class (implemented) here, I don't know who to ask for though. But both python and java version have default span classes though. - } } } } } // namespace trace +OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/trace/propagation/httptextformat.h b/api/include/opentelemetry/trace/propagation/httptextformat.h index 922aedb670..556bc65459 100644 --- a/api/include/opentelemetry/trace/propagation/httptextformat.h +++ b/api/include/opentelemetry/trace/propagation/httptextformat.h @@ -22,11 +22,18 @@ virtual static Span GetCurrentSpan(Context &context = NULL) template class HTTPTextFormat { public: + // Rules that manages how context will be extracted from carrier. using Getter = nostd::string_view(*)(T &carrier, nostd::string_view trace_type); + + // Rules that manages how context will be injected to carrier. using Setter = void(*)(T &carrier, nostd::string_view trace_type,nostd::string trace_description); + + // Returns the context that is stored in the HTTP header carrier with self defined rules. virtual Context extract(Getter get_from_carrier, const T &carrier, Context &context) = 0; + + // Sets the context for a HTTP header carrier with self defined rules. virtual void inject(Setter set_from_carrier, T &carrier, const Context &context) = 0; }; } -OPENTELEMETRY_END_NAMESPACE +} OPENTELEMETRY_END_NAMESPACE From 263f617d3217a7eeda5261c596ce70d3eb60d526 Mon Sep 17 00:00:00 2001 From: TianlinZhao Date: Thu, 25 Jun 2020 16:49:20 -0400 Subject: [PATCH 13/15] release version (no implementation, headers only) --- .gitignore | 1 + .../trace/propagation/HttpTraceContext.cc | 129 ------------------ 2 files changed, 1 insertion(+), 129 deletions(-) delete mode 100644 api/include/opentelemetry/trace/propagation/HttpTraceContext.cc diff --git a/.gitignore b/.gitignore index d1e2d69f2e..7463ceeeb3 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,4 @@ /.idea/modules.xml /.idea/opentelemetry-cpp.iml /.idea/vcs.xml +/.idea/workspace.xml \ No newline at end of file diff --git a/api/include/opentelemetry/trace/propagation/HttpTraceContext.cc b/api/include/opentelemetry/trace/propagation/HttpTraceContext.cc deleted file mode 100644 index 20ce421570..0000000000 --- a/api/include/opentelemetry/trace/propagation/HttpTraceContext.cc +++ /dev/null @@ -1,129 +0,0 @@ -#include - -OPENTELEMETRY_BEGIN_NAMESPACE -namespace trace -{ -namespace propagation -{ -namespace -{ -// The HttpTraceContext provides methods to extract and inject -// context into headers of HTTP requests with traces. -// Example: -// HttpTraceContext.inject(setter,&carrier,&context); -// HttpTraceContext.extract(getter,&carrier,&context); -class HttpTraceContext : public HTTPTextFormat -{ - public: - List fields() { - static const auto* FIELDS = new std::vector({TRACE_PARENT, TRACE_STATE}); - return FIELDS; - } - - void inject(Setter setter, T &carrier, const Context &context) override { - Span span = GetCurrentSpan(context); - if (span == null || !span.getContext().isValid()) { - // We don't have span.getContext() in span.h, should we just use span? As well as acquiring validity. (I do know how to implement them though) - return; - } - injectImpl(span.getContext(), carrier, setter); // span.getContext() function needed - } - - Context extract(Getter getter, const T &carrier, Context &context) override { - SpanContext spanContext = extractImpl(carrier, getter); - return SetSpanInContext(trace.DefaultSpan(spanContext), context); // I actually need a default span class (implemented) here, I don't know who to ask for though. But both python and java version have default span classes though. - } - - private: - static const nostd::string_view TRACE_PARENT = "traceparent"; - static const nostd::string_view TRACE_STATE = "tracestate"; - static const int VERSION_BYTES = 2; - static const int TRACE_ID_BYTES = 32; - static const int PARENT_ID_BYTES = 16; - static const int TRACE_FLAG_BYTES = 2; - static const int TRACE_DELIMITER_BYTES = 3; - static const int HEADER_SIZE = VERSION_BYTES + TRACE_ID_BYTES + PARENT_ID_BYTES + TRACE_FLAG_BYTES + TRACE_DELIMITER_BYTES; - - static Context checkNotNull(Context &arg, nostd::string_view errorMessage) { - if (arg == NULL) { - throw new NullPointerException("error: null " + errorMessage); - } - return arg; - } - - static void injectImpl(Setter setter, T &carrier, const SpanContext &spanContext) { - char hex_string[HEADER_SIZE]; - sprintf(hex_string, "00-%032x-%016x-%02x",span_context.trace_id,span_context.span_id,span_context.trace_flags); - nostd::string_view traceparent_string = nostd::string_view(hex_string, HEADER_SIZE); - setter(carrier, TRACE_PARENT, traceparent_string); - if (spanContext.getTraceState()) { - nostd::string_view tracestate_string = formatTracestate(span_context.getTraceState()); // I need the definition for the type of TraceState(Dictionary or something else) - setter(carrier, TRACE_STATE, tracestate_string); - } - } - - static SpanContext extractContextFromTraceParent(nostd::string_view &traceparent) { - bool isValid = traceparent.length() == HEADER_SIZE - && traceparent[VERSION_BYTES] == "-" - && traceparent[VERSION_BYTES+TRACE_ID_BYTES+1] == "-" - && traceparent[VERSION_BYTES+TRACE_ID_BYTES+PARENT_ID_BYTES+2] == "-"; - if (!isValid) { - std::cout<<"Unparseable traceparent header. Returning INVALID span context."< Date: Thu, 25 Jun 2020 22:59:38 -0400 Subject: [PATCH 14/15] virtual functions definition --- api/include/opentelemetry/trace/propagation/httptextformat.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/include/opentelemetry/trace/propagation/httptextformat.h b/api/include/opentelemetry/trace/propagation/httptextformat.h index 556bc65459..7faf9a5282 100644 --- a/api/include/opentelemetry/trace/propagation/httptextformat.h +++ b/api/include/opentelemetry/trace/propagation/httptextformat.h @@ -11,8 +11,8 @@ namespace trace namespace propagation { -virtual static Context SetSpanInContext(Span span, Context &context = NULL) -virtual static Span GetCurrentSpan(Context &context = NULL) +virtual static Context SetSpanInContext(Span span, Context &context) = 0; +virtual static Span GetCurrentSpan(Context &context) = 0; // The HTTPTextFormat class provides an interface that enables extracting and injecting // context into headers of HTTP requests. HTTP frameworks and clients From 7079eb57ff7c9e49eda2e5ef91923a5452e6c855 Mon Sep 17 00:00:00 2001 From: TianlinZhao Date: Thu, 25 Jun 2020 23:22:08 -0400 Subject: [PATCH 15/15] Function descriptors for get and set Spans --- api/include/opentelemetry/trace/propagation/httptextformat.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/include/opentelemetry/trace/propagation/httptextformat.h b/api/include/opentelemetry/trace/propagation/httptextformat.h index 7faf9a5282..3a9206426e 100644 --- a/api/include/opentelemetry/trace/propagation/httptextformat.h +++ b/api/include/opentelemetry/trace/propagation/httptextformat.h @@ -10,8 +10,9 @@ namespace trace { namespace propagation { - +// Set the span in the given context. virtual static Context SetSpanInContext(Span span, Context &context) = 0; +// Retrieve the current span. virtual static Span GetCurrentSpan(Context &context) = 0; // The HTTPTextFormat class provides an interface that enables extracting and injecting