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.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 10

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.



8
9
10
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 8

def data
  @data
end

#hostnameObject

Returns the value of attribute hostname.



8
9
10
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 8

def hostname
  @hostname
end

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 8

def logger
  @logger
end

#prefixObject

Returns the value of attribute prefix.



8
9
10
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 8

def prefix
  @prefix
end

#sourceObject

Returns the value of attribute source.



8
9
10
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 8

def source
  @source
end

#tagsObject

Returns the value of attribute tags.



8
9
10
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 8

def tags
  @tags
end

Instance Method Details

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 107

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



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
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 52

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/metriks-addons/opentsdb_reporter.rb', line 30

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