class NewRelic::Agent::SampledBuffer

Attributes

captured_lifetime[R]
seen_lifetime[R]

Public Class Methods

new(capacity) click to toggle source
Calls superclass method NewRelic::Agent::EventBuffer.new
# File lib/new_relic/agent/sampled_buffer.rb, line 16
def initialize(capacity)
  super
  @captured_lifetime = 0
  @seen_lifetime     = 0
end

Public Instance Methods

append(x = nil, &blk) click to toggle source
# File lib/new_relic/agent/sampled_buffer.rb, line 22
def append(x = nil, &blk)
  @seen += 1
  @seen_lifetime += 1
  append_event(x, &blk)
end
append_event(x = nil, &blk) click to toggle source
# File lib/new_relic/agent/sampled_buffer.rb, line 28
def append_event(x = nil, &blk)
  raise ArgumentError, "Expected argument or block, but received both" if x && blk

  if @items.size < @capacity
    x = blk.call if block_given?
    @items << x
    @captured_lifetime += 1
    return x
  else
    m = rand(@seen) # [0, @seen)
    if m < @capacity
      x = blk.call if block_given?
      @items[m] = x
      return x
    else
      # discard current sample
      return nil
    end
  end
end
decrement_lifetime_counts_by(n) click to toggle source
# File lib/new_relic/agent/sampled_buffer.rb, line 49
def decrement_lifetime_counts_by n
  @captured_lifetime -= n
  @seen_lifetime -= n
end
metadata() click to toggle source
Calls superclass method NewRelic::Agent::EventBuffer#metadata
# File lib/new_relic/agent/sampled_buffer.rb, line 58
def metadata
  super.merge!(
    :captured_lifetime => @captured_lifetime,
    :seen_lifetime => @seen_lifetime
  )
end
sample_rate_lifetime() click to toggle source
# File lib/new_relic/agent/sampled_buffer.rb, line 54
def sample_rate_lifetime
  @captured_lifetime > 0 ? (@captured_lifetime.to_f / @seen_lifetime) : 0.0
end