Sha256: 4f17ade83f600742c4161d6a1b3612592e7eb914909fb4695c8cc41f17eaad4d

Contents?: true

Size: 973 Bytes

Versions: 3

Compression:

Stored size: 973 Bytes

Contents

require 'honeybadger/metric'

module Honeybadger
  class Histogram < Metric
    DEFAULT_BINS = [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0]
    INFINITY = 1e20.to_f # not quite, but pretty much

    def record(value)
      return unless value

      @samples += 1

      @total ||= 0
      @total = @total + value

      @min = value if @min.nil? || @min > value
      @max = value if @max.nil? || @max < value
      @avg = @total.to_f / @samples
      @latest = value

      @bin_counts ||= Hash.new(0)
      @bin_counts[find_bin(value)] += 1
    end

    def find_bin(value)
      bin = bins.find {|b| b >= value  }
      bin = INFINITY if bin.nil?
      bin
    end

    def bins
      @attributes.fetch(:bins, DEFAULT_BINS).sort
    end

    def payloads
      [{
        min: @min,
        max: @max,
        avg: @avg,
        latest: @latest,
        bins: (bins + [INFINITY]).map { |bin| [bin.to_f, @bin_counts[bin]] }
      }]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
honeybadger-5.19.2 lib/honeybadger/histogram.rb
honeybadger-5.19.1 lib/honeybadger/histogram.rb
honeybadger-5.19.0 lib/honeybadger/histogram.rb