Class: Cloudscale::Monitor::InfluxDbReporter

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/cloudscale/monitor/reporter/influxdb_reporter.rb

Constant Summary collapse

@@iDB =
Constants::InfluxDb.load
@@COLUMNS_TIMER =
Array["time", "count", "min", "max", "mean", "std-dev",
   "50-percentile", "75-percentile", "95-percentile", "99-percentile", "999-percentile",
   "one-minute", "five-minute", "fifteen-minute", "mean-rate",
   "run-count"
]
@@COLUMNS_HISTOGRAM =
Array["time", "count", "min", "max", "mean", "std-dev",
   "50-percentile", "75-percentile", "95-percentile", "99-percentile", "999-percentile",
   "run-count"
]
@@COLUMNS_COUNT =
Array["time", "count"]
@@COLUMNS_GAUGE =
Array["time", "value"]
@@COLUMNS_METER =
Array["time", "count", "one-minute", "five-minute", "fifteen-minute", "mean"]
@@pointsTimer =
[[
  0,
  0,
  0.0,
  0.0,
  0.0,
  0.0,
  0.0,
  0.0,
  0.0,
  0.0,
  0.0,
  0.0,
  0.0,
  0.0,
  0,
  0
]]
@@pointsHistogram =
[[
  0,
  0,
  0.0,
  0.0,
  0.0,
  0.0,
  0.0,
  0.0,
  0.0,
  0.0,
  0,
  0
]]
@@pointsCounter =
[[
  0,
  0
]]
@@pointsGauge =
[[
  0,
  0
]]
@@pointsMeter =
[[
  0,
  0,
  0.0,
  0.0,
  0.0,
  0.0
]]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInfluxDbReporter

Returns a new instance of InfluxDbReporter.



92
93
94
95
96
# File 'lib/cloudscale/monitor/reporter/influxdb_reporter.rb', line 92

def initialize
  @prefix = Monitor::Registry.instance.agent_instance_id + "."
  @client = InfluxDB::Client.new @@iDB["database"], { :host => @@iDB["host"], :port => @@iDB["port"], :username => @@iDB["username"],
    :password => @@iDB["password"] }
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



16
17
18
# File 'lib/cloudscale/monitor/reporter/influxdb_reporter.rb', line 16

def client
  @client
end

#datasetObject (readonly)

Returns the value of attribute dataset.



16
17
18
# File 'lib/cloudscale/monitor/reporter/influxdb_reporter.rb', line 16

def dataset
  @dataset
end

#prefixObject (readonly)

Returns the value of attribute prefix.



16
17
18
# File 'lib/cloudscale/monitor/reporter/influxdb_reporter.rb', line 16

def prefix
  @prefix
end

Instance Method Details

#appendSeries(namePrefix, name, nameSuffix, columns, points) ⇒ Object



189
190
191
192
193
194
195
196
# File 'lib/cloudscale/monitor/reporter/influxdb_reporter.rb', line 189

def appendSeries(namePrefix, name, nameSuffix, columns, points)
  data = Hash.new
  columns.each_with_index { | val, index |
    data[columns[index]] = points[index]
  }

  @dataset.push({:name => namePrefix + name + nameSuffix, :data => data})
end

#clearObject



203
204
205
# File 'lib/cloudscale/monitor/reporter/influxdb_reporter.rb', line 203

def clear()
  @dataset = Array.new
end

#hasSeriesObject



207
208
209
# File 'lib/cloudscale/monitor/reporter/influxdb_reporter.rb', line 207

def hasSeries()
  return !@dataset.empty?
end

#logObject



18
19
20
# File 'lib/cloudscale/monitor/reporter/influxdb_reporter.rb', line 18

def log
  @log = Logger.new(STDOUT)
end

#report(metrics) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cloudscale/monitor/reporter/influxdb_reporter.rb', line 98

def report(metrics)
  clear

  timestamp = Time.now.to_i
  metrics.each do | key, metric |
    case metric
    when Metrics::Instruments::Counter
      reportCounter(key, metric, timestamp)
    when Metrics::Instruments::Gauge
      reportGauge(key, metric, timestamp)
    when Metrics::Instruments::Timer
      reportTimer(key, metric, timestamp)
    when Metrics::Instruments::Histogram
      reportHistogram(key, metric, timestamp)
    when Metrics::Instruments::Meter
      reportMeter(key, metric, timestamp)
    else
      puts 'Unhandled instrument'
    end
  end

  if (hasSeries)
    send
  end
end

#reportCounter(name, counter, timestamp) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/cloudscale/monitor/reporter/influxdb_reporter.rb', line 162

def reportCounter(name, counter, timestamp)
  p = @@pointsCounter[0]
  p[0] = timestamp
  p[1] = counter.to_i
  raise "counter dataset not equals column length" unless p.length == @@COLUMNS_COUNT.length
  appendSeries(@prefix, name, ".count", @@COLUMNS_COUNT, p)
end

#reportGauge(name, gauge, timestamp) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/cloudscale/monitor/reporter/influxdb_reporter.rb', line 170

def reportGauge(name, gauge, timestamp)
  p = @@pointsGauge[0]
  p[0] = timestamp
  p[1] = gauge.get[:value]
  raise "gauge dataset not equals column length" unless p.length == @@COLUMNS_GAUGE.length
  appendSeries(@prefix, name, ".value", @@COLUMNS_GAUGE, p)
end

#reportHistogram(name, histogram, timestamp) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/cloudscale/monitor/reporter/influxdb_reporter.rb', line 145

def reportHistogram(name, histogram, timestamp)
   p = @@pointsHistogram[0]
 p[0] = timestamp
 p[1] = histogram.size
 p[2] = histogram.min
  p[3] = histogram.max
  p[4] = histogram.mean
  p[5] = histogram.std_dev
  p[6] = 0
  p[7] = histogram.quantiles(0.75)
  p[8] = histogram.quantiles(0.95)
  p[9] = histogram.quantiles(0.99)
  p[11] = histogram.one_minute_rate
   raise "histogram dataset not equals column length" unless p.length == @@COLUMNS_HISTOGRAM.length
   appendSeries(@prefix, name, ".histogram", @@COLUMNS_HISTOGRAM, p)
end

#reportMeter(name, meter, timestamp) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/cloudscale/monitor/reporter/influxdb_reporter.rb', line 178

def reportMeter(name, meter, timestamp)
  p = @@pointsMeter[0]
  p[1] = 0
p[2] = meter.one_minute_rate
p[3] = meter.five_minute_rate
p[4] = meter.fifteen_minute_rate
p[5] = meter.mean_rate
  raise "meter dataset not equals column length" unless p.length == @@COLUMNS_METER.length
  appendSeries(@prefix, name, ".value", @@COLUMNS_METER, p)
end

#reportTimer(name, timer, timestamp) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cloudscale/monitor/reporter/influxdb_reporter.rb', line 124

def reportTimer(name, timer, timestamp)
   p = @@pointsTimer[0]
 p[0] = timestamp
 p[1] = timer.count
 p[2] = timer.min
  p[3] = timer.max
  p[4] = timer.mean
  p[5] = timer.std_dev
  p[6] = 0
  p[7] = timer.quantiles(0.75)
  p[8] = timer.quantiles(0.95)
  p[9] = timer.quantiles(0.99)
  p[11] = timer.one_minute_rate
  p[12] = timer.five_minute_rate
  p[13] = timer.fifteen_minute_rate
  p[14] = timer.mean_rate
  p[15] = timer.count
   raise "timer dataset not equals column length" unless p.length == @@COLUMNS_TIMER.length
   appendSeries(@prefix, name, ".timer", @@COLUMNS_TIMER, p)
end

#sendObject



198
199
200
201
# File 'lib/cloudscale/monitor/reporter/influxdb_reporter.rb', line 198

def send()        
  log.info("Dataset length: #{@dataset.length}. Now sending...")
  @client.write_points(@dataset, false)
end