class FFWD::Statistics::Collector

Constants

DEFAULT_PERIOD
DEFAULT_PREFIX

Public Class Methods

build(emitter, channel, opts={}) click to toggle source
# File lib/ffwd/statistics/collector.rb, line 29
def self.build emitter, channel, opts={}
  opts[:period] ||= DEFAULT_PERIOD
  opts[:prefix] ||= DEFAULT_PREFIX
  opts[:tags] ||= []
  opts[:attributes] ||= {}
  system = SystemStatistics.new(opts[:system] || {})
  system = if system.check then system end
  new emitter, channel, system, opts
end
new(emitter, channel, system, opts) click to toggle source

Initialize the statistics collector.

emitter - The emitter used to dispatch metrics for all reporters and statistics collectors. channel - A side-channel used by the SystemStatistics component to report information about the system. Messages sent on this channel help Core decide if it should seppuku.

# File lib/ffwd/statistics/collector.rb, line 46
def initialize emitter, channel, system, opts
  @emitter = emitter
  @channel = channel
  @system = system
  @period = opts[:period]
  @prefix = opts[:prefix]
  @tags = opts[:tags]
  @attributes = opts[:attributes]

  @reporters = {}
  @timer = nil

  starting do
    @last = Time.now

    @timer = EM::PeriodicTimer.new @period do
      now = Time.now
      generate! @last, now
      @last = now
    end

    log.info "Started #{opts.inspect}"
  end

  stopping do
    if @timer
      @timer.cancel
      @timer = nil
    end

    log.info "Stopped"
  end
end

Public Instance Methods

generate!(last, now) click to toggle source
# File lib/ffwd/statistics/collector.rb, line 80
def generate! last, now
  diff = now - last
  return if diff <= 0

  if @system
    @system.collect @channel do |key, unit, value|
      attributes = FFWD.merge_hashes @attributes, {:what => key, :unit => unit, :component => :system}
      @emitter.metric.emit(
        :key => @prefix, :value => value,
        :tags => @tags, :attributes => attributes)
    end
  end

  @reporters.each do |id, reporter|
    reporter.report!(diff) do |d|
      attributes = FFWD.merge_hashes @attributes, d[:meta]
      @emitter.metric.emit(
        :key => @prefix, :value => d[:value],
        :tags => @tags, :attributes => attributes)
    end
  end
end
register(lifecycle, id, reporter) click to toggle source
# File lib/ffwd/statistics/collector.rb, line 103
def register lifecycle, id, reporter
  lifecycle.starting do
    @reporters[id] = reporter
  end

  lifecycle.stopping do
    @reporters.delete id
  end
end