Class: Metrics::Instruments::Meter

Inherits:
Instrument show all
Includes:
TimeConversion
Defined in:
lib/ruby-metrics/instruments/meter.rb

Constant Summary collapse

INTERVAL =
5.0
INTERVAL_IN_NS =
5000000000.0
ONE_MINUTE_FACTOR =
1 - Math.exp(-INTERVAL / 60.0)
FIVE_MINUTE_FACTOR =
1 - Math.exp(-INTERVAL / (60.0 * 5.0))
FIFTEEN_MINUTE_FACTOR =
1 - Math.exp(-INTERVAL / (60.0 * 15.0))

Constants included from TimeConversion

TimeConversion::UNITS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TimeConversion

#convert_to_ns, #scale_time_units

Methods inherited from Instrument

#tag, #tags

Constructor Details

#initialize(options = {}) ⇒ Meter

Returns a new instance of Meter.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby-metrics/instruments/meter.rb', line 20

def initialize(options = {})
  @one_minute_rate = @five_minute_rate = @fifteen_minute_rate = 0.0
  @count  = 0
  @initialized = false
  @start_time = Time.now.to_f
  @units = options[:units]

  @timer_thread = Thread.new do
    begin
      loop do
        self.tick
        sleep(INTERVAL)
      end
    rescue Exception => e
      logger.error "Error in timer thread: #{e.class.name}: #{e}\n  #{e.backtrace.join("\n  ")}"
    end # begin
  end # thread new

end

Instance Attribute Details

#countObject (readonly) Also known as: counted

Returns the value of attribute count.



16
17
18
# File 'lib/ruby-metrics/instruments/meter.rb', line 16

def count
  @count
end

#unitsObject (readonly)

Returns the value of attribute units.



17
18
19
# File 'lib/ruby-metrics/instruments/meter.rb', line 17

def units
  @units
end

Instance Method Details

#as_json(*_) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/ruby-metrics/instruments/meter.rb', line 90

def as_json(*_)
  {
    :one_minute_rate => self.one_minute_rate,
    :five_minute_rate => self.five_minute_rate,
    :fifteen_minute_rate => self.fifteen_minute_rate
  }
end

#calc_rate(rate, factor, count) ⇒ Object



47
48
49
50
# File 'lib/ruby-metrics/instruments/meter.rb', line 47

def calc_rate(rate, factor, count)
  rate = rate.to_f + (factor.to_f * (count.to_f - rate.to_f))
  rate.to_f
end

#clearObject



40
41
# File 'lib/ruby-metrics/instruments/meter.rb', line 40

def clear
end

#fifteen_minute_rate(rate_unit = :seconds) ⇒ Object



75
76
77
# File 'lib/ruby-metrics/instruments/meter.rb', line 75

def fifteen_minute_rate(rate_unit = :seconds)
  convert_to_ns @fifteen_minute_rate, rate_unit
end

#five_minute_rate(rate_unit = :seconds) ⇒ Object



71
72
73
# File 'lib/ruby-metrics/instruments/meter.rb', line 71

def five_minute_rate(rate_unit = :seconds)
  convert_to_ns @five_minute_rate, rate_unit
end

#mark(count = 1) ⇒ Object



43
44
45
# File 'lib/ruby-metrics/instruments/meter.rb', line 43

def mark(count = 1)
  @count += count
end

#mean_rate(rate_unit = :seconds) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/ruby-metrics/instruments/meter.rb', line 79

def mean_rate(rate_unit = :seconds)
  count = @count
  if count == 0
    return 0.0
  else
    elapsed = Time.now.to_f - @start_time.to_f
    mult = scale_time_units(:seconds, rate_unit)
    count.to_f / (mult * elapsed.to_f)
  end
end

#one_minute_rate(rate_unit = :seconds) ⇒ Object



67
68
69
# File 'lib/ruby-metrics/instruments/meter.rb', line 67

def one_minute_rate(rate_unit = :seconds)
  convert_to_ns @one_minute_rate, rate_unit
end

#tickObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ruby-metrics/instruments/meter.rb', line 52

def tick
  count = @count.to_f  / Seconds.to_nsec(INTERVAL).to_f

  if (@initialized)
    @one_minute_rate      = calc_rate(@one_minute_rate,     ONE_MINUTE_FACTOR,     count)
    @five_minute_rate     = calc_rate(@five_minute_rate,    FIVE_MINUTE_FACTOR,    count)
    @fifteen_minute_rate  = calc_rate(@fifteen_minute_rate, FIFTEEN_MINUTE_FACTOR, count)
  else
    @one_minute_rate = @five_minute_rate = @fifteen_minute_rate  = (count)
    @initialized = true
  end

  @count = 0
end

#to_json(*_) ⇒ Object



98
99
100
# File 'lib/ruby-metrics/instruments/meter.rb', line 98

def to_json(*_)
  as_json.to_json
end