Sha256: 1be1ca3078129e262c2daebfe10fd438a9392d4e69a39d6b8d6a0b3f6fbfe02a

Contents?: true

Size: 1.68 KB

Versions: 2

Compression:

Stored size: 1.68 KB

Contents

# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.

# This class acts like an Array with a fixed capacity that randomly samples
# from a stream of items such that the probability of each item being included
# in the Array is equal. It uses reservoir sampling in order to achieve this:
# http://xlinux.nist.gov/dads/HTML/reservoirSampling.html

require 'new_relic/agent/event_buffer'

module NewRelic
  module Agent
    class SampledBuffer < EventBuffer
      attr_reader :seen_lifetime, :captured_lifetime

      def initialize(capacity)
        super
        @captured_lifetime = 0
        @seen_lifetime     = 0
      end

      def reset!
        @captured_lifetime += @items.size
        @seen_lifetime     += @seen
        super
      end

      def append(x = nil, &blk)
        @seen += 1
        append_event(x, &blk)
      end

      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
          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

      def decrement_lifetime_counts_by n
        @captured_lifetime -= n
        @seen_lifetime -= n
      end

      def sample_rate_lifetime
        @captured_lifetime > 0 ? (@captured_lifetime.to_f / @seen_lifetime) : 0.0
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
newrelic_rpm-3.14.1.311 lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.14.0.305 lib/new_relic/agent/sampled_buffer.rb