Class: OpenCensus::Trace::Samplers::Probability

Inherits:
Object
  • Object
show all
Defined in:
lib/opencensus/trace/samplers/probability.rb

Overview

The Probability sampler uses a random number generator against a configured rate to determine whether or not to sample.

Constant Summary collapse

DEFAULT_RATE =

The default sampling probability.

0.1

Instance Method Summary collapse

Constructor Details

#initialize(rate, rng: nil) ⇒ Probability

Create a sampler for the given probability.

Parameters:

  • rate (Number)

    Probability that we will sample. This value must be between 0 and 1.

  • rng (#rand) (defaults to: nil)

    The random number generator to use. Default is a new Random instance.



36
37
38
39
40
41
42
# File 'lib/opencensus/trace/samplers/probability.rb', line 36

def initialize rate, rng: nil
  if rate > 1 || rate < 0
    raise ArgumentError, "Invalid rate - must be between 0 and 1."
  end
  @rate = rate
  @rng = rng || Random.new
end

Instance Method Details

#call(opts = {}) ⇒ boolean

Implements the sampler contract. Checks to see whether a sample should be taken at this time.

Parameters:

  • opts (Hash) (defaults to: {})

    The options to sample with.

Options Hash (opts):

  • :span_context (SpanContext)

    If provided, the span context will be used to generate a deterministic value in place of the pseudo-random number generator. #

Returns:

  • (boolean)

    Whether to sample at this time.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/opencensus/trace/samplers/probability.rb', line 54

def call opts = {}
  span_context = opts[:span_context]
  return true if span_context && span_context.sampled?
  value =
    if span_context
      (span_context.trace_id.to_i(16) % 0x10000000000000000).to_f /
        0x10000000000000000
    else
      @rng.rand
    end
  value <= @rate
end