Class: XRay::LocalReservoir

Inherits:
Object
  • Object
show all
Defined in:
lib/aws-xray-sdk/sampling/local/reservoir.rb

Overview

Keeps track of the number of sampled segments within a single second in the local process. This class is implemented to be thread-safe to achieve accurate sampling.

Instance Method Summary collapse

Constructor Details

#initialize(traces_per_sec: 0) ⇒ LocalReservoir

Returns a new instance of LocalReservoir.

Parameters:

  • traces_per_sec (Integer) (defaults to: 0)

    The number of guranteed sampled segments per second.



8
9
10
11
12
13
# File 'lib/aws-xray-sdk/sampling/local/reservoir.rb', line 8

def initialize(traces_per_sec: 0)
  @traces_per_sec = traces_per_sec
  @used_this_sec = 0
  @this_sec = Time.now.to_i
  @lock = Mutex.new
end

Instance Method Details

#takeObject

Returns ‘true` if there are quota left within the current second, otherwise returns `false`.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/aws-xray-sdk/sampling/local/reservoir.rb', line 17

def take
  # nothing to provide if reserved is set to 0
  return false if @traces_per_sec.zero?
  @lock.synchronize do
    now = Time.now.to_i
    # refresh time frame
    if now != @this_sec
      @used_this_sec = 0
      @this_sec = now
    end
    # return false if reserved item ran out
    return false unless @used_this_sec < @traces_per_sec
    # otherwise increment used counter and return true
    @used_this_sec += 1
    return true
  end
end