Class: XRay::Reservoir
- Inherits:
-
Object
- Object
- XRay::Reservoir
- Defined in:
- lib/aws-xray-sdk/sampling/reservoir.rb
Overview
Keeps track of the number of sampled segments within a single second. This class is implemented to be thread-safe to achieve accurate sampling.
Instance Method Summary collapse
-
#initialize(traces_per_sec: 0) ⇒ Reservoir
constructor
A new instance of Reservoir.
-
#take ⇒ Object
Returns ‘true` if there are segments left within the current second, otherwise returns `false`.
Constructor Details
#initialize(traces_per_sec: 0) ⇒ Reservoir
Returns a new instance of Reservoir.
8 9 10 11 12 13 |
# File 'lib/aws-xray-sdk/sampling/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
#take ⇒ Object
Returns ‘true` if there are segments 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/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 |