class Metrux::Commands::PeriodicGauge::Agent

Constants

DEFAULT_INVERVAL

Attributes

command[R]
interval[R]
registry[R]

Public Class Methods

new(command, registry, config) click to toggle source
# File lib/metrux/commands/periodic_gauge/agent.rb, line 10
def initialize(command, registry, config)
  @command = command
  @registry = registry
  @interval = config.periodic_gauge_interval || DEFAULT_INVERVAL
  @logger = config.logger
  @thread = nil
end

Public Instance Methods

alive?() click to toggle source
# File lib/metrux/commands/periodic_gauge/agent.rb, line 40
def alive?
  @thread && @thread.alive?
end
start() click to toggle source
# File lib/metrux/commands/periodic_gauge/agent.rb, line 18
def start
  return false if alive?

  log('Starting...', :info)
  @thread = Thread.new do
    loop do
      log("sleeping for #{interval}s...")
      wait(interval)

      metrics.each(&method(:execute_metric))
    end
  end

  true
end
stop() click to toggle source
# File lib/metrux/commands/periodic_gauge/agent.rb, line 34
def stop
  log('Stopping...', :info)
  @thread.kill if @thread
  @thread = nil
end

Private Instance Methods

execute_metric(key, params) click to toggle source
# File lib/metrux/commands/periodic_gauge/agent.rb, line 52
def execute_metric(key, params)
  log("Executing #{key}...", :info)

  command.gauge(
    params.fetch(:measurement), params.fetch(:options),
    &params.fetch(:metric)
  )
rescue => e
  log(
    "ERROR #{key}: #{e.class}: #{e.message} #{e.backtrace.take(2)}",
    :error
  )
end
metrics() click to toggle source
# File lib/metrux/commands/periodic_gauge/agent.rb, line 48
def metrics
  registry.metrics
end