Class: Metrics::Instruments::Meter
- Inherits:
-
Object
- Object
- Metrics::Instruments::Meter
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))
TimeConversion::UNITS
Instance Attribute Summary collapse
Instance Method Summary
collapse
#convert_to_ns, #scale_time_units
Constructor Details
#initialize(options = {}) ⇒ Meter
Returns a new instance of Meter.
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 19
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
end
end
|
Instance Attribute Details
#count ⇒ Object
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
|
#units ⇒ Object
Returns the value of attribute units.
16
17
18
|
# File 'lib/ruby-metrics/instruments/meter.rb', line 16
def units
@units
end
|
Instance Method Details
#as_json(*_) ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/ruby-metrics/instruments/meter.rb', line 89
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
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
|
#clear ⇒ Object
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
87
|
# 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
mult = scale_time_units(:seconds, rate_unit)
count.to_f / (mult * elapsed.to_f)
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
|
#tick ⇒ Object
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
97
98
99
|
# File 'lib/ruby-metrics/instruments/meter.rb', line 97
def to_json(*_)
as_json.to_json
end
|