Sha256: a33464209154791774e2433a11d49819476037b0105c5b90393fed70afe25db3

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

# encoding: utf-8

# http://xlinux.nist.gov/dads/HTML/reservoirSampling.html

require 'forwardable'
require 'one_apm/support/event_buffer'

module OneApm
  module Agent
    class SampledBuffer < EventBuffer
      extend Forwardable
      attr_reader :seen_lifetime, :captured_lifetime
      def_delegators :@items, :select

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

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

      def append_event(x)
        if @items.size < @capacity
          @items << x
          return x
        else
          m = rand(@seen) # [0, @seen)
          if m < @capacity
            @items[m] = x
            return x
          else
            # discard current sample
            return nil
          end
        end
      end


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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
oneapm_rpm-1.4.2 lib/one_apm/support/event_buffer/sampled_buffer.rb
oneapm_rpm-1.4.1 lib/one_apm/support/event_buffer/sampled_buffer.rb
oneapm_rpm-1.4.0 lib/one_apm/support/event_buffer/sampled_buffer.rb