Class: FFWD::Statistics::Collector

Inherits:
Object
  • Object
show all
Includes:
Lifecycle, Logging
Defined in:
lib/ffwd/statistics/collector.rb

Constant Summary collapse

DEFAULT_PERIOD =
10
DEFAULT_PREFIX =
"ffwd"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, #log

Methods included from Lifecycle

#depend_on, #start, #started?, #starting, #starting_hooks, #stop, #stopped?, #stopping, #stopping_hooks

Constructor Details

#initialize(emitter, channel, system, opts) ⇒ Collector

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.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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

Class Method Details

.build(emitter, channel, opts = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
# 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

Instance Method Details

#generate!(last, now) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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) ⇒ Object



103
104
105
106
107
108
109
110
111
# 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