Class: OpenCensus::Trace::Samplers::MaxQPS

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

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(qps = 0.1) ⇒ MaxQPS

Create a sampler for the given QPS.

Parameters:

  • qps (Number) (defaults to: 0.1)

    Samples per second. Default is 0.1.



28
29
30
31
# File 'lib/opencensus/trace/samplers/max_qps.rb', line 28

def initialize qps = 0.1
  @delay_secs = 1.0 / qps
  @last_time = ::Time.now.to_f - @delay_secs
end

Instance Method Details

#call(_opts = {}) ⇒ boolean

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

Returns:

  • (boolean)

    Whether to sample at this time.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/opencensus/trace/samplers/max_qps.rb', line 39

def call _opts = {}
  time = ::Time.now.to_f
  delays = (time - @last_time) / @delay_secs
  if delays >= 2.0
    @last_time = time - @delay_secs
    true
  elsif delays >= 1.0
    @last_time += @delay_secs
    true
  else
    false
  end
end