Skip to content
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

fix: Consistent probability sampler init #1447

Merged
merged 4 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def initialize(probability)
end

@p_floor = (Math.frexp(probability)[1] - 1).abs
@p_ceil = @p_floor + 1
@p_ceil = @p_floor - 1
floor = Math.ldexp(1.0, -@p_floor)
ceil = Math.ldexp(1.0, -@p_ceil)
@p_ceil_probability = (probability - floor) / (ceil - floor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,37 @@

# TODO: statistical tests
end

describe '#initialize' do
# Check that the internal state is initialized correctly. We cache the
# floor and ceil of the negative power-of-two of the provided probability,
# and the probability of the ceil. It should be possible to calculate the
# original probability from these.
#
# Note that these are negative power-of-two exponent values, so the ceiling
# is the smaller value.

it 'initializes correctly for power-of-two probabilities' do
p_values = 62.downto(0)
probabilities = p_values.map { |i| 2**-i }
p_values.zip(probabilities).each do |p, probability|
sampler = OpenTelemetry::SDK::Trace::Samplers::ConsistentProbabilityBased.new(probability)
_(sampler.instance_variable_get(:@p_floor)).must_equal(p)
_(sampler.instance_variable_get(:@p_ceil)).must_equal(p-1)
_(sampler.instance_variable_get(:@p_ceil_probability)).must_equal(0)
end
end

it 'initializes correctly for 0.1' do
sampler = OpenTelemetry::SDK::Trace::Samplers::ConsistentProbabilityBased.new(0.1)
_(sampler.instance_variable_get(:@p_floor)).must_equal(4) # 2**-4 = 0.0625
_(sampler.instance_variable_get(:@p_ceil)).must_equal(3) # 2**-3 = 0.125
_(sampler.instance_variable_get(:@p_ceil_probability)).must_be_within_epsilon(0.6) # 0.0375 / 0.0625
ceil_prob = sampler.instance_variable_get(:@p_ceil_probability)
floor_prob = 1 - ceil_prob
p_floor = sampler.instance_variable_get(:@p_floor)
p_ceil = sampler.instance_variable_get(:@p_ceil)
_((2**-p_ceil * ceil_prob) + (2**-p_floor * floor_prob)).must_be_within_epsilon(0.1)
fbogsany marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
Loading