Class: Datadog::Sampling::RuleSampler

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ddtrace/sampling/rule_sampler.rb

Overview

Span Datadog::Sampler that applies a set of Rules to decide on sampling outcome. Then, a rate limiter is applied.

If a span does not conform to any rules, a default sampling strategy is applied.

Constant Summary collapse

AGENT_RATE_METRIC_KEY =
'_dd.agent_psr'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules = [], rate_limit: Datadog.configuration.sampling.rate_limit, rate_limiter: nil, default_sample_rate: Datadog.configuration.sampling.default_rate, default_sampler: nil) ⇒ RuleSampler

Returns a new instance of RuleSampler.

Parameters:

  • rules (Array<Rule>) (defaults to: [])

    ordered list of rules to be applied to a span

  • rate_limit (Float) (defaults to: Datadog.configuration.sampling.rate_limit)

    number of traces per second, defaults to 100

  • rate_limiter (RateLimiter) (defaults to: nil)

    limiter applied after rule matching

  • default_sample_rate (Float) (defaults to: Datadog.configuration.sampling.default_rate)

    fallback sample rate when no rules apply to a span, between [0,1], defaults to 1

  • default_sampler (Sample) (defaults to: nil)

    fallback strategy when no rules apply to a span



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ddtrace/sampling/rule_sampler.rb', line 29

def initialize(rules = [],
               rate_limit: Datadog.configuration.sampling.rate_limit,
               rate_limiter: nil,
               default_sample_rate: Datadog.configuration.sampling.default_rate,
               default_sampler: nil)

  @rules = rules

  @rate_limiter = if rate_limiter
                    rate_limiter
                  elsif rate_limit
                    Datadog::Sampling::TokenBucket.new(rate_limit)
                  else
                    Datadog::Sampling::UnlimitedLimiter.new
                  end

  @default_sampler = if default_sampler
                       default_sampler
                     elsif default_sample_rate
                       # We want to allow 0.0 to drop all traces, but \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 \RateSampler
                       # because it breaks its current contract to existing users.
                       Datadog::RateSampler.new.tap { |s| s.sample_rate = default_sample_rate }
                     else
                       RateByServiceSampler.new(1.0, env: -> { Datadog.tracer.tags[:env] })
                     end
end

Instance Attribute Details

#default_samplerObject (readonly)

Returns the value of attribute default_sampler.



21
22
23
# File 'lib/ddtrace/sampling/rule_sampler.rb', line 21

def default_sampler
  @default_sampler
end

#rate_limiterObject (readonly)

Returns the value of attribute rate_limiter.



21
22
23
# File 'lib/ddtrace/sampling/rule_sampler.rb', line 21

def rate_limiter
  @rate_limiter
end

#rulesObject (readonly)

Returns the value of attribute rules.



21
22
23
# File 'lib/ddtrace/sampling/rule_sampler.rb', line 21

def rules
  @rules
end

Instance Method Details

#sample!(span) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ddtrace/sampling/rule_sampler.rb', line 72

def sample!(span)
  sampled = sample_span(span) do |s|
    @default_sampler.sample!(s).tap do
      # We want to make sure the span is tagged with the agent-derived
      # service rate. Retrieve this from the rate by service sampler.
      # Only do this if it was set by a RateByServiceSampler.
      if @default_sampler.is_a?(RateByServiceSampler)
        s.set_metric(AGENT_RATE_METRIC_KEY, @default_sampler.sample_rate(span))
      end
    end
  end

  sampled.tap do
    span.sampled = sampled
  end
end

#sample?(_span) ⇒ Boolean

/RuleSampler’s components (it’s rate limiter, for example) are not be guaranteed to be size-effect free. It is not possible to guarantee that a call to #sample? will return the same result as a successive call to #sample! with the same span.

Use #sample! instead

Returns:

  • (Boolean)


68
69
70
# File 'lib/ddtrace/sampling/rule_sampler.rb', line 68

def sample?(_span)
  raise 'RuleSampler cannot be evaluated without side-effects'
end

#update(*args) ⇒ Object



89
90
91
92
# File 'lib/ddtrace/sampling/rule_sampler.rb', line 89

def update(*args)
  return false unless @default_sampler.respond_to?(:update)
  @default_sampler.update(*args)
end