Class: Datadog::Tracing::Sampling::Span::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/tracing/sampling/span/rule.rb

Overview

Span sampling rule that applies a sampling rate if the span matches the provided Matcher. Additionally, a rate limiter is also applied.

If a span does not conform to the matcher, no changes are made.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matcher, sample_rate: Span::Ext::DEFAULT_SAMPLE_RATE, rate_limit: Span::Ext::DEFAULT_MAX_PER_SECOND) ⇒ Rule

Creates a new span sampling rule.

Parameters:

  • matcher (Sampling::Span::Matcher)

    whether this rule applies to a specific span

  • sample_rate (Float) (defaults to: Span::Ext::DEFAULT_SAMPLE_RATE)

    span sampling ratio, between 0.0 (0%) and 1.0 (100%).

  • rate_limit (Numeric) (defaults to: Span::Ext::DEFAULT_MAX_PER_SECOND)

    maximum number of spans sampled per second. Negative numbers mean unlimited spans.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/datadog/tracing/sampling/span/rule.rb', line 22

def initialize(
  matcher,
  sample_rate: Span::Ext::DEFAULT_SAMPLE_RATE,
  rate_limit: Span::Ext::DEFAULT_MAX_PER_SECOND
)
  @matcher = matcher
  @sample_rate = sample_rate
  @rate_limit = rate_limit

  @sampler = Sampling::RateSampler.new(sample_rate)
  @rate_limiter = Core::TokenBucket.new(rate_limit)
end

Instance Attribute Details

#matcherObject (readonly)

Returns the value of attribute matcher.



15
16
17
# File 'lib/datadog/tracing/sampling/span/rule.rb', line 15

def matcher
  @matcher
end

#rate_limitObject (readonly)

Returns the value of attribute rate_limit.



15
16
17
# File 'lib/datadog/tracing/sampling/span/rule.rb', line 15

def rate_limit
  @rate_limit
end

#sample_rateObject (readonly)

Returns the value of attribute sample_rate.



15
16
17
# File 'lib/datadog/tracing/sampling/span/rule.rb', line 15

def sample_rate
  @sample_rate
end

Instance Method Details

#==(other) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/datadog/tracing/sampling/span/rule.rb', line 66

def ==(other)
  return super unless other.is_a?(Rule)

  matcher == other.matcher &&
    sample_rate == other.sample_rate &&
    rate_limit == other.rate_limit
end

#sample!(trace_op, span_op) ⇒ :kept, ...

This method should only be invoked for spans that are part of a trace that has been dropped by trace-level sampling. Invoking it for other spans will cause incorrect sampling metrics to be reported by the Datadog App.

Returns true if the provided span is sampled. If the span is dropped due to sampling rate or rate limiting, it returns false.

Returns nil if the span did not meet the matching criteria by the provided matcher.

This method modifies the span if it matches the provided matcher.

Parameters:

Returns:

  • (:kept, :rejected)

    should this span be sampled?

  • (:not_matched)

    span did not satisfy the matcher, no changes are made to the span



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

def sample!(trace_op, span_op)
  return :not_matched unless @matcher.match?(span_op)

  if @rate_limiter.allow? && @sampler.sample!(trace_op)
    span_op.set_metric(Span::Ext::TAG_MECHANISM, Sampling::Ext::Mechanism::SPAN_SAMPLING_RATE)
    span_op.set_metric(Span::Ext::TAG_RULE_RATE, @sample_rate)
    span_op.set_metric(Span::Ext::TAG_MAX_PER_SECOND, @rate_limit)
    :kept
  else
    :rejected
  end
end