Sha256: 51959a4a1afc72909dbd5b10b1ccdc14318ba22af45147341ed9fd859cd4c6ae

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

Contents

require 'barnes/consts'

module Barnes
  # The periodic class is used to send occasional metrics
  # to a reporting instance of `Barnes::Reporter` at a semi-regular
  # rate.
  class Periodic
    def initialize(reporter:, sample_rate: 1, panels: [])
      @reporter = reporter
      @reporter.sample_rate = sample_rate

      # 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
            @reporter.report env
          end
        end
      }
      @thread.abort_on_exception = true
    end

    def stop
      @thread.exit
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
barnes-0.0.3 lib/barnes/periodic.rb
barnes-0.0.2 /Users/schneems/Documents/projects/barnes/lib/barnes/periodic.rb
barnes-0.0.1 /Users/schneems/Documents/projects/barnes/lib/barnes/periodic.rb