Class: Datadog::RateSampler
Overview
\RateSampler is based on a sample rate.
Constant Summary collapse
- KNUTH_FACTOR =
1111111111111111111
- SAMPLE_RATE_METRIC_KEY =
'_sample_rate'.freeze
Instance Method Summary collapse
-
#initialize(sample_rate = 1.0) ⇒ RateSampler
constructor
Initialize a \RateSampler.
- #sample!(span) ⇒ Object
- #sample?(span) ⇒ Boolean
- #sample_rate(*_) ⇒ Object
- #sample_rate=(sample_rate) ⇒ Object
Constructor Details
#initialize(sample_rate = 1.0) ⇒ RateSampler
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.
50 51 52 53 54 55 56 57 |
# File 'lib/ddtrace/sampler.rb', line 50 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 |
Instance Method Details
#sample!(span) ⇒ Object
72 73 74 75 76 |
# File 'lib/ddtrace/sampler.rb', line 72 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) ⇒ Boolean
68 69 70 |
# File 'lib/ddtrace/sampler.rb', line 68 def sample?(span) ((span.trace_id * KNUTH_FACTOR) % Datadog::Span::EXTERNAL_MAX_ID) <= @sampling_id_threshold end |
#sample_rate(*_) ⇒ Object
59 60 61 |
# File 'lib/ddtrace/sampler.rb', line 59 def sample_rate(*_) @sample_rate end |
#sample_rate=(sample_rate) ⇒ Object
63 64 65 66 |
# File 'lib/ddtrace/sampler.rb', line 63 def sample_rate=(sample_rate) @sample_rate = sample_rate @sampling_id_threshold = sample_rate * Span::EXTERNAL_MAX_ID end |