Class: Metrics::Reporters::OpenTSDBReporter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ OpenTSDBReporter

Returns a new instance of OpenTSDBReporter.



16
17
18
19
20
21
# File 'lib/ruby-metrics/reporters/opentsdb.rb', line 16

def initialize(options = {})
  @hostname = options[:hostname]
  @port = options[:port] 
  @client = OpenTSDB::Client.new({:hostname => @hostname, :port => @port})
  @tags = options[:tags]
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



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

def hostname
  @hostname
end

#portObject (readonly)

Returns the value of attribute port.



12
13
14
# File 'lib/ruby-metrics/reporters/opentsdb.rb', line 12

def port
  @port
end

#tagsObject (readonly)

Returns the value of attribute tags.



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

def tags
  @tags
end

Instance Method Details

#report(agent) ⇒ Object



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ruby-metrics/reporters/opentsdb.rb', line 49

def report(agent)
  agent.instruments.clone.each do |name, instrument|
    case instrument
      when Metrics::Instruments::Counter
        send_data :name  => "#{name}",
                  :value => instrument.to_i,
                  :tags  => instrument.tags,
                  :units => instrument.units

      when Metrics::Instruments::Gauge
        if instrument.get.is_a? Hash
          instrument.get.each do |key, value|
            send_data :name  => "#{name}.#{key}",
                      :value => value,
                      :tags  => instrument.tags,
                      :units => instrument.units
          end
        else 
          send_data :name  => "#{name}",
                    :value => instrument.get,
                    :tags  => instrument.tags,
                    :units => instrument.units
        end
      when Metrics::Instruments::Timer
        rate_units = "#{instrument.units}/sec"
        time_units = "sec/#{instrument.units}"

        send_data :name  => "#{name}.count",
                  :value => instrument.count,
                  :tags  => instrument.tags,
                  :units => instrument.units

        [:fifteen_minute_rate, :five_minute_rate, :one_minute_rate].each do |attribute|
          send_data :name  => "#{name}.#{attribute}",
                    :value => instrument.send(attribute),
                    :tags  => instrument.tags,
                    :units => rate_units
        end

        [:min, :max, :mean].each do |attribute|
          send_data :name  => "#{name}.#{attribute}",
                    :value => instrument.send(attribute),
                    :tags  => instrument.tags,
                    :units => time_units
        end
      when Metrics::Instruments::Meter
        send_data :name  => "#{name}.count",
                  :value => instrument.counted,
                  :tags  => instrument.tags,
                  :units => instrument.units

        rate_units = "#{instrument.units}/sec"
        [:fifteen_minute_rate, :five_minute_rate, :one_minute_rate, :mean_rate].each do |attribute|
          send_data :name  => "#{name}.#{attribute}",
                    :value => instrument.send(attribute),
                    :tags  => instrument.tags,
                    :units => rate_units
        end
      else
        puts 'Unhandled instrument'
    end
  end
end

#send_data(opts = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby-metrics/reporters/opentsdb.rb', line 23

def send_data(opts = {})
  name = opts[:name]
  value = opts[:value]
  units = opts[:units]
  extra_tags = opts[:tags]

  tsdb_data = {
    :metric => name,
    :timestamp => Time.now.to_i,
    :value => value,
    :tags => @tags
  }

  if units
    tsdb_data[:tags][:units] = units
  else
    tsdb_data[:tags][:units] = 'none'
  end

  if extra_tags
    tsdb_data[:tags].merge!(extra_tags)
  end

  @client.put(tsdb_data)
end