-
Notifications
You must be signed in to change notification settings - Fork 579
[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
base: main
Are you sure you want to change the base?
Changes from 7 commits
f0d2754
71a91df
8fc8f8c
6abbf4b
a9d8d7e
9d016cf
c9f3672
28e7580
9fb789d
6f3250b
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,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 |
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #include <stddef.h> | ||
| #include <atomic> | ||
| #include <cstdint> | ||
| #include <map> | ||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| #include "opentelemetry/nostd/function_ref.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 nostd = opentelemetry::nostd; | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
| bool ParseHex56(nostd::string_view hex, uint64_t *value) noexcept | ||
| { | ||
| if (hex.size() != 14) | ||
| return false; | ||
| uint64_t result = 0; | ||
| for (char c : hex) | ||
| { | ||
| result <<= 4; | ||
| if (c >= '0' && c <= '9') | ||
| result |= static_cast<uint64_t>(c - '0'); | ||
| else if (c >= 'a' && c <= 'f') | ||
| result |= static_cast<uint64_t>(c - 'a' + 10); | ||
| else | ||
| return false; | ||
| } | ||
| *value = result; | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Encodes a 56-bit threshold as lowercase hex with trailing zeros removed, | ||
| * e.g. 2^55 becomes "8" and 0 becomes "0". | ||
| */ | ||
| std::string EncodeThreshold(uint64_t threshold) | ||
| { | ||
| if (threshold == 0) | ||
| return "0"; | ||
| static const char kHex[] = "0123456789abcdef"; | ||
| std::string hex(14, '0'); | ||
| for (int i = 13; i >= 0; --i) | ||
| { | ||
| hex[i] = kHex[threshold & 0xf]; | ||
| threshold >>= 4; | ||
| } | ||
| hex.erase(hex.find_last_not_of('0') + 1); | ||
| return hex; | ||
| } | ||
|
|
||
| // Returns true when the ot value carries a valid rv sub-key. | ||
| bool ExplicitRandomness(nostd::string_view ot_value, uint64_t *randomness) noexcept | ||
| { | ||
| while (!ot_value.empty()) | ||
| { | ||
| size_t end = ot_value.find(';'); | ||
| nostd::string_view sub_key = ot_value.substr(0, end); | ||
| ot_value = end == nostd::string_view::npos ? "" : ot_value.substr(end + 1); | ||
|
|
||
| if (sub_key.substr(0, 3) == "rv:") | ||
| return ParseHex56(sub_key.substr(3), randomness); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| // Sub-keys that are empty, lack a key:value separator, or duplicate th are dropped. | ||
| std::string SetThresholdSubKey(nostd::string_view ot_value, const std::string &threshold) | ||
| { | ||
| std::string result; | ||
| bool replaced = false; | ||
| while (!ot_value.empty()) | ||
| { | ||
| size_t end = ot_value.find(';'); | ||
| nostd::string_view sub_key = ot_value.substr(0, end); | ||
| ot_value = end == nostd::string_view::npos ? "" : ot_value.substr(end + 1); | ||
|
|
||
| if (sub_key.empty() || sub_key.find(':') == nostd::string_view::npos) | ||
| continue; | ||
| if (sub_key.substr(0, 3) == "th:") | ||
| { | ||
| if (replaced) | ||
| continue; | ||
| replaced = true; | ||
| if (!result.empty()) | ||
| result += ';'; | ||
| result += "th:" + threshold; | ||
| continue; | ||
| } | ||
| if (!result.empty()) | ||
| result += ';'; | ||
| result.append(sub_key.data(), sub_key.size()); | ||
| } | ||
| if (!replaced) | ||
| { | ||
| if (!result.empty()) | ||
| result += ';'; | ||
| result += "th:" + threshold; | ||
| } | ||
| return result; | ||
| } | ||
| } // 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(); | ||
| std::string ot_value; | ||
| bool has_ot = parent_trace_state->Get(kOtTraceStateKey, ot_value); | ||
|
|
||
| bool drop = threshold_ == kMaxThreshold; | ||
| if (!drop) | ||
| { | ||
| uint64_t randomness = 0; | ||
| if (!ExplicitRandomness(ot_value, &randomness)) | ||
| { | ||
| 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_; | ||
| } | ||
|
|
||
| if (drop) | ||
| { | ||
| OtelTraceState ot_state = OtelTraceState::Parse(ot_value); | ||
| ot_state.has_threshold = false; | ||
| std::string dropped_ot = ot_state.Serialize(); | ||
| auto trace_state = parent_trace_state->Delete(kOtTraceStateKey); | ||
| if (!dropped_ot.empty()) | ||
| trace_state = trace_state->Set(kOtTraceStateKey, dropped_ot); | ||
| return {Decision::DROP, nullptr, trace_state}; | ||
| } | ||
|
|
||
| std::string new_ot_value = SetThresholdSubKey(ot_value, EncodeThreshold(threshold_)); | ||
| if (!trace_api::TraceState::IsValidValue(new_ot_value)) | ||
|
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. Same stale-
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. Yes. Reused the drop-path strip on the invalid-ot branch. The full branch is !has_ot, so nothing stale there. Added a test. |
||
| return {Decision::RECORD_AND_SAMPLE, nullptr, {}}; | ||
|
|
||
| if (!has_ot) | ||
| { | ||
| size_t entry_count = 0; | ||
| parent_trace_state->GetAllEntries([&entry_count](nostd::string_view, nostd::string_view) { | ||
| ++entry_count; | ||
| return true; | ||
| }); | ||
| if (entry_count >= trace_api::TraceState::kMaxKeyValuePairs) | ||
| return {Decision::RECORD_AND_SAMPLE, nullptr, {}}; | ||
| } | ||
|
|
||
| auto trace_state = | ||
| has_ot ? parent_trace_state->Delete(kOtTraceStateKey)->Set(kOtTraceStateKey, new_ot_value) | ||
| : parent_trace_state->Set(kOtTraceStateKey, new_ot_value); | ||
|
|
||
| return {Decision::RECORD_AND_SAMPLE, nullptr, trace_state}; | ||
| } | ||
|
|
||
| nostd::string_view ProbabilitySampler::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,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 |
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.