Skip to content

Commit

Permalink
make span context management public (#967)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored Sep 3, 2021
1 parent 7b1842b commit 703576c
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 51 deletions.
33 changes: 33 additions & 0 deletions api/include/opentelemetry/trace/context.h
Original file line number Diff line number Diff line change
@@ -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<Span> GetSpan(const opentelemetry::context::Context &context)
{
context::ContextValue span = context.GetValue(kSpanKey);
if (nostd::holds_alternative<nostd::shared_ptr<Span>>(span))
{
return nostd::get<nostd::shared_ptr<Span>>(span);
}
return nostd::shared_ptr<Span>(new DefaultSpan(SpanContext::GetInvalid()));
}

// Set Span into explicit context
inline context::Context SetSpan(opentelemetry::context::Context &context,
nostd::shared_ptr<Span> span)
{
return context.SetValue(kSpanKey, span);
}

} // namespace trace
OPENTELEMETRY_END_NAMESPACE
6 changes: 3 additions & 3 deletions api/include/opentelemetry/trace/propagation/b3_propagator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <array>
Expand Down Expand Up @@ -50,7 +50,7 @@ class B3PropagatorExtractor : public opentelemetry::context::propagation::TextMa
{
SpanContext span_context = ExtractImpl(carrier);
nostd::shared_ptr<Span> sp{new DefaultSpan(span_context)};
return SetSpan(context, sp);
return trace::SetSpan(context, sp);
}

static TraceId TraceIdFromHex(nostd::string_view trace_id)
Expand Down Expand Up @@ -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;
Expand Down
32 changes: 0 additions & 32 deletions api/include/opentelemetry/trace/propagation/detail/context.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

#include <array>
#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
Expand Down Expand Up @@ -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;
Expand All @@ -47,7 +47,7 @@ class HttpTraceContext : public opentelemetry::context::propagation::TextMapProp
{
SpanContext span_context = ExtractImpl(carrier);
nostd::shared_ptr<Span> sp{new DefaultSpan(span_context)};
return SetSpan(context, sp);
return trace::SetSpan(context, sp);
}

static TraceId TraceIdFromHex(nostd::string_view trace_id)
Expand Down
6 changes: 3 additions & 3 deletions api/include/opentelemetry/trace/propagation/jaeger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -53,7 +53,7 @@ class JaegerPropagator : public context::propagation::TextMapPropagator
{
SpanContext span_context = ExtractImpl(carrier);
nostd::shared_ptr<Span> sp{new DefaultSpan(span_context)};
return SetSpan(context, sp);
return trace::SetSpan(context, sp);
}

bool Fields(nostd::function_ref<bool(nostd::string_view)> callback) const noexcept override
Expand Down
3 changes: 2 additions & 1 deletion api/test/trace/propagation/http_text_format_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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());
}
}
Expand Down
4 changes: 2 additions & 2 deletions api/test/trace/propagation/jaeger_propagation_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
}
Expand Down
4 changes: 2 additions & 2 deletions api/test/trace/span_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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<trace_api::Span>(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);
{
Expand Down
8 changes: 5 additions & 3 deletions examples/grpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <grpcpp/grpcpp.h>
#include <grpcpp/server.h>
Expand Down Expand Up @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions examples/http/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -12,7 +13,7 @@ namespace
{

using namespace opentelemetry::trace;
;

uint16_t server_port = 8800;
constexpr const char *server_name = "localhost";

Expand All @@ -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")
Expand Down

0 comments on commit 703576c

Please sign in to comment.