Class: Metrics::Instruments::Meter

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

Constant Summary collapse

INTERVAL_SECONDS =
5.0
ONE_MINUTE_FACTOR =
1 - Math.exp(-INTERVAL_SECONDS / 60.0)
FIVE_MINUTE_FACTOR =
1 - Math.exp(-INTERVAL_SECONDS / (60.0 * 5.0))
FIFTEEN_MINUTE_FACTOR =
1 - Math.exp(-INTERVAL_SECONDS / (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.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby-metrics/instruments/meter.rb', line 18

def initialize(options = {})
  @units = options[:units]
  clear

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

Instance Attribute Details

#countObject (readonly) Also known as: counted

Returns the value of attribute count.



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

def count
  @count
end

#unitsObject (readonly)

Returns the value of attribute units.



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

def units
  @units
end

Instance Method Details

#as_json(*_) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/ruby-metrics/instruments/meter.rb', line 86

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

#calc_rate(rate, factor, count) ⇒ Object



45
46
47
# File 'lib/ruby-metrics/instruments/meter.rb', line 45

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

#clearObject



34
35
36
37
38
39
# File 'lib/ruby-metrics/instruments/meter.rb', line 34

def clear
  @one_minute_rate = @five_minute_rate = @fifteen_minute_rate = 0.0
  @count  = 0
  @initialized = false
  @start_time = Time.now.to_f
end

#fifteen_minute_rate(rate_unit = :seconds) ⇒ Object



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

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

#five_minute_rate(rate_unit = :seconds) ⇒ Object



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

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

#mark(count = 1) ⇒ Object



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

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

#mean_rate(rate_unit = :seconds) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/ruby-metrics/instruments/meter.rb', line 76

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

#one_minute_rate(rate_unit = :seconds) ⇒ Object



64
65
66
# File 'lib/ruby-metrics/instruments/meter.rb', line 64

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

#tickObject



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

def tick
  count = @count.to_f / Seconds.to_nsec(INTERVAL_SECONDS).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



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

def to_json(*_)
  as_json.to_json
end