Skip to content

Commit

Permalink
Cleanup: Get Span from Context, Set Span to Context (open-telemetry#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored May 7, 2021
1 parent 32f10c7 commit 5a97750
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 40 deletions.
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 @@ -47,7 +47,7 @@ class B3PropagatorExtractor : public opentelemetry::context::propagation::TextMa
{
SpanContext span_context = ExtractImpl(carrier);
nostd::shared_ptr<Span> sp{new DefaultSpan(span_context)};
return context.SetValue(kSpanKey, sp);
return SetSpan(context, sp);
}

static TraceId TraceIdFromHex(nostd::string_view trace_id)
Expand Down Expand Up @@ -128,7 +128,7 @@ class B3Propagator : public B3PropagatorExtractor
void Inject(opentelemetry::context::propagation::TextMapCarrier &carrier,
const context::Context &context) noexcept override
{
SpanContext span_context = detail::GetCurrentSpan(context);
SpanContext span_context = GetSpan(context)->GetContext();
if (!span_context.IsValid())
{
return;
Expand All @@ -155,7 +155,7 @@ class B3PropagatorMultiHeader : public B3PropagatorExtractor
void Inject(opentelemetry::context::propagation::TextMapCarrier &carrier,
const context::Context &context) noexcept override
{
SpanContext span_context = detail::GetCurrentSpan(context);
SpanContext span_context = GetSpan(context)->GetContext();
if (!span_context.IsValid())
{
return;
Expand Down
15 changes: 9 additions & 6 deletions api/include/opentelemetry/trace/propagation/detail/context.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
#pragma once
#include "opentelemetry/context/context.h"
#include "opentelemetry/trace/default_span.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace trace
{
namespace propagation
{
namespace detail
{

inline trace::SpanContext GetCurrentSpan(const context::Context &context)
inline nostd::shared_ptr<trace::Span> GetSpan(const context::Context &context)
{
context::ContextValue span = context.GetValue(trace::kSpanKey);
if (nostd::holds_alternative<nostd::shared_ptr<trace::Span>>(span))
{
return nostd::get<nostd::shared_ptr<trace::Span>>(span).get()->GetContext();
return nostd::get<nostd::shared_ptr<trace::Span>>(span);
}
return nostd::shared_ptr<trace::Span>(new DefaultSpan(SpanContext::GetInvalid()));
}

inline context::Context SetSpan(context::Context &context, nostd::shared_ptr<trace::Span> span)
{

return trace::SpanContext::GetInvalid();
return context.SetValue(kSpanKey, span);
}

} // namespace detail
} // namespace propagation
} // namespace trace
OPENTELEMETRY_END_NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class HttpTraceContext : public opentelemetry::context::propagation::TextMapProp
void Inject(opentelemetry::context::propagation::TextMapCarrier &carrier,
const context::Context &context) noexcept override
{
SpanContext span_context = detail::GetCurrentSpan(context);
SpanContext span_context = GetSpan(context)->GetContext();
if (!span_context.IsValid())
{
return;
Expand All @@ -58,7 +58,7 @@ class HttpTraceContext : public opentelemetry::context::propagation::TextMapProp
{
SpanContext span_context = ExtractImpl(carrier);
nostd::shared_ptr<Span> sp{new DefaultSpan(span_context)};
return context.SetValue(kSpanKey, sp);
return SetSpan(context, sp);
}

static TraceId TraceIdFromHex(nostd::string_view trace_id)
Expand Down
4 changes: 2 additions & 2 deletions api/include/opentelemetry/trace/propagation/jaeger.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class JaegerPropagator : public context::propagation::TextMapPropagator
void Inject(context::propagation::TextMapCarrier &carrier,
const context::Context &context) noexcept override
{
SpanContext span_context = detail::GetCurrentSpan(context);
SpanContext span_context = GetSpan(context)->GetContext();
if (!span_context.IsValid())
{
return;
Expand Down Expand Up @@ -64,7 +64,7 @@ class JaegerPropagator : public context::propagation::TextMapPropagator
{
SpanContext span_context = ExtractImpl(carrier);
nostd::shared_ptr<Span> sp{new DefaultSpan(span_context)};
return context.SetValue(kSpanKey, sp);
return SetSpan(context, sp);
}

private:
Expand Down
4 changes: 2 additions & 2 deletions api/include/opentelemetry/trace/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class Tracer
* @param span the span that should be set as the new active span.
* @return a Scope that controls how long the span will be active.
*/
nostd::unique_ptr<Scope> WithActiveSpan(nostd::shared_ptr<Span> &span) noexcept
static nostd::unique_ptr<Scope> WithActiveSpan(nostd::shared_ptr<Span> &span) noexcept
{
return nostd::unique_ptr<Scope>(new Scope{span});
}
Expand All @@ -150,7 +150,7 @@ class Tracer
* @return the currently active span, or an invalid default span if no span
* is active.
*/
nostd::shared_ptr<Span> GetCurrentSpan() noexcept
static nostd::shared_ptr<Span> GetCurrentSpan() noexcept
{
context::ContextValue active_span = context::RuntimeContext::GetValue(kSpanKey);
if (nostd::holds_alternative<nostd::shared_ptr<Span>>(active_span))
Expand Down
2 changes: 1 addition & 1 deletion api/test/trace/propagation/http_text_format_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ TEST(TextMapPropagatorTest, InvalidIdentitiesAreNotExtracted)
context::Context ctx1 = context::Context{};
context::Context ctx2 = format.Extract(carrier, ctx1);

auto span = trace::propagation::detail::GetCurrentSpan(ctx2);
auto span = trace::propagation::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 @@ -95,7 +95,7 @@ TEST(JaegerPropagatorTest, ExtractValidSpans)
context::Context ctx1 = context::Context{};
context::Context ctx2 = format.Extract(carrier, ctx1);

auto span = trace::propagation::detail::GetCurrentSpan(ctx2);
auto span = trace::propagation::GetSpan(ctx2)->GetContext();
EXPECT_TRUE(span.IsValid());

EXPECT_EQ(Hex(span.trace_id()), test_trace.expected_trace_id);
Expand Down Expand Up @@ -126,7 +126,7 @@ TEST(JaegerPropagatorTest, ExctractInvalidSpans)
context::Context ctx1 = context::Context{};
context::Context ctx2 = format.Extract(carrier, ctx1);

auto span = trace::propagation::detail::GetCurrentSpan(ctx2);
auto span = trace::propagation::GetSpan(ctx2)->GetContext();
EXPECT_FALSE(span.IsValid());
}
}
Expand Down
24 changes: 2 additions & 22 deletions sdk/src/trace/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,14 @@ Tracer::Tracer(std::shared_ptr<sdk::trace::TracerContext> context,
: context_{context}, instrumentation_library_{std::move(instrumentation_library)}
{}

trace_api::SpanContext GetCurrentSpanContext(const trace_api::SpanContext &explicit_parent)
{
// Use the explicit parent, if it's valid.
if (explicit_parent.IsValid())
{
return explicit_parent;
}

// Use the currently active span, if there's one.
auto curr_span_context = context::RuntimeContext::GetValue(trace_api::kSpanKey);

if (nostd::holds_alternative<nostd::shared_ptr<trace_api::Span>>(curr_span_context))
{
auto curr_span = nostd::get<nostd::shared_ptr<trace_api::Span>>(curr_span_context);
return curr_span->GetContext();
}

// Otherwise return an invalid SpanContext.
return trace_api::SpanContext::GetInvalid();
}

nostd::shared_ptr<trace_api::Span> Tracer::StartSpan(
nostd::string_view name,
const opentelemetry::common::KeyValueIterable &attributes,
const trace_api::SpanContextKeyValueIterable &links,
const trace_api::StartSpanOptions &options) noexcept
{
trace_api::SpanContext parent = GetCurrentSpanContext(options.parent);
trace_api::SpanContext parent =
options.parent.IsValid() ? options.parent : GetCurrentSpan()->GetContext();

auto sampling_result = context_->GetSampler().ShouldSample(parent, parent.trace_id(), name,
options.kind, attributes, links);
Expand Down

0 comments on commit 5a97750

Please sign in to comment.