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.

Parameters:

  • sample_rate (Numeric) (defaults to: 1.0)

    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.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/datadog/tracing/sampling/rate_sampler.rb', line 19

def initialize(sample_rate = 1.0, decision: nil)
  super()

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

  self.sample_rate = sample_rate

  @decision = decision
end

Instance Method Details

#sample!(trace) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/datadog/tracing/sampling/rate_sampler.rb', line 45

def sample!(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)


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

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

#sample_rate(*_) ⇒ Object



32
33
34
# File 'lib/datadog/tracing/sampling/rate_sampler.rb', line 32

def sample_rate(*_)
  @sample_rate
end

#sample_rate=(sample_rate) ⇒ Object



36
37
38
39
# File 'lib/datadog/tracing/sampling/rate_sampler.rb', line 36

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