-
Notifications
You must be signed in to change notification settings - Fork 581
[SDK] Implement the ProbabilitySampler #4135
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
Open
ayush-that
wants to merge
10
commits into
open-telemetry:main
Choose a base branch
from
ayush-that:probability-sampler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cd65df1
[SDK] Implement the ProbabilitySampler
ayush-that 710b267
reuse shared CalculateThreshold and GetRandomnessFromTraceId, strip s…
ayush-that 3b30ff1
drop unused nostd includes flagged by include-what-you-use
ayush-that 297388e
strip stale th on the invalid-ot sample path
ayush-that 210adcb
reuse OtelTraceState for ot value handling in the probability sampler
ayush-that 1815c46
[API] fix the trace state regex to comply with the w3c trace context …
dbarker 04ab5fe
Merge upstream/main into probability-sampler
ayush-that 06078a4
[DOC] add ProbabilitySampler to GettingStarted sampler example
ayush-that 247d05c
Merge branch 'main' into probability-sampler
ayush-that 3b0c1cb
Merge branch 'main' into probability-sampler
dbarker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
sdk/include/opentelemetry/sdk/trace/samplers/probability.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <stdint.h> | ||
| #include <string> | ||
|
|
||
| #include "opentelemetry/nostd/string_view.h" | ||
| #include "opentelemetry/sdk/trace/sampler.h" | ||
| #include "opentelemetry/trace/span_metadata.h" | ||
| #include "opentelemetry/trace/trace_id.h" | ||
| #include "opentelemetry/version.h" | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace sdk | ||
| { | ||
| namespace trace | ||
| { | ||
| /** | ||
| * The ProbabilitySampler computes and returns a decision based on the | ||
| * span's 56-bit randomness value and the configured ratio, following the | ||
| * consistent probability sampling specification. | ||
| */ | ||
| class ProbabilitySampler : public Sampler | ||
| { | ||
| public: | ||
| /** | ||
| * @param ratio a required value, 1.0 >= ratio >= 0.0. If the randomness | ||
| * value of the span is greater than or equal to the rejection threshold | ||
| * derived from the ratio, ShouldSample will return RECORD_AND_SAMPLE. | ||
| */ | ||
| explicit ProbabilitySampler(double ratio); | ||
|
|
||
| /** | ||
| * @return Returns either RECORD_AND_SAMPLE or DROP based on the comparison | ||
| * of the span's randomness value against the configured rejection | ||
| * threshold. The parent SampledFlag is ignored. | ||
| */ | ||
| SamplingResult ShouldSample( | ||
| const opentelemetry::trace::SpanContext &parent_context, | ||
| opentelemetry::trace::TraceId trace_id, | ||
| nostd::string_view /*name*/, | ||
| opentelemetry::trace::SpanKind /*span_kind*/, | ||
| const opentelemetry::common::KeyValueIterable & /*attributes*/, | ||
| const opentelemetry::trace::SpanContextKeyValueIterable & /*links*/) noexcept override; | ||
|
|
||
| /** | ||
| * @return Description MUST be ProbabilitySampler{0.000100} | ||
| */ | ||
| nostd::string_view GetDescription() const noexcept override; | ||
|
|
||
| private: | ||
| std::string description_; | ||
| const uint64_t threshold_; | ||
| }; | ||
| } // namespace trace | ||
| } // namespace sdk | ||
| OPENTELEMETRY_END_NAMESPACE |
31 changes: 31 additions & 0 deletions
31
sdk/include/opentelemetry/sdk/trace/samplers/probability_factory.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include "opentelemetry/sdk/trace/sampler.h" | ||
| #include "opentelemetry/version.h" | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace sdk | ||
| { | ||
| namespace trace | ||
| { | ||
|
|
||
| /** | ||
| * Factory class for ProbabilitySampler. | ||
| */ | ||
| class ProbabilitySamplerFactory | ||
| { | ||
| public: | ||
| /** | ||
| * Create a ProbabilitySampler. | ||
| */ | ||
| static std::unique_ptr<Sampler> Create(double ratio); | ||
| }; | ||
|
|
||
| } // namespace trace | ||
| } // namespace sdk | ||
| OPENTELEMETRY_END_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #include <atomic> | ||
| #include <cstdint> | ||
| #include <map> | ||
| #include <string> | ||
|
|
||
| #include "opentelemetry/nostd/shared_ptr.h" | ||
| #include "opentelemetry/nostd/string_view.h" | ||
| #include "opentelemetry/sdk/common/global_log_handler.h" | ||
| #include "opentelemetry/sdk/trace/sampler.h" | ||
| #include "opentelemetry/sdk/trace/samplers/probability.h" | ||
| #include "opentelemetry/trace/span_context.h" | ||
| #include "opentelemetry/trace/trace_flags.h" | ||
| #include "opentelemetry/trace/trace_id.h" | ||
| #include "opentelemetry/trace/trace_state.h" | ||
| #include "opentelemetry/version.h" | ||
|
|
||
| #include "ot_trace_state.h" | ||
|
|
||
| namespace trace_api = opentelemetry::trace; | ||
|
|
||
| namespace | ||
| { | ||
| // Clamps ratio to [0, 1]. NaN and negatives map to 0 (never-sample). | ||
| double ClampProbability(double ratio) noexcept | ||
| { | ||
| if (!(ratio > 0.0)) | ||
| return 0.0; | ||
| if (ratio > 1.0) | ||
| return 1.0; | ||
| return ratio; | ||
| } | ||
| } // namespace | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace sdk | ||
| { | ||
| namespace trace | ||
| { | ||
|
|
||
| ProbabilitySampler::ProbabilitySampler(double ratio) | ||
| : description_("ProbabilitySampler{" + std::to_string(ClampProbability(ratio)) + "}"), | ||
| threshold_(CalculateThreshold(ClampProbability(ratio))) | ||
| {} | ||
|
|
||
| SamplingResult ProbabilitySampler::ShouldSample( | ||
| 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 | ||
| { | ||
| auto parent_trace_state = parent_context.trace_state(); | ||
| if (!parent_trace_state) | ||
| { | ||
| parent_trace_state = trace_api::TraceState::GetDefault(); | ||
| } | ||
|
|
||
| std::string ot_value; | ||
| parent_trace_state->Get(kOtTraceStateKey, ot_value); | ||
| OtelTraceState ot_state = OtelTraceState::Parse(ot_value); | ||
|
|
||
| bool drop = threshold_ == kMaxThreshold; | ||
| if (!drop) | ||
| { | ||
| uint64_t randomness = 0; | ||
| if (ot_state.has_random_value) | ||
| { | ||
| // An explicit "rv" from an upstream Level 2 participant wins over the | ||
| // trace id, keeping the sampling decision consistent across the trace. | ||
| randomness = ot_state.random_value; | ||
| } | ||
| else | ||
| { | ||
| if (parent_context.IsValid() && !parent_context.trace_flags().IsRandom()) | ||
| { | ||
| static std::atomic<bool> warned{false}; | ||
| if (!warned.exchange(true)) | ||
| { | ||
| OTEL_INTERNAL_LOG_WARN( | ||
| "ProbabilitySampler presumes TraceID randomness, but the W3C random trace flag is " | ||
| "not set. Upgrade the caller to W3C Trace Context Level 2."); | ||
| } | ||
| } | ||
| randomness = GetRandomnessFromTraceId(trace_id); | ||
| } | ||
| drop = randomness < threshold_; | ||
| } | ||
|
|
||
| // Record the effective threshold when sampling; a dropped span carries no | ||
| // probability, so its inherited (now stale) "th" must be erased. The "rv" | ||
| // sub-key and any other "ot" sub-keys are preserved by OtelTraceState. | ||
| if (drop) | ||
| { | ||
| ot_state.has_threshold = false; | ||
| } | ||
| else | ||
| { | ||
| ot_state.has_threshold = true; | ||
| ot_state.threshold = threshold_; | ||
| } | ||
|
|
||
| std::string new_ot_value = ot_state.Serialize(); | ||
| auto trace_state = parent_trace_state->Delete(kOtTraceStateKey); | ||
| if (!new_ot_value.empty()) | ||
| { | ||
| trace_state = trace_state->Set(kOtTraceStateKey, new_ot_value); | ||
| } | ||
|
|
||
| return {drop ? Decision::DROP : Decision::RECORD_AND_SAMPLE, nullptr, trace_state}; | ||
| } | ||
|
|
||
| nostd::string_view ProbabilitySampler::GetDescription() const noexcept | ||
| { | ||
| return description_; | ||
| } | ||
| } // namespace trace | ||
| } // namespace sdk | ||
| OPENTELEMETRY_END_NAMESPACE | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #include "opentelemetry/sdk/trace/samplers/probability_factory.h" | ||
| #include "opentelemetry/sdk/trace/samplers/probability.h" | ||
| #include "opentelemetry/version.h" | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace sdk | ||
| { | ||
| namespace trace | ||
| { | ||
|
|
||
| std::unique_ptr<Sampler> ProbabilitySamplerFactory::Create(double ratio) | ||
| { | ||
| std::unique_ptr<Sampler> sampler(new ProbabilitySampler(ratio)); | ||
| return sampler; | ||
| } | ||
|
|
||
| } // namespace trace | ||
| } // namespace sdk | ||
| OPENTELEMETRY_END_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Non-blocking: probably sample on the warning instead of just showing the first warning.
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.
Kept it once-per-process to stay off the per-span hot path.