-
Notifications
You must be signed in to change notification settings - Fork 547
Parent-or-else Sampler #142
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
Changes from all commits
4090669
7843d59
d2cdef3
be589b9
a251523
da9c643
a91663c
822144a
1eef198
fc69477
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| #pragma once | ||
|
|
||
| #include "opentelemetry/sdk/common/atomic_shared_ptr.h" | ||
| #include "opentelemetry/sdk/trace/sampler.h" | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace sdk | ||
| { | ||
| namespace trace | ||
| { | ||
| namespace trace_api = opentelemetry::trace; | ||
|
|
||
| /** | ||
| * A placeholder class that stores the states of a span. | ||
| * Will be replaced by the real SpanContext class once it is implemented. | ||
| */ | ||
| class Sampler::SpanContext | ||
| { | ||
| public: | ||
| inline explicit SpanContext(bool is_recording, bool sampled_flag) | ||
| : is_recording(is_recording), sampled_flag(sampled_flag) | ||
| {} | ||
|
|
||
| bool is_recording; | ||
| bool sampled_flag; | ||
| }; | ||
|
|
||
| /** | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: probably add an empty line here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| * The parent or else sampler is a composite sampler. ParentOrElse(delegateSampler) either respects | ||
| * the parent span's sampling decision or delegates to delegateSampler for root spans. | ||
| */ | ||
| class ParentOrElseSampler : public Sampler | ||
| { | ||
| public: | ||
| explicit ParentOrElseSampler(std::shared_ptr<Sampler> delegate_sampler) noexcept; | ||
| /** The decision either respects the parent span's sampling decision or delegates to | ||
| * delegateSampler for root spans | ||
| * @return Returns NOT_RECORD always | ||
| */ | ||
| SamplingResult ShouldSample(const SpanContext * parent_context, | ||
| trace_api::TraceId trace_id, | ||
| nostd::string_view name, | ||
| trace_api::SpanKind span_kind, | ||
| const trace_api::KeyValueIterable & attributes) noexcept override; | ||
|
|
||
| /** | ||
| * @return Description MUST be ParentOrElse{delegate_sampler_.getDescription()} | ||
| */ | ||
| std::string GetDescription() const noexcept override; | ||
|
|
||
| private: | ||
| const std::shared_ptr<Sampler> delegate_sampler_; | ||
| const std::string description_; | ||
| }; | ||
| } // namespace trace | ||
| } // namespace sdk | ||
| OPENTELEMETRY_END_NAMESPACE | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| add_library(opentelemetry_trace tracer_provider.cc tracer.cc span.cc) | ||
| add_library(opentelemetry_trace tracer_provider.cc tracer.cc span.cc samplers/parent_or_else.cc) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #include "opentelemetry/sdk/trace/samplers/parent_or_else.h" | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace sdk | ||
| { | ||
| namespace trace | ||
| { | ||
| ParentOrElseSampler::ParentOrElseSampler(std::shared_ptr<Sampler> delegate_sampler) noexcept | ||
| : delegate_sampler_(delegate_sampler), | ||
| description_("ParentOrElse{" + delegate_sampler->GetDescription() + "}") | ||
| {} | ||
|
|
||
| SamplingResult ParentOrElseSampler::ShouldSample( | ||
| const SpanContext *parent_context, | ||
| trace_api::TraceId trace_id, | ||
| nostd::string_view name, | ||
| trace_api::SpanKind span_kind, | ||
| const trace_api::KeyValueIterable &attributes) noexcept | ||
| { | ||
| if (parent_context == nullptr) | ||
| { | ||
| // If no parent (root span) exists returns the result of the delegateSampler | ||
| return delegate_sampler_->ShouldSample(parent_context, trace_id, name, span_kind, attributes); | ||
| } | ||
|
|
||
| // If parent exists: | ||
| if (parent_context->sampled_flag) | ||
| { | ||
| return {Decision::RECORD_AND_SAMPLE, nullptr}; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could flatten this out, to increase readability: if (parent_context == nullptr)
{
return delegate_sampler_->ShouldSample(parent_context, trace_id, name, span_kind, attributes);
}
if (parent_context->sampled_flag)
{
return {Decision::RECORD_AND_SAMPLE, nullptr};
}
return {Decision::NOT_RECORD, nullptr}; |
||
|
|
||
| return {Decision::NOT_RECORD, nullptr}; | ||
| } | ||
|
|
||
| std::string ParentOrElseSampler::GetDescription() const noexcept | ||
| { | ||
| return description_; | ||
| } | ||
| } // namespace trace | ||
| } // namespace sdk | ||
| OPENTELEMETRY_END_NAMESPACE | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| #include <gtest/gtest.h> | ||
| #include <memory> | ||
| #include "opentelemetry/sdk/trace/samplers/always_off.h" | ||
| #include "opentelemetry/sdk/trace/samplers/always_on.h" | ||
| #include "opentelemetry/sdk/trace/samplers/parent_or_else.h" | ||
|
|
||
| using opentelemetry::sdk::trace::AlwaysOffSampler; | ||
| using opentelemetry::sdk::trace::AlwaysOnSampler; | ||
| using opentelemetry::sdk::trace::Decision; | ||
| using opentelemetry::sdk::trace::ParentOrElseSampler; | ||
|
|
||
| TEST(ParentOrElseSampler, ShouldSample) | ||
| { | ||
| ParentOrElseSampler sampler_off(std::make_shared<AlwaysOffSampler>()); | ||
| ParentOrElseSampler sampler_on(std::make_shared<AlwaysOnSampler>()); | ||
|
|
||
| // Set up parameters | ||
| opentelemetry::trace::TraceId trace_id; | ||
| opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; | ||
| using M = std::map<std::string, int>; | ||
| M m1 = {{}}; | ||
| opentelemetry::trace::KeyValueIterableView<M> view{m1}; | ||
| opentelemetry::sdk::trace::Sampler::SpanContext parent_context_sampled(true, true); | ||
| opentelemetry::sdk::trace::Sampler::SpanContext parent_context_nonsampled(true, false); | ||
|
|
||
| // Case 1: Parent doesn't exist. Return result of delegateSampler() | ||
|
ziqizh marked this conversation as resolved.
|
||
| auto sampling_result = sampler_off.ShouldSample(nullptr, trace_id, "", span_kind, view); | ||
| auto sampling_result2 = sampler_on.ShouldSample(nullptr, trace_id, "", span_kind, view); | ||
|
|
||
| ASSERT_EQ(Decision::NOT_RECORD, sampling_result.decision); | ||
| ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result2.decision); | ||
|
|
||
| // Case 2: Parent exists and SampledFlag is true | ||
| auto sampling_result3 = | ||
| sampler_off.ShouldSample(&parent_context_sampled, trace_id, "", span_kind, view); | ||
| ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result3.decision); | ||
|
|
||
| // Case 3: Parent exists and SampledFlag is false | ||
| auto sampling_result4 = | ||
| sampler_on.ShouldSample(&parent_context_nonsampled, trace_id, "", span_kind, view); | ||
| ASSERT_EQ(Decision::NOT_RECORD, sampling_result4.decision); | ||
| } | ||
|
|
||
| TEST(ParentOrElseSampler, GetDescription) | ||
| { | ||
| ParentOrElseSampler sampler(std::make_shared<AlwaysOffSampler>()); | ||
| ASSERT_EQ("ParentOrElse{AlwaysOffSampler}", sampler.GetDescription()); | ||
| ParentOrElseSampler sampler2(std::make_shared<AlwaysOnSampler>()); | ||
| ASSERT_EQ("ParentOrElse{AlwaysOnSampler}", sampler2.GetDescription()); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/open-telemetry/opentelemetry-cpp/pull/136/files#r451701975