From 8151510f2013a9708d3b576eacec56b430314a05 Mon Sep 17 00:00:00 2001 From: ilya Date: Wed, 23 Mar 2016 16:18:37 +0300 Subject: [PATCH] Support string type for val. Convert it to int or float if smart_strings opt if true --- .../prometheus_client/prometheus_client.go | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/plugins/outputs/prometheus_client/prometheus_client.go b/plugins/outputs/prometheus_client/prometheus_client.go index 79a838304..af290563a 100644 --- a/plugins/outputs/prometheus_client/prometheus_client.go +++ b/plugins/outputs/prometheus_client/prometheus_client.go @@ -5,6 +5,7 @@ import ( "log" "net/http" "regexp" + "strconv" "strings" "github.com/influxdata/telegraf" @@ -25,13 +26,15 @@ var ( ) type PrometheusClient struct { - Listen string - metrics map[string]*prometheus.UntypedVec + Listen string + SmartStrings bool + metrics map[string]*prometheus.UntypedVec } var sampleConfig = ` ## Address to listen on # listen = ":9126" + # smart_strings = true ` func (p *PrometheusClient) Start() error { @@ -126,6 +129,40 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error { default: log.Printf("Prometheus output, unsupported type. key: %s, type: %T\n", mname, val) + case string: + if !p.SmartStrings { + log.Printf("Prometheus output, unsupported type. key: %s, label: %s, type: %T\n", + mname, l, val) + } + // Get metric value + m, err := p.metrics[mname].GetMetricWith(l) + if err != nil { + log.Printf("ERROR Getting metric in Prometheus output, "+ + "key: %s, labels: %v,\nerr: %s\n", + mname, l, err.Error()) + continue + } + + // If has dot in val - parse as float, else int + if strings.Contains(val, ".") { + // Float + tval, err := strconv.ParseFloat(val, 64) + if err != nil { + log.Printf("Prometheus output, can't convert string to float. key: %s, label: %s, val: %s\n", + mname, l, val) + continue + } + m.Set(tval) + } else { + // Int + tval, err := strconv.ParseInt(val, 10, 64) + if err != nil { + log.Printf("Prometheus output, can't convert string to int. key: %s, label: %s, val: %s\n", + mname, l, val) + continue + } + m.Set(float64(tval)) + } case int64: m, err := p.metrics[mname].GetMetricWith(l) if err != nil {