Class: Datadog::Core::KnuthSampler Private
- Inherits:
-
Object
- Object
- Datadog::Core::KnuthSampler
- Defined in:
- lib/datadog/core/knuth_sampler.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Deterministic sampler using Knuth multiplicative hash algorithm.
This sampler provides consistent sampling decisions based on an input value, ensuring the same input always produces the same sampling decision for a given rate.
The algorithm multiplies the input by a large prime (Knuth factor), takes modulo to constrain to a fixed range, and compares against a threshold derived from the sample rate.
Constant Summary collapse
- UINT64_MAX =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Maximum unsigned 64-bit integer for uniform distribution across 64-bit input space.
(1 << 64) - 1
- UINT64_MODULO =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
1 << 64
- DEFAULT_KNUTH_FACTOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Golden ratio constant for optimal distribution.
11400714819323198485
Instance Attribute Summary collapse
- #rate ⇒ Object readonly private
Instance Method Summary collapse
-
#initialize(rate = 1.0, knuth_factor: DEFAULT_KNUTH_FACTOR) ⇒ KnuthSampler
constructor
private
A new instance of KnuthSampler.
-
#sample?(input) ⇒ Boolean
private
Determines if the given input should be sampled.
Constructor Details
#initialize(rate = 1.0, knuth_factor: DEFAULT_KNUTH_FACTOR) ⇒ KnuthSampler
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of KnuthSampler.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/datadog/core/knuth_sampler.rb', line 31 def initialize(rate = 1.0, knuth_factor: DEFAULT_KNUTH_FACTOR) @knuth_factor = knuth_factor rate = rate.to_f unless rate >= 0.0 && rate <= 1.0 Datadog.logger.warn("Sample rate #{rate} is not between 0.0 and 1.0, falling back to 1.0") rate = 1.0 end @rate = rate @threshold = @rate * UINT64_MAX end |
Instance Attribute Details
#rate ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
24 25 26 |
# File 'lib/datadog/core/knuth_sampler.rb', line 24 def rate @rate end |
Instance Method Details
#sample?(input) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Determines if the given input should be sampled.
This method is deterministic: the same input value always produces the same result for a given sample rate and configuration.
52 53 54 |
# File 'lib/datadog/core/knuth_sampler.rb', line 52 def sample?(input) ((input * @knuth_factor) % UINT64_MODULO) <= @threshold end |