Class: Salus::Counter

Inherits:
Metric
  • Object
show all
Defined in:
lib/salus/metric/counter.rb

Constant Summary collapse

INT32_MAX =
2**32

Constants inherited from Metric

Metric::STORAGE_DEPTH

Instance Method Summary collapse

Methods inherited from Metric

#clear, descendants, #expired?, inherited, #load, #mute?, #push, #save, #timestamp, #to_h, #ttl, #value

Methods included from Lockable

#broadcast, #signal, #synchronize, #wait, #wait_until

Methods included from Logging

#log

Constructor Details

#initialize(defaults = {}) ⇒ Counter

Returns a new instance of Counter.



4
5
6
7
8
# File 'lib/salus/metric/counter.rb', line 4

def initialize(defaults={})
  super(defaults)
  option :maximum, Numeric
  validate(:maximum, @opts[:maximum]) if @opts.key?(:maximum)
end

Instance Method Details

#calcObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/salus/metric/counter.rb', line 10

def calc
  super
  @last_calced_value = nil

  if @values.length < STORAGE_DEPTH
    return
  elsif @values[0].expired?(@values[1].timestamp)
    return
  elsif !@values[0].value.is_a?(Numeric)
    return
  elsif !@values[1].value.is_a?(Numeric)
    return
  end

  @last_calced_value = begin
    dt = (@values[1].timestamp - @values[0].timestamp)
    dv = if @values[1].value < @values[0].value
      w = @values[0].value < INT32_MAX ? 32 : 64
      (2**w - @values[0].value + @values[1].value)
    else
      (@values[1].value - @values[0].value)
    end
    r = (dt == 0) ? nil : (dv / dt)
    if @opts.key?(:maximum) && !r.nil? && r > @opts[:maximum]
      nil
    else
      r
    end
  end
end