class Datadog::RateSampler
RateSampler is based on a sample rate.
Constants
- KNUTH_FACTOR
- SAMPLE_RATE_METRIC_KEY
Public Class Methods
new(sample_rate = 1.0)
click to toggle source
Initialize a RateSampler. 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.
# File lib/ddtrace/sampler.rb, line 49 def initialize(sample_rate = 1.0) unless sample_rate > 0.0 && sample_rate <= 1.0 Datadog.logger.error('sample rate is not between 0 and 1, disabling the sampler') sample_rate = 1.0 end self.sample_rate = sample_rate end
Public Instance Methods
sample!(span)
click to toggle source
# File lib/ddtrace/sampler.rb, line 71 def sample!(span) (span.sampled = sample?(span)).tap do |sampled| span.set_metric(SAMPLE_RATE_METRIC_KEY, @sample_rate) if sampled end end
sample?(span)
click to toggle source
# File lib/ddtrace/sampler.rb, line 67 def sample?(span) ((span.trace_id * KNUTH_FACTOR) % Datadog::Span::MAX_ID) <= @sampling_id_threshold end
sample_rate(*_)
click to toggle source
# File lib/ddtrace/sampler.rb, line 58 def sample_rate(*_) @sample_rate end
sample_rate=(sample_rate)
click to toggle source
# File lib/ddtrace/sampler.rb, line 62 def sample_rate=(sample_rate) @sample_rate = sample_rate @sampling_id_threshold = sample_rate * Span::MAX_ID end