@@ -49,6 +49,7 @@ type Metric struct {
49
49
Labels []string
50
50
MetricsDesc map [string ]string
51
51
MetricsType map [string ]string
52
+ MetricsBuckets map [string ]map [string ]string
52
53
FieldToAppend string
53
54
Request string
54
55
IgnoreZeroResult bool
@@ -225,6 +226,7 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
225
226
log .Debugln ("- Metric MetricsDesc: " , metric .MetricsDesc )
226
227
log .Debugln ("- Metric Context: " , metric .Context )
227
228
log .Debugln ("- Metric MetricsType: " , metric .MetricsType )
229
+ log .Debugln ("- Metric MetricsBuckets: " , metric .MetricsBuckets , "(Ignored unless Histogram type)" )
228
230
log .Debugln ("- Metric Labels: " , metric .Labels )
229
231
log .Debugln ("- Metric FieldToAppend: " , metric .FieldToAppend )
230
232
log .Debugln ("- Metric IgnoreZeroResult: " , metric .IgnoreZeroResult )
@@ -240,6 +242,8 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
240
242
return
241
243
}
242
244
245
+ // FIXME - Should check that a MetricBuckets entry exists for everything of Histogram type
246
+
243
247
if err = ScrapeMetric (e .db , ch , metric ); err != nil {
244
248
log .Errorln ("Error scraping for" , metric .Context , "_" , metric .MetricsDesc , ":" , err )
245
249
e .scrapeErrors .WithLabelValues (metric .Context ).Inc ()
@@ -255,6 +259,7 @@ func GetMetricType(metricType string, metricsType map[string]string) prometheus.
255
259
var strToPromType = map [string ]prometheus.ValueType {
256
260
"gauge" : prometheus .GaugeValue ,
257
261
"counter" : prometheus .CounterValue ,
262
+ "histogram" : prometheus .UntypedValue ,
258
263
}
259
264
260
265
strType , ok := metricsType [strings .ToLower (metricType )]
@@ -272,14 +277,14 @@ func GetMetricType(metricType string, metricsType map[string]string) prometheus.
272
277
func ScrapeMetric (db * sql.DB , ch chan <- prometheus.Metric , metricDefinition Metric ) error {
273
278
log .Debugln ("Calling function ScrapeGenericValues()" )
274
279
return ScrapeGenericValues (db , ch , metricDefinition .Context , metricDefinition .Labels ,
275
- metricDefinition .MetricsDesc , metricDefinition .MetricsType ,
280
+ metricDefinition .MetricsDesc , metricDefinition .MetricsType , metricDefinition . MetricsBuckets ,
276
281
metricDefinition .FieldToAppend , metricDefinition .IgnoreZeroResult ,
277
282
metricDefinition .Request )
278
283
}
279
284
280
285
// generic method for retrieving metrics.
281
286
func ScrapeGenericValues (db * sql.DB , ch chan <- prometheus.Metric , context string , labels []string ,
282
- metricsDesc map [string ]string , metricsType map [string ]string , fieldToAppend string , ignoreZeroResult bool , request string ) error {
287
+ metricsDesc map [string ]string , metricsType map [string ]string , metricsBuckets map [ string ] map [ string ] string , fieldToAppend string , ignoreZeroResult bool , request string ) error {
283
288
metricsCount := 0
284
289
genericParser := func (row map [string ]string ) error {
285
290
// Construct labels value
@@ -304,15 +309,67 @@ func ScrapeGenericValues(db *sql.DB, ch chan<- prometheus.Metric, context string
304
309
metricHelp ,
305
310
labels , nil ,
306
311
)
307
- ch <- prometheus .MustNewConstMetric (desc , GetMetricType (metric , metricsType ), value , labelsValues ... )
312
+ if metricsType [strings .ToLower (metric )] == "histogram" {
313
+ count , err := strconv .ParseUint (strings .TrimSpace (row ["count" ]), 10 , 64 )
314
+ if err != nil {
315
+ log .Errorln ("Unable to convert count value to int (metric=" + metric +
316
+ ",metricHelp=" + metricHelp + ",value=<" + row ["count" ] + ">)" )
317
+ continue
318
+ }
319
+ buckets := make (map [float64 ]uint64 )
320
+ for field , le := range metricsBuckets [metric ] {
321
+ lelimit , err := strconv .ParseFloat (strings .TrimSpace (le ), 64 )
322
+ if err != nil {
323
+ log .Errorln ("Unable to convert bucket limit value to float (metric=" + metric +
324
+ ",metricHelp=" + metricHelp + ",bucketlimit=<" + le + ">)" )
325
+ continue
326
+ }
327
+ counter , err := strconv .ParseUint (strings .TrimSpace (row [field ]), 10 , 64 )
328
+ if err != nil {
329
+ log .Errorln ("Unable to convert " , field , " value to int (metric=" + metric +
330
+ ",metricHelp=" + metricHelp + ",value=<" + row [field ] + ">)" )
331
+ continue
332
+ }
333
+ buckets [lelimit ] = counter
334
+ }
335
+ ch <- prometheus .MustNewConstHistogram (desc , count , value , buckets , labelsValues ... )
336
+ } else {
337
+ ch <- prometheus .MustNewConstMetric (desc , GetMetricType (metric , metricsType ), value , labelsValues ... )
338
+ }
308
339
// If no labels, use metric name
309
340
} else {
310
341
desc := prometheus .NewDesc (
311
342
prometheus .BuildFQName (namespace , context , cleanName (row [fieldToAppend ])),
312
343
metricHelp ,
313
344
nil , nil ,
314
345
)
315
- ch <- prometheus .MustNewConstMetric (desc , GetMetricType (metric , metricsType ), value )
346
+ if metricsType [strings .ToLower (metric )] == "histogram" {
347
+ count , err := strconv .ParseUint (strings .TrimSpace (row ["count" ]), 10 , 64 )
348
+ if err != nil {
349
+ log .Errorln ("Unable to convert count value to int (metric=" + metric +
350
+ ",metricHelp=" + metricHelp + ",value=<" + row ["count" ] + ">)" )
351
+ continue
352
+ }
353
+ buckets := make (map [float64 ]uint64 )
354
+ for field , le := range metricsBuckets [metric ] {
355
+ lelimit , err := strconv .ParseFloat (strings .TrimSpace (le ), 64 )
356
+ if err != nil {
357
+ log .Errorln ("Unable to convert bucket limit value to float (metric=" + metric +
358
+ ",metricHelp=" + metricHelp + ",bucketlimit=<" + le + ">)" )
359
+ continue
360
+ }
361
+ counter , err := strconv .ParseUint (strings .TrimSpace (row [field ]), 10 , 64 )
362
+ if err != nil {
363
+ log .Errorln ("Unable to convert " , field , " value to int (metric=" + metric +
364
+ ",metricHelp=" + metricHelp + ",value=<" + row [field ] + ">)" )
365
+ continue
366
+ }
367
+ buckets [lelimit ] = counter
368
+ }
369
+ ch <- prometheus .MustNewConstHistogram (desc , count , value , buckets )
370
+ } else {
371
+ ch <- prometheus .MustNewConstMetric (desc , GetMetricType (metric , metricsType ), value )
372
+ }
316
373
}
317
374
metricsCount ++
318
375
}
0 commit comments