From 96c067ce721dfeb5504b92a3b4feea71d4a940ea Mon Sep 17 00:00:00 2001 From: Lalit Date: Thu, 2 Sep 2021 06:21:33 +0000 Subject: [PATCH 1/2] make span context management public --- api/include/opentelemetry/trace/contex.h | 33 +++++++++++++++++++ .../trace/propagation/b3_propagator.h | 6 ++-- .../trace/propagation/detail/context.h | 32 ------------------ .../trace/propagation/http_trace_context.h | 6 ++-- .../opentelemetry/trace/propagation/jaeger.h | 6 ++-- .../propagation/http_text_format_test.cc | 3 +- .../propagation/jaeger_propagation_test.cc | 4 +-- api/test/trace/span_benchmark.cc | 4 +-- examples/grpc/server.cpp | 8 +++-- examples/http/server.cc | 5 +-- 10 files changed, 56 insertions(+), 51 deletions(-) create mode 100644 api/include/opentelemetry/trace/contex.h delete mode 100644 api/include/opentelemetry/trace/propagation/detail/context.h diff --git a/api/include/opentelemetry/trace/contex.h b/api/include/opentelemetry/trace/contex.h new file mode 100644 index 0000000000..165f1540b9 --- /dev/null +++ b/api/include/opentelemetry/trace/contex.h @@ -0,0 +1,33 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/context/context.h" +#include "opentelemetry/trace/default_span.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace trace +{ + +// Get Span from explicit context +inline nostd::shared_ptr GetSpan(const opentelemetry::context::Context &context) +{ + context::ContextValue span = context.GetValue(kSpanKey); + if (nostd::holds_alternative>(span)) + { + return nostd::get>(span); + } + return nostd::shared_ptr(new DefaultSpan(SpanContext::GetInvalid())); +} + +// Set Span into explicit context +inline context::Context SetSpan(opentelemetry::context::Context &context, + nostd::shared_ptr span) +{ + return context.SetValue(kSpanKey, span); +} + +} // namespace trace +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/api/include/opentelemetry/trace/propagation/b3_propagator.h b/api/include/opentelemetry/trace/propagation/b3_propagator.h index f9485bd619..d5661cb2a9 100644 --- a/api/include/opentelemetry/trace/propagation/b3_propagator.h +++ b/api/include/opentelemetry/trace/propagation/b3_propagator.h @@ -3,10 +3,10 @@ #pragma once -#include "detail/context.h" #include "detail/hex.h" #include "detail/string.h" #include "opentelemetry/context/propagation/text_map_propagator.h" +#include "opentelemetry/trace/context.h" #include "opentelemetry/trace/default_span.h" #include @@ -50,7 +50,7 @@ class B3PropagatorExtractor : public opentelemetry::context::propagation::TextMa { SpanContext span_context = ExtractImpl(carrier); nostd::shared_ptr sp{new DefaultSpan(span_context)}; - return SetSpan(context, sp); + return trace::SetSpan(context, sp); } static TraceId TraceIdFromHex(nostd::string_view trace_id) @@ -131,7 +131,7 @@ class B3Propagator : public B3PropagatorExtractor void Inject(opentelemetry::context::propagation::TextMapCarrier &carrier, const context::Context &context) noexcept override { - SpanContext span_context = GetSpan(context)->GetContext(); + SpanContext span_context = trace::GetSpan(context)->GetContext(); if (!span_context.IsValid()) { return; diff --git a/api/include/opentelemetry/trace/propagation/detail/context.h b/api/include/opentelemetry/trace/propagation/detail/context.h deleted file mode 100644 index 728a198911..0000000000 --- a/api/include/opentelemetry/trace/propagation/detail/context.h +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -#pragma once -#include "opentelemetry/context/context.h" -#include "opentelemetry/trace/default_span.h" - -OPENTELEMETRY_BEGIN_NAMESPACE -namespace trace -{ -namespace propagation -{ - -inline nostd::shared_ptr GetSpan(const context::Context &context) -{ - context::ContextValue span = context.GetValue(trace::kSpanKey); - if (nostd::holds_alternative>(span)) - { - return nostd::get>(span); - } - return nostd::shared_ptr(new DefaultSpan(SpanContext::GetInvalid())); -} - -inline context::Context SetSpan(context::Context &context, nostd::shared_ptr span) -{ - - return context.SetValue(kSpanKey, span); -} - -} // namespace propagation -} // namespace trace -OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/trace/propagation/http_trace_context.h b/api/include/opentelemetry/trace/propagation/http_trace_context.h index 41c6b1e61d..eca0d68f28 100644 --- a/api/include/opentelemetry/trace/propagation/http_trace_context.h +++ b/api/include/opentelemetry/trace/propagation/http_trace_context.h @@ -2,11 +2,11 @@ // SPDX-License-Identifier: Apache-2.0 #include -#include "detail/context.h" #include "detail/hex.h" #include "detail/string.h" #include "opentelemetry/context/propagation/text_map_propagator.h" #include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/trace/context.h" #include "opentelemetry/trace/default_span.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -34,7 +34,7 @@ class HttpTraceContext : public opentelemetry::context::propagation::TextMapProp void Inject(opentelemetry::context::propagation::TextMapCarrier &carrier, const context::Context &context) noexcept override { - SpanContext span_context = GetSpan(context)->GetContext(); + SpanContext span_context = trace::GetSpan(context)->GetContext(); if (!span_context.IsValid()) { return; @@ -47,7 +47,7 @@ class HttpTraceContext : public opentelemetry::context::propagation::TextMapProp { SpanContext span_context = ExtractImpl(carrier); nostd::shared_ptr sp{new DefaultSpan(span_context)}; - return SetSpan(context, sp); + return trace::SetSpan(context, sp); } static TraceId TraceIdFromHex(nostd::string_view trace_id) diff --git a/api/include/opentelemetry/trace/propagation/jaeger.h b/api/include/opentelemetry/trace/propagation/jaeger.h index 6203752b91..78fc9e9c07 100644 --- a/api/include/opentelemetry/trace/propagation/jaeger.h +++ b/api/include/opentelemetry/trace/propagation/jaeger.h @@ -3,10 +3,10 @@ #pragma once -#include "detail/context.h" #include "detail/hex.h" #include "detail/string.h" #include "opentelemetry/context/propagation/text_map_propagator.h" +#include "opentelemetry/trace/context.h" #include "opentelemetry/trace/default_span.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -23,7 +23,7 @@ class JaegerPropagator : public context::propagation::TextMapPropagator void Inject(context::propagation::TextMapCarrier &carrier, const context::Context &context) noexcept override { - SpanContext span_context = GetSpan(context)->GetContext(); + SpanContext span_context = trace::GetSpan(context)->GetContext(); if (!span_context.IsValid()) { return; @@ -53,7 +53,7 @@ class JaegerPropagator : public context::propagation::TextMapPropagator { SpanContext span_context = ExtractImpl(carrier); nostd::shared_ptr sp{new DefaultSpan(span_context)}; - return SetSpan(context, sp); + return trace::SetSpan(context, sp); } bool Fields(nostd::function_ref callback) const noexcept override diff --git a/api/test/trace/propagation/http_text_format_test.cc b/api/test/trace/propagation/http_text_format_test.cc index 497d716bb9..a815920a83 100644 --- a/api/test/trace/propagation/http_text_format_test.cc +++ b/api/test/trace/propagation/http_text_format_test.cc @@ -3,6 +3,7 @@ #include "opentelemetry/context/propagation/global_propagator.h" #include "opentelemetry/context/runtime_context.h" +#include "opentelemetry/trace/context.h" #include "opentelemetry/trace/propagation/http_trace_context.h" #include "opentelemetry/trace/scope.h" #include "util.h" @@ -150,7 +151,7 @@ TEST(TextMapPropagatorTest, InvalidIdentitiesAreNotExtracted) context::Context ctx1 = context::Context{}; context::Context ctx2 = format.Extract(carrier, ctx1); - auto span = trace::propagation::GetSpan(ctx2)->GetContext(); + auto span = trace::GetSpan(ctx2)->GetContext(); EXPECT_FALSE(span.IsValid()); } } diff --git a/api/test/trace/propagation/jaeger_propagation_test.cc b/api/test/trace/propagation/jaeger_propagation_test.cc index a0e0ade270..c337617383 100644 --- a/api/test/trace/propagation/jaeger_propagation_test.cc +++ b/api/test/trace/propagation/jaeger_propagation_test.cc @@ -98,7 +98,7 @@ TEST(JaegerPropagatorTest, ExtractValidSpans) context::Context ctx1 = context::Context{}; context::Context ctx2 = format.Extract(carrier, ctx1); - auto span = trace::propagation::GetSpan(ctx2)->GetContext(); + auto span = trace::GetSpan(ctx2)->GetContext(); EXPECT_TRUE(span.IsValid()); EXPECT_EQ(Hex(span.trace_id()), test_trace.expected_trace_id); @@ -129,7 +129,7 @@ TEST(JaegerPropagatorTest, ExctractInvalidSpans) context::Context ctx1 = context::Context{}; context::Context ctx2 = format.Extract(carrier, ctx1); - auto span = trace::propagation::GetSpan(ctx2)->GetContext(); + auto span = trace::GetSpan(ctx2)->GetContext(); EXPECT_FALSE(span.IsValid()); } } diff --git a/api/test/trace/span_benchmark.cc b/api/test/trace/span_benchmark.cc index a25d8f4a2d..238e8ca8a0 100644 --- a/api/test/trace/span_benchmark.cc +++ b/api/test/trace/span_benchmark.cc @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 #include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/trace/context.h" #include "opentelemetry/trace/noop.h" -#include "opentelemetry/trace/propagation/detail/context.h" #include "opentelemetry/trace/span_id.h" #include "opentelemetry/trace/trace_id.h" @@ -112,7 +112,7 @@ void BM_SpanCreationWitContextPropagation(benchmark::State &state) auto outer_span_context = SpanContext(trace_id, span_id, trace_api::TraceFlags(), false); auto outer_span = nostd::shared_ptr(new trace_api::DefaultSpan(outer_span_context)); - trace_api::propagation::SetSpan(current_ctx, outer_span); + trace_api::SetSpan(current_ctx, outer_span); auto inner_child = tracer->StartSpan("inner"); auto scope = tracer->WithActiveSpan(inner_child); { diff --git a/examples/grpc/server.cpp b/examples/grpc/server.cpp index b22ad2a6ca..9ff855c140 100644 --- a/examples/grpc/server.cpp +++ b/examples/grpc/server.cpp @@ -3,9 +3,11 @@ #else #include "messages.grpc.pb.h" #endif -#include "tracer_common.h" -#include "opentelemetry/trace/span_context_kv_iterable_view.h" + +#include "opentelemetry/trace/context.h" #include "opentelemetry/trace/semantic_conventions.h" +#include "opentelemetry/trace/span_context_kv_iterable_view.h" +#include "tracer_common.h" #include #include @@ -56,7 +58,7 @@ class GreeterServer final : public Greeter::Service auto prop = opentelemetry::context::propagation::GlobalTextMapPropagator::GetGlobalPropagator(); auto current_ctx = opentelemetry::context::RuntimeContext::GetCurrent(); auto new_context = prop->Extract(carrier, current_ctx); - options.parent = opentelemetry::trace::propagation::GetSpan(new_context)->GetContext(); + options.parent = opentelemetry::trace::GetSpan(new_context)->GetContext(); std::string span_name = "GreeterService/Greet"; auto span = get_tracer("grpc") diff --git a/examples/http/server.cc b/examples/http/server.cc index 6c1c9ce713..7358ee92e1 100644 --- a/examples/http/server.cc +++ b/examples/http/server.cc @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #include "server.h" +#include "opentelemetry/trace/context.h" #include "opentelemetry/trace/semantic_conventions.h" #include "tracer_common.h" @@ -12,7 +13,7 @@ namespace { using namespace opentelemetry::trace; -; + uint16_t server_port = 8800; constexpr const char *server_name = "localhost"; @@ -32,7 +33,7 @@ class RequestHandler : public HTTP_SERVER_NS::HttpRequestCallback auto prop = opentelemetry::context::propagation::GlobalTextMapPropagator::GetGlobalPropagator(); auto current_ctx = opentelemetry::context::RuntimeContext::GetCurrent(); auto new_context = prop->Extract(carrier, current_ctx); - options.parent = opentelemetry::trace::propagation::GetSpan(new_context)->GetContext(); + options.parent = GetSpan(new_context)->GetContext(); // start span with parent context extracted from http header auto span = get_tracer("http-server") From fade70dd343ba3102f31f568de871b8b764e4b6c Mon Sep 17 00:00:00 2001 From: Lalit Date: Thu, 2 Sep 2021 06:47:58 +0000 Subject: [PATCH 2/2] rename --- api/include/opentelemetry/trace/{contex.h => context.h} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename api/include/opentelemetry/trace/{contex.h => context.h} (100%) diff --git a/api/include/opentelemetry/trace/contex.h b/api/include/opentelemetry/trace/context.h similarity index 100% rename from api/include/opentelemetry/trace/contex.h rename to api/include/opentelemetry/trace/context.h