Class: Metrics::Instruments::Meter

Inherits:
Object
  • Object
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))

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TimeConversion

#convert_to_ns

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
33
34
35
# File 'lib/ruby-metrics/instruments/meter.rb', line 18

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

  @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.



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

def count
  @count
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 => 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



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

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

#clearObject



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

def clear
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



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

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)
  count = @count
  if count == 0
    return 0.0;
  else
    elapsed = Time.now.to_f - @start_time.to_f
    convert_to_ns (count.to_f / elapsed.to_f), rate_unit
  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).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