Class: MetricsMachine::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/metrics_machine/monitor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reporter, options = {}, &block) ⇒ Monitor

Returns a new instance of Monitor.



9
10
11
12
13
14
# File 'lib/metrics_machine/monitor.rb', line 9

def initialize reporter, options = {}, &block
  @reporter = reporter
  @monitors = {}
  @prefix = options.fetch(:prefix, self.class.default_prefix)
  configure &block if block_given?
end

Instance Attribute Details

#monitorsObject (readonly)

Returns the value of attribute monitors.



7
8
9
# File 'lib/metrics_machine/monitor.rb', line 7

def monitors
  @monitors
end

#reporterObject (readonly)

Returns the value of attribute reporter.



7
8
9
# File 'lib/metrics_machine/monitor.rb', line 7

def reporter
  @reporter
end

Class Method Details

.default_prefixObject



61
62
63
# File 'lib/metrics_machine/monitor.rb', line 61

def self.default_prefix
  `hostname`.strip.split('.').reverse.join('.')
end

Instance Method Details

#configure(&block) ⇒ Object



24
25
26
# File 'lib/metrics_machine/monitor.rb', line 24

def configure &block
  instance_eval &block
end

#monitor(symbol_or_class, *args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/metrics_machine/monitor.rb', line 42

def monitor symbol_or_class, *args
  key = nil
  klass = case symbol_or_class
            when Symbol
              MetricsMachine.const_get(symbol_or_class.to_s.capitalize)
            when Class
              symbol_or_class
            when Hash
              key = symbol_or_class.keys.first
              symbol_or_class[key]
          end

  c = klass.new *args
  key ||= c.key if c.respond_to? :key
  key ||= klass.key if klass.respond_to? :key
  key ||= ActiveSupport::Inflector.underscore(klass.to_s.gsub('MetricsMachine::', ''))
  monitors[key] = c
end

#prefix(*args) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/metrics_machine/monitor.rb', line 16

def prefix *args
  if args.empty?
    @prefix
  else
    @prefix = args.first
  end
end

#report(name, monitor) ⇒ Object



36
37
38
39
40
# File 'lib/metrics_machine/monitor.rb', line 36

def report name, monitor
  monitor.statistics.each do |k,v|
    reporter.gauge "#{prefix}.#{name}.#{ActiveSupport::Inflector.underscore(k)}", v
  end
end

#run!Object



28
29
30
31
32
33
34
# File 'lib/metrics_machine/monitor.rb', line 28

def run!
  monitors.each do |name,monitor|
    EM.add_periodic_timer(monitor.interval) do
      report name, monitor
    end
  end
end