Class: MetriksAddons::OpenTSDBReporter

Inherits:
BaseReporter show all
Defined in:
lib/metriks-addons/opentsdb_reporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseReporter

#flush, #log, #restart, #start, #stop

Constructor Details

#initialize(h, t, options = {}) ⇒ OpenTSDBReporter

Returns a new instance of OpenTSDBReporter.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 25

def initialize(h, t, options = {})
  super(options)
  @hostname = h
  @tags = t

  @prefix = options[:prefix]
  @source = options[:source]

  @batch_size   = options[:batch_size] || 50

  if not @logger.nil?
    RestClient.log =
      Object.new.tap do |proxy|
        def proxy.<<(message)
          Rails.logger.debug message
        end
      end
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



23
24
25
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 23

def data
  @data
end

#hostnameObject

Returns the value of attribute hostname.



23
24
25
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 23

def hostname
  @hostname
end

#loggerObject

Returns the value of attribute logger.



23
24
25
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 23

def logger
  @logger
end

#prefixObject

Returns the value of attribute prefix.



23
24
25
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 23

def prefix
  @prefix
end

#sourceObject

Returns the value of attribute source.



23
24
25
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 23

def source
  @source
end

#tagsObject

Returns the value of attribute tags.



23
24
25
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 23

def tags
  @tags
end

Instance Method Details

#create_datapoints(base_name, metric, time, keys, snapshot_keys = []) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 122

def create_datapoints(base_name, metric, time, keys, snapshot_keys = [])
  datapoints = []
  keys.flatten.each do |key|
    name = key.to_s.gsub(/^get_/, '')
    datapoints << {
      :metric => "#{base_name}.#{name}",
      :timestamp => time,
      :value => metric.send(key),
      :tags => @tags
    }
  end

  unless snapshot_keys.empty?
    snapshot = metric.snapshot
    snapshot_keys.flatten.each do |key|
      name = key.to_s.gsub(/^get_/, '')
      datapoints << {
        :metric => "#{base_name}.#{name}",
        :timestamp => time,
        :value => snapshot.send(key),
        :tags => @tags
      }
    end
  end
  datapoints
end

#get_datapointsObject



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
112
113
114
115
116
117
118
119
120
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 67

def get_datapoints
  time = @time_tracker.now_floored

  datapoints = []
  @registry.each do |name, metric|
    next if name.nil? || name.empty?
    name = name.to_s.gsub(/ +/, '_')
    if @prefix
      name = "#{@prefix}.#{name}"
    end

    case metric
    when Metriks::Meter
      datapoints |= create_datapoints name, metric, time, [
        :count, :one_minute_rate, :five_minute_rate,
        :fifteen_minute_rate, :mean_rate
      ]
    when Metriks::Counter
      datapoints |= create_datapoints name, metric, time, [
        :count
      ]
    when Metriks::Gauge
      datapoints |= create_datapoints name, metric, time, [
        :value
      ]
    when Metriks::UtilizationTimer
      datapoints |= create_datapoints name, metric, time, [
        :count, :one_minute_rate, :five_minute_rate,
        :fifteen_minute_rate, :mean_rate,
        :min, :max, :mean, :stddev,
        :one_minute_utilization, :five_minute_utilization,
        :fifteen_minute_utilization, :mean_utilization,
      ], [
        :median, :get_95th_percentile
      ]

      when Metriks::Timer
      datapoints |= create_datapoints name, metric, time, [
        :count, :one_minute_rate, :five_minute_rate,
        :fifteen_minute_rate, :mean_rate,
        :min, :max, :mean, :stddev
      ], [
        :median, :get_95th_percentile
      ]
      when Metriks::Histogram
      datapoints |= create_datapoints name, metric, time, [
        :count, :min, :max, :mean, :stddev
      ], [
        :median, :get_95th_percentile
      ]
    end
  end
  datapoints
end

#submit(datapoints) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 45

def submit(datapoints)
  return if datapoints.empty?

  index = 0
  length = @batch_size
  while index < datapoints.size
    to_send = nil
    if datapoints.size < (index + length)
      length = datapoints.size - index
    else
      length = @batch_size
    end
    jsonstr = datapoints[index, length].to_json
    RestClient.post "#{@hostname}/api/put",
      jsonstr,
      :content_type => :json, :accept => :json
    log "debug", "Sent #{length} metrics from #{index}"
    index += length
  end
  log "info", "Sent #{datapoints.size} metrics to OpenTSDB"
end