class XRay::LocalReservoir

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.

Public Class Methods

new(traces_per_sec: 0) click to toggle source

@param [Integer] traces_per_sec The number of guranteed sampled

segments per second.
# 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

Public Instance Methods

take() click to toggle source

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

# 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