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
5 changes: 4 additions & 1 deletion sdk/include/opentelemetry/sdk/trace/sampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "opentelemetry/trace/span_context.h"
#include "opentelemetry/trace/span_context_kv_iterable.h"
#include "opentelemetry/trace/trace_id.h"
#include "opentelemetry/trace/trace_state.h"
#include "opentelemetry/version.h"

#include <map>
Expand Down Expand Up @@ -41,6 +42,8 @@ struct SamplingResult
Decision decision;
// A set of span Attributes that will also be added to the Span. Can be nullptr.
std::unique_ptr<const std::map<std::string, opentelemetry::common::AttributeValue>> attributes;
// The tracestate used by the span.
nostd::shared_ptr<opentelemetry::trace::TraceState> trace_state;
};

/**
Expand All @@ -61,7 +64,7 @@ class Sampler
* @param name the name of the new Span.
* @param spanKind the trace_api::SpanKind of the Span.
* @param attributes list of AttributeValue with their keys.
* @param links TODO: Collection of links that will be associated with the Span to be created.
* @param links Collection of links that will be associated with the Span to be created.
* @return sampling result whether span should be sampled or not.
* @since 0.1.0
*/
Expand Down
11 changes: 9 additions & 2 deletions sdk/include/opentelemetry/sdk/trace/samplers/always_off.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@ class AlwaysOffSampler : public Sampler
* @return Returns DROP always
*/
SamplingResult ShouldSample(
const trace_api::SpanContext & /*parent_context*/,
const trace_api::SpanContext &parent_context,
trace_api::TraceId /*trace_id*/,
nostd::string_view /*name*/,
trace_api::SpanKind /*span_kind*/,
const opentelemetry::common::KeyValueIterable & /*attributes*/,
const trace_api::SpanContextKeyValueIterable & /*links*/) noexcept override
{
return {Decision::DROP, nullptr};
if (!parent_context.IsValid())
{
return {Decision::DROP, nullptr, opentelemetry::trace::TraceState::GetDefault()};
}
else
{
return {Decision::DROP, nullptr, parent_context.trace_state()};
}
}

/**
Expand Down
11 changes: 9 additions & 2 deletions sdk/include/opentelemetry/sdk/trace/samplers/always_on.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@ class AlwaysOnSampler : public Sampler
* @return Always return Decision RECORD_AND_SAMPLE
*/
inline SamplingResult ShouldSample(
const trace_api::SpanContext & /*parent_context*/,
const trace_api::SpanContext &parent_context,
trace_api::TraceId /*trace_id*/,
nostd::string_view /*name*/,
trace_api::SpanKind /*span_kind*/,
const opentelemetry::common::KeyValueIterable & /*attributes*/,
const trace_api::SpanContextKeyValueIterable & /*links*/) noexcept override
{
return {Decision::RECORD_AND_SAMPLE, nullptr};
if (!parent_context.IsValid())
{
return {Decision::RECORD_AND_SAMPLE, nullptr, opentelemetry::trace::TraceState::GetDefault()};
}
else
{
return {Decision::RECORD_AND_SAMPLE, nullptr, parent_context.trace_state()};
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/trace/samplers/parent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ SamplingResult ParentBasedSampler::ShouldSample(
// If parent exists:
if (parent_context.IsSampled())
{
return {Decision::RECORD_AND_SAMPLE, nullptr};
return {Decision::RECORD_AND_SAMPLE, nullptr, parent_context.trace_state()};
}

return {Decision::DROP, nullptr};
return {Decision::DROP, nullptr, parent_context.trace_state()};
}

nostd::string_view ParentBasedSampler::GetDescription() const noexcept
Expand Down
6 changes: 4 additions & 2 deletions sdk/src/trace/span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
const trace_api::StartSpanOptions &options,
const trace_api::SpanContext &parent_span_context,
const opentelemetry::sdk::resource::Resource &resource,
const nostd::shared_ptr<opentelemetry::trace::TraceState> trace_state,
const bool sampled) noexcept
: tracer_{std::move(tracer)},
processor_{processor},
Expand Down Expand Up @@ -98,8 +99,9 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
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()));
trace_state ? trace_state
: 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
2 changes: 2 additions & 0 deletions sdk/src/trace/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Span final : public trace_api::Span
const trace_api::StartSpanOptions &options,
const trace_api::SpanContext &parent_span_context,
const opentelemetry::sdk::resource::Resource &resource,
const nostd::shared_ptr<opentelemetry::trace::TraceState> trace_state =
trace_api::TraceState::GetDefault(),
const bool sampled = false) noexcept;

~Span() override;
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/trace/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ nostd::shared_ptr<trace_api::Span> Tracer::StartSpan(
}
else
{
auto span = nostd::shared_ptr<trace_api::Span>{
new (std::nothrow) Span{this->shared_from_this(), processor_.load(), name, attributes,
links, options, parent, resource_, true}};
auto span = nostd::shared_ptr<trace_api::Span>{new (std::nothrow) Span{
this->shared_from_this(), processor_.load(), name, attributes, links, options, parent,
resource_, sampling_result.trace_state, true}};

// if the attributes is not nullptr, add attributes to the span.
if (sampling_result.attributes)
Expand Down
1 change: 1 addition & 0 deletions sdk/test/trace/always_off_sampler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ TEST(AlwaysOffSampler, ShouldSample)

ASSERT_EQ(Decision::DROP, sampling_result.decision);
ASSERT_EQ(nullptr, sampling_result.attributes);
ASSERT_EQ("", sampling_result.trace_state->ToHeader());
}

TEST(AlwaysOffSampler, GetDescription)
Expand Down
2 changes: 2 additions & 0 deletions sdk/test/trace/always_on_sampler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ TEST(AlwaysOnSampler, ShouldSample)

ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result.decision);
ASSERT_EQ(nullptr, sampling_result.attributes);
ASSERT_EQ("", sampling_result.trace_state->ToHeader());

// Test with a valid trace id and empty parent context
sampling_result = sampler.ShouldSample(
Expand All @@ -44,6 +45,7 @@ TEST(AlwaysOnSampler, ShouldSample)

ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result.decision);
ASSERT_EQ(nullptr, sampling_result.attributes);
ASSERT_EQ("", sampling_result.trace_state->ToHeader());
}

TEST(AlwaysOnSampler, GetDescription)
Expand Down
10 changes: 8 additions & 2 deletions sdk/test/trace/parent_sampler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ TEST(ParentBasedSampler, ShouldSample)

opentelemetry::common::KeyValueIterableView<M> view{m1};
trace_api::SpanContextKeyValueIterableView<L> links{l1};
trace_api::SpanContext parent_context_sampled(trace_id, span_id, trace_api::TraceFlags{1}, false);
auto trace_state = opentelemetry::trace::TraceState::FromHeader("congo=t61rcWkgMzE");
trace_api::SpanContext parent_context_sampled(trace_id, span_id, trace_api::TraceFlags{1}, false,
trace_state);
trace_api::SpanContext parent_context_nonsampled(trace_id, span_id, trace_api::TraceFlags{0},
false);
false, trace_state);

// Case 1: Parent doesn't exist. Return result of delegateSampler()
auto sampling_result = sampler_off.ShouldSample(trace_api::SpanContext::GetInvalid(), trace_id,
Expand All @@ -43,16 +45,20 @@ TEST(ParentBasedSampler, ShouldSample)

ASSERT_EQ(Decision::DROP, sampling_result.decision);
ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result2.decision);
ASSERT_EQ("", sampling_result.trace_state->ToHeader());
ASSERT_EQ("", sampling_result2.trace_state->ToHeader());

// Case 2: Parent exists and SampledFlag is true
auto sampling_result3 =
sampler_off.ShouldSample(parent_context_sampled, trace_id, "", span_kind, view, links);
ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result3.decision);
ASSERT_EQ("congo=t61rcWkgMzE", sampling_result3.trace_state->ToHeader());

// Case 3: Parent exists and SampledFlag is false
auto sampling_result4 =
sampler_on.ShouldSample(parent_context_nonsampled, trace_id, "", span_kind, view, links);
ASSERT_EQ(Decision::DROP, sampling_result4.decision);
ASSERT_EQ("congo=t61rcWkgMzE", sampling_result4.trace_state->ToHeader());
}

TEST(ParentBasedSampler, GetDescription)
Expand Down