Class: OpenCensus::Trace::Samplers::Probability
- Inherits:
-
Object
- Object
- OpenCensus::Trace::Samplers::Probability
- 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, equal to 1/10000.
0.0001
Instance Method Summary collapse
-
#call(opts = {}) ⇒ boolean
Implements the sampler contract.
-
#initialize(rate, rng: nil) ⇒ Probability
constructor
Create a sampler for the given probability.
Constructor Details
#initialize(rate, rng: nil) ⇒ Probability
Create a sampler for the given probability.
39 40 41 42 43 44 45 46 |
# File 'lib/opencensus/trace/samplers/probability.rb', line 39 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 @lock = Monitor.new end |
Instance Method Details
#call(opts = {}) ⇒ boolean
Implements the sampler contract. Checks to see whether a sample should be taken at this time.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/opencensus/trace/samplers/probability.rb', line 60 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 @lock.synchronize do @rng.rand end end value <= @rate end |