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.



198
199
200
201
# File 'lib/ddtrace/sampler.rb', line 198

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

Instance Method Details

#sample!(span) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/ddtrace/sampler.rb', line 207

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.
      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)


203
204
205
# File 'lib/ddtrace/sampler.rb', line 203

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