Class: OpenCensus::Trace::Samplers::RateLimiting

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

Overview

The RateLimiting sampler delays a minimum amount of time between each sample, enforcing a maximum QPS across traces that use this sampler.

Constant Summary collapse

DEFAULT_RATE =

Default rate in samples per second.

0.1

Instance Method Summary collapse

Constructor Details

#initialize(qps = DEFAULT_RATE, rng: nil, time_class: nil) ⇒ RateLimiting

Create a sampler for the given QPS.



40
41
42
43
44
45
46
# File 'lib/opencensus/trace/samplers/rate_limiting.rb', line 40

def initialize qps = DEFAULT_RATE, rng: nil, time_class: nil
  @qps = qps
  @time_class = time_class || Time
  @rng = rng || Random.new
  @last_time = @time_class.now.to_f
  @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.

Options Hash (opts):

  • :span_context (SpanContext)

    If provided, the span context will be checked and the parent's sampling decision will be propagated if the parent was sampled.



58
59
60
61
62
63
64
65
66
67
# File 'lib/opencensus/trace/samplers/rate_limiting.rb', line 58

def call opts = {}
  span_context = opts[:span_context]
  return true if span_context && span_context.sampled?
  @lock.synchronize do
    time = @time_class.now.to_f
    elapsed = time - @last_time
    @last_time = time
    @rng.rand <= elapsed * @qps
  end
end