Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate trace_flags from parent span to child span #603

Merged
merged 4 commits into from
Mar 10, 2021
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
13 changes: 8 additions & 5 deletions sdk/src/trace/span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
const trace_api::SpanContextKeyValueIterable &links,
const trace_api::StartSpanOptions &options,
const trace_api::SpanContext &parent_span_context,
const opentelemetry::sdk::resource::Resource &resource) noexcept
const opentelemetry::sdk::resource::Resource &resource,
const bool sampled) noexcept
: tracer_{std::move(tracer)},
processor_{processor},
recordable_{processor_->MakeRecordable()},
Expand Down Expand Up @@ -93,10 +94,12 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
recordable_->SetIds(trace_id, span_id, trace_api::SpanId());
}

span_context_ = std::unique_ptr<trace_api::SpanContext>(
new trace_api::SpanContext(trace_id, span_id, trace_api::TraceFlags(), false,
is_parent_span_valid ? parent_span_context.trace_state()
: trace_api::TraceState::GetDefault()));
span_context_ = std::unique_ptr<trace_api::SpanContext>(new trace_api::SpanContext(
trace_id, span_id,
sampled ? trace_api::TraceFlags{trace_api::TraceFlags::kIsSampled} : trace_api::TraceFlags{},
false,
is_parent_span_valid ? parent_span_context.trace_state()
: trace_api::TraceState::GetDefault()));

attributes.ForEachKeyValue(
[&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept {
Expand Down
17 changes: 9 additions & 8 deletions sdk/src/trace/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ namespace trace_api = opentelemetry::trace;
class Span final : public trace_api::Span
{
public:
explicit Span(std::shared_ptr<Tracer> &&tracer,
std::shared_ptr<SpanProcessor> processor,
nostd::string_view name,
const opentelemetry::common::KeyValueIterable &attributes,
const trace_api::SpanContextKeyValueIterable &links,
const trace_api::StartSpanOptions &options,
const trace_api::SpanContext &parent_span_context,
const opentelemetry::sdk::resource::Resource &resource) noexcept;
Span(std::shared_ptr<Tracer> &&tracer,
std::shared_ptr<SpanProcessor> processor,
nostd::string_view name,
const opentelemetry::common::KeyValueIterable &attributes,
const trace_api::SpanContextKeyValueIterable &links,
const trace_api::StartSpanOptions &options,
const trace_api::SpanContext &parent_span_context,
const opentelemetry::sdk::resource::Resource &resource,
const bool sampled = false) noexcept;

~Span() override;

Expand Down
2 changes: 1 addition & 1 deletion sdk/src/trace/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ nostd::shared_ptr<trace_api::Span> Tracer::StartSpan(
{
auto span = nostd::shared_ptr<trace_api::Span>{
new (std::nothrow) Span{this->shared_from_this(), processor_.load(), name, attributes,
links, options, parent, resource_}};
links, options, parent, resource_, true}};

// if the attributes is not nullptr, add attributes to the span.
if (sampling_result.attributes)
Expand Down
49 changes: 49 additions & 0 deletions sdk/test/trace/tracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ using opentelemetry::common::KeyValueIterableView;
using opentelemetry::exporter::memory::InMemorySpanData;
using opentelemetry::exporter::memory::InMemorySpanExporter;
using opentelemetry::trace::SpanContext;
using opentelemetry::trace::TraceFlags;

/**
* A mock sampler that returns non-empty sampling results attributes.
Expand Down Expand Up @@ -563,3 +564,51 @@ TEST(Tracer, ExpectParent)
EXPECT_EQ(spandata_first->GetSpanId(), spandata_second->GetParentSpanId());
EXPECT_EQ(spandata_second->GetSpanId(), spandata_third->GetParentSpanId());
}

TEST(Tracer, ExpectParentWithValidSpanContext)
{
std::unique_ptr<InMemorySpanExporter> exporter(new InMemorySpanExporter());
std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
auto tracer = initTracer(std::move(exporter));
auto spans = span_data.get()->GetSpans();

ASSERT_EQ(0, spans.size());

// produce valid SpanContext with pseudo span and trace Id.
uint8_t span_id_buf[trace_api::SpanId::kSize] = {
1,
};
trace_api::SpanId span_id{span_id_buf};
uint8_t trace_id_buf[trace_api::TraceId::kSize] = {
2,
};
trace_api::TraceId trace_id{trace_id_buf};

trace_api::StartSpanOptions options;
options.parent = SpanContext(trace_id, span_id, TraceFlags{TraceFlags::kIsSampled}, true);

auto span_first = tracer->StartSpan("span 1", options);

EXPECT_EQ(span_first->GetContext().trace_flags().IsSampled(), true);

options.parent = span_first->GetContext();
auto span_second = tracer->StartSpan("span 2", options);
EXPECT_EQ(span_second->GetContext().trace_flags().IsSampled(), true);

options.parent = span_second->GetContext();
auto span_third = tracer->StartSpan("span 3", options);
EXPECT_EQ(span_third->GetContext().trace_flags().IsSampled(), true);

span_third->End();
span_second->End();
span_first->End();

spans = span_data->GetSpans();
ASSERT_EQ(3, spans.size());
auto spandata_first = std::move(spans.at(2));
auto spandata_second = std::move(spans.at(1));
auto spandata_third = std::move(spans.at(0));

EXPECT_EQ(spandata_first->GetSpanId(), spandata_second->GetParentSpanId());
EXPECT_EQ(spandata_second->GetSpanId(), spandata_third->GetParentSpanId());
}