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

Allow RateSampler with a sampling rate of zero #3295

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 4 additions & 15 deletions lib/datadog/tracing/sampling/rate_sampler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,13 @@ class RateSampler < Sampler
# This sampler keeps a random subset of the traces. Its main purpose is to
# reduce the instrumentation footprint.
#
# * +sample_rate+: the sample rate as a {Float} between 0.0 and 1.0. 0.0
# means that no trace will be sampled; 1.0 means that all traces will be
# sampled.
#
# DEV-2.0: Allow for `sample_rate` zero (drop all) to be allowed. This eases
# DEV-2.0: usage for all internal users of the {RateSampler} class: both
# DEV-2.0: RuleSampler and Single Span Sampling leverage the RateSampler, but want
# DEV-2.0: `sample_rate` zero to mean "drop all". They work around this by hard-
# DEV-2.0: setting the `sample_rate` to zero like so:
# DEV-2.0: ```
# DEV-2.0: sampler = RateSampler.new
# DEV-2.0: sampler.sample_rate = sample_rate
# DEV-2.0: ```
# @param sample_rate [Numeric] the sample rate between 0.0 and 1.0, inclusive.
# 0.0 means that no trace will be sampled; 1.0 means that all traces will be sampled.
def initialize(sample_rate = 1.0, decision: nil)
super()

unless sample_rate > 0.0 && sample_rate <= 1.0
Datadog.logger.error('sample rate is not between 0 and 1, disabling the sampler')
unless sample_rate >= 0.0 && sample_rate <= 1.0
Datadog.logger.error('sample rate is not between 0 and 1, falling back to 1')
sample_rate = 1.0
end

Expand Down
13 changes: 1 addition & 12 deletions lib/datadog/tracing/sampling/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,7 @@ class SimpleRule < Rule
# defaults to always match
# @param sample_rate [Float] Sampling rate between +[0,1]+
def initialize(name: SimpleMatcher::MATCH_ALL, service: SimpleMatcher::MATCH_ALL, sample_rate: 1.0)
# We want to allow 0.0 to drop all traces, but {Datadog::Tracing::Sampling::RateSampler}
# considers 0.0 an invalid rate and falls back to 100% sampling.
#
# We address that here by not setting the rate in the constructor,
# but using the setter method.
#
# We don't want to make this change directly to {Datadog::Tracing::Sampling::RateSampler}
# because it breaks its current contract to existing users.
sampler = RateSampler.new
sampler.sample_rate = sample_rate

super(SimpleMatcher.new(name: name, service: service), sampler)
super(SimpleMatcher.new(name: name, service: service), RateSampler.new(sample_rate))
end
end
end
Expand Down
7 changes: 1 addition & 6 deletions lib/datadog/tracing/sampling/span/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ def initialize(
@sample_rate = sample_rate
@rate_limit = rate_limit

@sampler = Sampling::RateSampler.new
# Set the sample_rate outside of the initializer to allow for
# zero to be a "drop all".
# The RateSampler initializer enforces non-zero, falling back to 100% sampling
# if zero is provided.
@sampler.sample_rate = sample_rate
@sampler = Sampling::RateSampler.new(sample_rate)
@rate_limiter = Sampling::TokenBucket.new(rate_limit)
end

Expand Down
6 changes: 6 additions & 0 deletions spec/datadog/tracing/sampling/rate_sampler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
context 'that is 0' do
let(:sample_rate) { 0.0 }

it_behaves_like 'sampler with sample rate', 0.0
end

context 'that is 1' do
let(:sample_rate) { 1.0 }

it_behaves_like 'sampler with sample rate', 1.0
end

Expand Down
Loading