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



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
# File 'lib/ddtrace/sampling/rule_sampler.rb', line 31

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
                       # Add to the end of the rule list a rule always matches any span
                       @rules << SimpleRule.new(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.



23
24
25
# File 'lib/ddtrace/sampling/rule_sampler.rb', line 23

def default_sampler
  @default_sampler
end

#rate_limiterObject (readonly)

Returns the value of attribute rate_limiter.



23
24
25
# File 'lib/ddtrace/sampling/rule_sampler.rb', line 23

def rate_limiter
  @rate_limiter
end

#rulesObject (readonly)

Returns the value of attribute rules.



23
24
25
# File 'lib/ddtrace/sampling/rule_sampler.rb', line 23

def rules
  @rules
end

Instance Method Details

#sample!(span) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ddtrace/sampling/rule_sampler.rb', line 67

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)


63
64
65
# File 'lib/ddtrace/sampling/rule_sampler.rb', line 63

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

#update(*args) ⇒ Object



84
85
86
87
88
# File 'lib/ddtrace/sampling/rule_sampler.rb', line 84

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

  @default_sampler.update(*args)
end