Class: Datadog::PrioritySampler

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

Overview

PrioritySampler

Constant Summary collapse

SAMPLE_RATE_METRIC_KEY =
'_sample_rate'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ PrioritySampler

Returns a new instance of PrioritySampler.



126
127
128
129
# File 'lib/ddtrace/sampler.rb', line 126

def initialize(opts = {})
  @pre_sampler = opts[:base_sampler] || AllSampler.new
  @priority_sampler = opts[:post_sampler] || RateByServiceSampler.new
end

Instance Method Details

#sample!(span) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ddtrace/sampler.rb', line 135

def sample!(span)
  # If pre-sampling is configured, do it first. (By default, this will sample at 100%.)
  # NOTE: Pre-sampling at rates < 100% may result in partial traces; not recommended.
  span.sampled = pre_sample?(span) ? @pre_sampler.sample!(span) : true

  if span.sampled
    # If priority sampling has already been applied upstream, use that, otherwise...
    unless priority_assigned_upstream?(span)
      # Roll the dice and determine whether how we set the priority.
      # NOTE: We'll want to leave `span.sampled = true` here; all spans for priority sampling must
      #       be sent to the agent. Otherwise metrics for traces will not be accurate, since the
      #       agent will have an incomplete dataset.
      priority = priority_sample(span) ? Datadog::Ext::Priority::AUTO_KEEP : Datadog::Ext::Priority::AUTO_REJECT
      assign_priority!(span, priority)
    end
  else
    # If discarded by pre-sampling, set "reject" priority, so other
    # services for the same trace don't sample needlessly.
    assign_priority!(span, Datadog::Ext::Priority::AUTO_REJECT)
  end

  span.sampled
end

#sample?(span) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/ddtrace/sampler.rb', line 131

def sample?(span)
  @pre_sampler.sample?(span)
end