Class: Metrics::Reporters::GangliaReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-metrics/reporters/ganglia.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GangliaReporter

Returns a new instance of GangliaReporter.



13
14
15
16
# File 'lib/ruby-metrics/reporters/ganglia.rb', line 13

def initialize(options = {})
  @host_ip = options[:host_ip]
  @host_port = options[:host_port] 
end

Instance Attribute Details

#host_ipObject (readonly)

Returns the value of attribute host_ip.



10
11
12
# File 'lib/ruby-metrics/reporters/ganglia.rb', line 10

def host_ip
  @host_ip
end

#host_portObject (readonly)

Returns the value of attribute host_port.



11
12
13
# File 'lib/ruby-metrics/reporters/ganglia.rb', line 11

def host_port
  @host_port
end

Instance Method Details

#report(agent) ⇒ Object



42
43
44
45
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
# File 'lib/ruby-metrics/reporters/ganglia.rb', line 42

def report(agent)

  agent.instruments.each do |name, instrument|
    nothing_to_do = false
    data =  { :name => name, :units => instrument.units }
    case instrument
      when Metrics::Instruments::Counter
        value = instrument.to_i
        data.merge! :value => value.to_i
        send_data data
      when Metrics::Instruments::Gauge
        if instrument.get.is_a? Hash
          instrument.get.each do |key, value|
            data.merge! :name => "#{name}_#{key}", :value => value
            send_data data
          end
        else 
          data.merge! :value => instrument.get
          send_data data
        end
      when Metrics::Instruments::Timer
        [:count, :fifteen_minute_rate, :five_minute_rate, :one_minute_rate, :min, :max, :mean].each do |attribute|
          data.merge!(:name => "#{name}_#{attribute}", :value => instrument.send(attribute))
          send_data data
        end
      when Metrics::Instruments::Meter
        [:count, :fifteen_minute_rate, :five_minute_rate, :one_minute_rate, :mean_rate].each do |attribute|
          data.merge!(:name => "#{name_attribute}", :value => instrument.send(attribute) )
        end
      else 
        puts "Unhandled instrument"
    end
  end
end

#send_data(data) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby-metrics/reporters/ganglia.rb', line 18

def send_data(data)
  puts "Sending data: #{data.inspect}"
  data_type = case data[:value].class.to_s
              when "Fixnum"
                "uint32"
              when "Float"
                "float"
              when "String"
                "string"
              else
                "unknown"
              end

  Ganglia::GMetric.send(@host_ip, @host_port.to_i, {
     :spoof => 0,
     :name => data[:name],
     :units => data[:units],
     :type => data_type,
     :value => data[:value],
     :tmax => 60,
     :dmax => 300,
  })
end