Class: Datadog::Tracing::Sampling::Span::Sampler

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

Overview

Applies Single Span Sampling rules to spans. When matching the configured rules, a span is ensured to be processed Datadog App. In other words, a single sampled span will never be dropped by the tracer or Datadog agent.

All spans in a trace are subject to the single sampling rules, if any rules are configured.

Single Span Sampling is distinct from trace-level sampling: Single Span Sampling can ensure a span is kept, even if its enclosing trace is rejected by trace-level sampling.

This class only applies operations to spans that are part of traces that was rejected by trace sampling. A trace is rejected if either of the following conditions is true:

  • The priority sampling for a trace is set to either USER_REJECT or AUTO_REJECT.

  • The trace was rejected by internal sampling, thus never flushed.

Single-sampled spans are tagged and the tracer ensures they will reach the Datadog App, regardless of their enclosing trace sampling decision.

Single Span Sampling does not inspect spans that are part of a trace that has been accepted by trace-level sampling rules: all spans from such trace are guaranteed to reach the Datadog App.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules = []) ⇒ Sampler

Receives sampling rules to apply to individual spans.

Parameters:



37
38
39
# File 'lib/datadog/tracing/sampling/span/sampler.rb', line 37

def initialize(rules = [])
  @rules = rules
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



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

def rules
  @rules
end

Instance Method Details

#sample!(trace_op, span_op) ⇒ void

This method returns an undefined value.

Applies Single Span Sampling rules to the span if the trace has been rejected.

The trace can be outright rejected, and never reach the transport, or be set as rejected by priority sampling. In both cases, the trace is considered rejected for Single Span Sampling purposes.

If multiple rules match, only the first one is applied.

Parameters:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/datadog/tracing/sampling/span/sampler.rb', line 52

def sample!(trace_op, span_op)
  return if trace_op.sampled? && trace_op.priority_sampled?

  # Applies the first matching rule
  @rules.each do |rule|
    decision = rule.sample!(trace_op, span_op)

    next if decision == :not_matched # Iterate until we find a matching decision

    if decision == :kept
      trace_op.set_tag(
        Metadata::Ext::Distributed::TAG_DECISION_MAKER,
        Sampling::Ext::Decision::SPAN_SAMPLING_RATE
      )
    end

    break # Found either a `kept` or `rejected` decision
  end

  nil
end