Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions sdk/src/trace/span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "src/common/random.h"

#include "opentelemetry/context/runtime_context.h"
#include "opentelemetry/trace/trace_flags.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand Down Expand Up @@ -77,16 +78,22 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
}
recordable_->SetName(name);

trace_api::TraceId trace_id;
trace_api::SpanId span_id = GenerateRandomSpanId();

if (parent_span_context.IsValid())
{
recordable_->SetIds(parent_span_context.trace_id(), GenerateRandomSpanId(),
parent_span_context.span_id());
trace_id = parent_span_context.trace_id();
recordable_->SetIds(trace_id, span_id, parent_span_context.span_id());
}
else
{
recordable_->SetIds(GenerateRandomTraceId(), GenerateRandomSpanId(), trace_api::SpanId());
trace_id = GenerateRandomTraceId();
recordable_->SetIds(trace_id, span_id, trace_api::SpanId());
}
// TODO: Create and populate SpanContext for this span when SpanContext is fully implemented

span_context_ = std::unique_ptr<trace_api::SpanContext>(
new trace_api::SpanContext(trace_id, span_id, trace_api::TraceFlags(), false));

attributes.ForEachKeyValue([&](nostd::string_view key,
opentelemetry::common::AttributeValue value) noexcept {
Expand Down
20 changes: 15 additions & 5 deletions sdk/src/trace/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ std::shared_ptr<Sampler> Tracer::GetSampler() const noexcept
return sampler_;
}

// Helper function to extract the current span context from the runtime context.
// Returns an invalid span context if the runtime context doesn't contain a span.
trace_api::SpanContext GetCurrentSpanContext()
{
context::ContextValue curr_span_context = context::RuntimeContext::GetValue(SpanKey);

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();
}
return trace_api::SpanContext();
}

nostd::shared_ptr<trace_api::Span> Tracer::StartSpan(
nostd::string_view name,
const trace_api::KeyValueIterable &attributes,
Expand All @@ -47,13 +61,9 @@ nostd::shared_ptr<trace_api::Span> Tracer::StartSpan(
}
else
{
// TODO: Get parent span context from current context. For now we assume all spans
// have no parent, and pass an invalid parent span context to the Span constructor.
const trace_api::SpanContext kInvalidParentSpanContext(false, false);

auto span = nostd::shared_ptr<trace_api::Span>{
new (std::nothrow) Span{this->shared_from_this(), processor_.load(), name, attributes,
options, kInvalidParentSpanContext}};
options, GetCurrentSpanContext()}};

span->SetToken(
nostd::unique_ptr<context::Token>(new context::Token(context::RuntimeContext::Attach(
Expand Down
7 changes: 6 additions & 1 deletion sdk/test/trace/tracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,16 @@ TEST(Tracer, ToMockSpanExporter)
ASSERT_EQ("span 2", spans_received->at(0)->GetName());
EXPECT_TRUE(spans_received->at(0)->GetTraceId().IsValid());
EXPECT_TRUE(spans_received->at(0)->GetSpanId().IsValid());
EXPECT_FALSE(spans_received->at(0)->GetParentSpanId().IsValid());
EXPECT_TRUE(spans_received->at(0)->GetParentSpanId().IsValid());
Comment thread
nadiaciobanu marked this conversation as resolved.

span_first->End();
ASSERT_EQ(2, spans_received->size());

// Verify trace and parent span id propagation
EXPECT_EQ(span_second->GetContext().trace_id(), span_first->GetContext().trace_id());
EXPECT_EQ(spans_received->at(0)->GetTraceId(), spans_received->at(1)->GetTraceId());
EXPECT_EQ(spans_received->at(0)->GetParentSpanId(), spans_received->at(1)->GetSpanId());

ASSERT_EQ("span 1", spans_received->at(1)->GetName());
EXPECT_TRUE(spans_received->at(1)->GetTraceId().IsValid());
EXPECT_TRUE(spans_received->at(1)->GetSpanId().IsValid());
Expand Down