Class: Barnes::Periodic
- Inherits:
-
Object
- Object
- Barnes::Periodic
- Defined in:
- lib/barnes/periodic.rb
Overview
The periodic class is used to send occasional metrics to a reporting instance of ‘Barnes::Reporter` at a semi-regular rate.
Instance Method Summary collapse
-
#initialize(reporter:, sample_rate: 1, debug: false, panels: []) ⇒ Periodic
constructor
A new instance of Periodic.
- #stop ⇒ Object
Constructor Details
#initialize(reporter:, sample_rate: 1, debug: false, panels: []) ⇒ Periodic
Returns a new instance of Periodic.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/barnes/periodic.rb', line 31 def initialize(reporter:, sample_rate: 1, debug: false, panels: []) @reporter = reporter @reporter.sample_rate = sample_rate @debug = debug # compute interval based on a 60s reporting phase. @interval = sample_rate * 60.0 @panels = panels @thread = Thread.new { Thread.current[:barnes_state] = {} @panels.each do |panel| panel.start! Thread.current[:barnes_state] end loop do begin sleep @interval # read the current values env = { STATE => Thread.current[:barnes_state], COUNTERS => {}, GAUGES => {} } @panels.each do |panel| panel.instrument! env[STATE], env[COUNTERS], env[GAUGES] end puts env.to_json if @debug @reporter.report env end end } @thread.abort_on_exception = true end |
Instance Method Details
#stop ⇒ Object
69 70 71 |
# File 'lib/barnes/periodic.rb', line 69 def stop @thread.exit end |