Class: Cabin::Metrics::Histogram

Inherits:
Object
  • Object
show all
Includes:
Cabin::Metric
Defined in:
lib/cabin/metrics/histogram.rb

Direct Known Subclasses

Timer

Instance Method Summary collapse

Methods included from Cabin::Metric

#emit, #instance, #instance=

Methods included from Publisher

#channel, #channel=, #publish

Methods included from Inspectable

#inspect

Constructor Details

#initializeHistogram

Returns a new instance of Histogram.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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

Instance Method Details

#record(value) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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_hashObject



55
56
57
58
59
60
61
62
63
64
65
# 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

#valueObject



50
51
52
# File 'lib/cabin/metrics/histogram.rb', line 50

def value
  return @lock.synchronize { @count }
end