Class: Barnes::Periodic

Inherits:
Object
  • Object
show all
Defined in:
lib/barnes/periodic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reporter:, interval: 10, 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
# File 'lib/barnes/periodic.rb', line 31

def initialize(reporter:, interval: 10, debug: false, panels: [])
  @reporter = reporter
  @debug = debug
  @interval = interval
  @panels = panels
  @stopping = false

  @thread = Thread.new {
    Thread.current[:barnes_state] = {}

    @panels.each do |panel|
      panel.start! Thread.current[:barnes_state]
    end

    loop do
      sleep @interval
      break if @stopping

      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
    rescue => e
      $stderr.puts "barnes: error during metrics collection: #{e.class}: #{e.message}"
    end
  }
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



29
30
31
# File 'lib/barnes/periodic.rb', line 29

def thread
  @thread
end

Instance Method Details

#stop(wait:) ⇒ Object



67
68
69
70
71
# File 'lib/barnes/periodic.rb', line 67

def stop(wait: )
  @stopping = true
  @thread.wakeup if @thread.alive?
  @thread.join if wait
end