class Cabin::Metrics::Histogram

Public Class Methods

new() click to toggle source

A new Histogram.

# File lib/cabin/metrics/histogram.rb, line 10
def initialize
  @lock = Mutex.new
  @inspectables = [ :@total, :@min, :@max, :@count, :@mean ]

  # Histogram should track many things, including:
  # - percentiles (50, 75, 90, 95, 99?)
  # - median
  # - max
  # - min
  # - total sum
  #
  # Sliding values of all of these?
  @total = 0
  @min = nil
  @max = nil
  @count = 0
  @mean = 0.0
end

Public Instance Methods

record(value) click to toggle source
# File lib/cabin/metrics/histogram.rb, line 30
def record(value)
  @lock.synchronize do
    @count += 1
    @total += value
    if @min.nil? or value < @min
      @min = value
    end
    if @max.nil? or value > @max
      @max = value
    end
    @mean = @total / @count
    # TODO(sissel): median
    # TODO(sissel): percentiles
  end
  emit
end
to_hash() click to toggle source
# File lib/cabin/metrics/histogram.rb, line 55
def to_hash
  return @lock.synchronize do
    { 
      :count => @count,
      :total => @total,
      :min => @min,
      :max => @max,
      :mean => @mean,
    }
  end
end
value() click to toggle source

This is a very poor way to access the metric data. TODO(sissel): Need to figure out a better interface.

# File lib/cabin/metrics/histogram.rb, line 50
def value
  return @lock.synchronize { @count }
end