Class: Metrics::Instruments::Meter

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

Direct Known Subclasses

PostProcessMeter

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
36
37
# 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

  if !options[:no_thread]
    @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

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



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

def as_json(*_)
  {
    :type => 'meter',
    :count => @count,
    :one_minute_rate => self.one_minute_rate,
    :five_minute_rate => self.five_minute_rate,
    :fifteen_minute_rate => self.fifteen_minute_rate,
    :mean => mean_rate,
    :unit => 'seconds'
  }
end

#calc_rate(rate, factor, count) ⇒ Object



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

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

#clearObject



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

def clear
end

#fifteen_minute_rate(rate_unit = :seconds) ⇒ Object



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

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

#five_minute_rate(rate_unit = :seconds) ⇒ Object



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

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

#mark(count = 1) ⇒ Object



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

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

#mean_rate(rate_unit = :seconds) ⇒ Object



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

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



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

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

#tickObject



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

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



100
101
102
# File 'lib/ruby-metrics/instruments/meter.rb', line 100

def to_json(*_)
  as_json.to_json
end