Class: Datadog::Tracing::Sampling::RateSampler

Inherits:
Sampler
  • Object
show all
Defined in:
lib/datadog/tracing/sampling/rate_sampler.rb

Overview

RateSampler is based on a sample rate.

Constant Summary collapse

KNUTH_FACTOR =
1111111111111111111

Instance Method Summary collapse

Constructor Details

#initialize(sample_rate = 1.0, decision: nil) ⇒ RateSampler

Initialize a Datadog::Tracing::Sampling::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.

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 Datadog::Tracing::Sampling::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: “`



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/datadog/tracing/sampling/rate_sampler.rb', line 29

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')
    sample_rate = 1.0
  end

  self.sample_rate = sample_rate

  @decision = decision
end

Instance Method Details

#sample!(trace) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/datadog/tracing/sampling/rate_sampler.rb', line 55

def sample!(trace)
  sampled = trace.sampled = sample?(trace)

  return false unless sampled

  trace.sample_rate = @sample_rate
  trace.set_tag(Tracing::Metadata::Ext::Distributed::TAG_DECISION_MAKER, @decision) if @decision

  true
end

#sample?(trace) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/datadog/tracing/sampling/rate_sampler.rb', line 51

def sample?(trace)
  ((trace.id * KNUTH_FACTOR) % Tracing::Utils::EXTERNAL_MAX_ID) <= @sampling_id_threshold
end

#sample_rate(*_) ⇒ Object



42
43
44
# File 'lib/datadog/tracing/sampling/rate_sampler.rb', line 42

def sample_rate(*_)
  @sample_rate
end

#sample_rate=(sample_rate) ⇒ Object



46
47
48
49
# File 'lib/datadog/tracing/sampling/rate_sampler.rb', line 46

def sample_rate=(sample_rate)
  @sample_rate = sample_rate
  @sampling_id_threshold = sample_rate * Tracing::Utils::EXTERNAL_MAX_ID
end