Class: Graphiterb::Monitors::PeriodicMonitor
- Inherits:
-
Object
- Object
- Graphiterb::Monitors::PeriodicMonitor
- Includes:
- Utils::SystemInfo
- Defined in:
- lib/graphiterb/monitors.rb
Overview
Accepts a lightweight call every iteration.
Once either a time or an iteration criterion is met, executes the block and resets the timer until next execution.
Note that the time_interval
is measured *excution to execution* and not in multiples of iter_interval. Say I set a time_interval of 300s, and happen to iterate at 297s and 310s after start. Then the monitor will execute at 310s, and the next execution will happen on or after 610s.
Also note that when either criterion is met, both criteria are reset. Say I set a time interval of 300s and an iter_interval
of 10_000; and that at 250s I reach iteration 10_000. Then the monitor will execute on or after 20_000 iteration or 550s, whichever happens first.
Stolen from Monkeyshines::Monitor::PeriodicMonitor
Direct Known Subclasses
Instance Attribute Summary collapse
-
#current_iter ⇒ Object
Internal metrics stored by the monitor.
-
#iter ⇒ Object
Internal metrics stored by the monitor.
-
#iter_interval ⇒ Object
Maximum number of internal “iterations” that should elapse between running the monitor.
-
#last_time ⇒ Object
Internal metrics stored by the monitor.
-
#main_scope ⇒ Object
The main scope under which the monitor’s metrics will be written.
-
#options ⇒ Object
The options hash the monitor was created with.
-
#started_at ⇒ Object
Internal metrics stored by the monitor.
-
#time_interval ⇒ Object
Maximum number of seconds that should elapse between running the monitor.
Instance Method Summary collapse
-
#enough_iterations? ⇒ Boolean
True if more than
iter_interval
has elapsed since last execution. -
#enough_time?(now) ⇒ Boolean
True if more than
time_interval
has elapsed since last execution. -
#get_metrics(metrics, since) ⇒ Object
Add metrics to the
metrics
array. -
#initialize(main_scope, options = {}) ⇒ PeriodicMonitor
constructor
Create a new PeriodicMonitor.
-
#inst_rate(now) ⇒ Float
“Instantaneous” iterations per second.
-
#periodically {|Array<String>, Time| ... } ⇒ Object
If the interval conditions are met, executes block; otherwise just does bookkeeping and returns.
-
#rate ⇒ Float
Overall iterations per second.
-
#run! ⇒ Object
Run this monitor.
-
#scope(*names) ⇒ String
Return the scope built from this monitor’s
main_scope
and the givennames
. -
#sender ⇒ Graphiterb::Sender
The Graphiterb::Sender used to communicate with the Graphite server.
-
#since ⇒ Time
Time since monitor was created.
Methods included from Utils::SystemInfo
#graphite_identifier, #hostname, #node_name
Constructor Details
#initialize(main_scope, options = {}) ⇒ PeriodicMonitor
Create a new PeriodicMonitor
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/graphiterb/monitors.rb', line 49 def initialize main_scope, ={} self.main_scope = main_scope self.started_at = Time.now.utc.to_f self.last_time = started_at self.iter = 0 self.current_iter = 0 self. = self.time_interval = [:time] || 30 self.iter_interval = [:iters] || 30 end |
Instance Attribute Details
#current_iter ⇒ Object
Internal metrics stored by the monitor.
42 43 44 |
# File 'lib/graphiterb/monitors.rb', line 42 def current_iter @current_iter end |
#iter ⇒ Object
Internal metrics stored by the monitor.
42 43 44 |
# File 'lib/graphiterb/monitors.rb', line 42 def iter @iter end |
#iter_interval ⇒ Object
Maximum number of internal “iterations” that should elapse between running the monitor
36 37 38 |
# File 'lib/graphiterb/monitors.rb', line 36 def iter_interval @iter_interval end |
#last_time ⇒ Object
Internal metrics stored by the monitor.
42 43 44 |
# File 'lib/graphiterb/monitors.rb', line 42 def last_time @last_time end |
#main_scope ⇒ Object
The main scope under which the monitor’s metrics will be written.
29 30 31 |
# File 'lib/graphiterb/monitors.rb', line 29 def main_scope @main_scope end |
#options ⇒ Object
The options hash the monitor was created with.
39 40 41 |
# File 'lib/graphiterb/monitors.rb', line 39 def @options end |
#started_at ⇒ Object
Internal metrics stored by the monitor.
42 43 44 |
# File 'lib/graphiterb/monitors.rb', line 42 def started_at @started_at end |
#time_interval ⇒ Object
Maximum number of seconds that should elapse between running the monitor.
33 34 35 |
# File 'lib/graphiterb/monitors.rb', line 33 def time_interval @time_interval end |
Instance Method Details
#enough_iterations? ⇒ Boolean
True if more than iter_interval
has elapsed since last execution.
70 71 72 |
# File 'lib/graphiterb/monitors.rb', line 70 def enough_iterations? iter % iter_interval == 0 if iter_interval end |
#enough_time?(now) ⇒ Boolean
True if more than time_interval
has elapsed since last execution.
75 76 77 |
# File 'lib/graphiterb/monitors.rb', line 75 def enough_time? now (now - last_time) > time_interval if time_interval end |
#get_metrics(metrics, since) ⇒ Object
Add metrics to the metrics
array.
This method is meant to be overridden by a sub-class (indeed, it will raise an error if called directly).
It should take an array of metrics and a time interval since last ran and insert metrics into the array.“)
141 142 143 |
# File 'lib/graphiterb/monitors.rb', line 141 def get_metrics metrics, since raise Graphiterb::NotImplementedError.new("Override the get_metrics method of the #{self.class} class") end |
#inst_rate(now) ⇒ Float
“Instantaneous” iterations per second
96 97 98 |
# File 'lib/graphiterb/monitors.rb', line 96 def inst_rate now current_iter.to_f / (now-last_time).to_f end |
#periodically {|Array<String>, Time| ... } ⇒ Object
If the interval conditions are met, executes block; otherwise just does bookkeeping and returns.
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/graphiterb/monitors.rb', line 118 def periodically &block self.iter += 1 self.current_iter += 1 now = Time.now.utc.to_f if enough_iterations? || enough_time?(now) metrics = [] block.call(metrics, (now-last_time)) sender.send(*metrics) self.last_time = now self.current_iter = 0 end end |
#rate ⇒ Float
Overall iterations per second
89 90 91 |
# File 'lib/graphiterb/monitors.rb', line 89 def rate iter.to_f / since.to_f end |
#run! ⇒ Object
Run this monitor.
Sleep for 1 second and then wake up and check if either enough time (time_interval
) or enough iterations (iter_interval
) have passed run get_metrics
if so.
150 151 152 153 154 155 156 157 |
# File 'lib/graphiterb/monitors.rb', line 150 def run! loop do periodically do |metrics, since| get_metrics metrics, since end sleep 1 end end |
#scope(*names) ⇒ String
Return the scope built from this monitor’s main_scope
and the given names
.
monitor.main_scope
#=> 'system.parameters'
monitor.scope 'disk', 'space'
#=> 'system.paramters.disk.space'
110 111 112 |
# File 'lib/graphiterb/monitors.rb', line 110 def scope *names [main_scope, *names].flatten.reject(&:blank?).join('.') end |
#sender ⇒ Graphiterb::Sender
The Graphiterb::Sender used to communicate with the Graphite server.
64 65 66 |
# File 'lib/graphiterb/monitors.rb', line 64 def sender @sender ||= Graphiterb::Sender.new end |
#since ⇒ Time
Time since monitor was created
82 83 84 |
# File 'lib/graphiterb/monitors.rb', line 82 def since Time.now.utc.to_f - started_at end |