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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ PrioritySampler

Returns a new instance of PrioritySampler.



207
208
209
210
# File 'lib/ddtrace/sampler.rb', line 207

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

Instance Attribute Details

#pre_samplerObject (readonly)

NOTE: We do not advise using a pre-sampler. It can save resources, but pre-sampling at rates < 100% may result in partial traces, unless the pre-sampler knows exactly how to drop a span without dropping its ancestors.

Additionally, as service metrics are calculated in the Datadog Agent, the service's throughput will be underestimated.



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

def pre_sampler
  @pre_sampler
end

#priority_samplerObject (readonly)

NOTE: We do not advise using a pre-sampler. It can save resources, but pre-sampling at rates < 100% may result in partial traces, unless the pre-sampler knows exactly how to drop a span without dropping its ancestors.

Additionally, as service metrics are calculated in the Datadog Agent, the service's throughput will be underestimated.



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

def priority_sampler
  @priority_sampler
end

Instance Method Details

#sample!(span) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/ddtrace/sampler.rb', line 216

def sample!(span)
  # If pre-sampling is configured, do it first. (By default, this will sample at 100%.)
  span.sampled = pre_sample?(span) ? @pre_sampler.sample!(span) : true

  if span.sampled
    # If priority sampling has already been applied upstream, use that value.
    return true if priority_assigned?(span)

    # Check with post sampler how we set the priority.
    sample = priority_sample!(span)

    # Check if post sampler has already assigned a priority.
    return true if priority_assigned?(span)

    # If not, use agent priority values.
    priority = sample ? Datadog::Ext::Priority::AUTO_KEEP : Datadog::Ext::Priority::AUTO_REJECT
    assign_priority!(span, priority)
  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)


212
213
214
# File 'lib/ddtrace/sampler.rb', line 212

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