Support string type for val. Convert it to int or float if smart_strings opt if true

This commit is contained in:
ilya 2016-03-23 16:18:37 +03:00
parent a95710ed0c
commit 8151510f20
1 changed files with 39 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import (
"log" "log"
"net/http" "net/http"
"regexp" "regexp"
"strconv"
"strings" "strings"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
@ -25,13 +26,15 @@ var (
) )
type PrometheusClient struct { type PrometheusClient struct {
Listen string Listen string
metrics map[string]*prometheus.UntypedVec SmartStrings bool
metrics map[string]*prometheus.UntypedVec
} }
var sampleConfig = ` var sampleConfig = `
## Address to listen on ## Address to listen on
# listen = ":9126" # listen = ":9126"
# smart_strings = true
` `
func (p *PrometheusClient) Start() error { func (p *PrometheusClient) Start() error {
@ -126,6 +129,40 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
default: default:
log.Printf("Prometheus output, unsupported type. key: %s, type: %T\n", log.Printf("Prometheus output, unsupported type. key: %s, type: %T\n",
mname, val) 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: case int64:
m, err := p.metrics[mname].GetMetricWith(l) m, err := p.metrics[mname].GetMetricWith(l)
if err != nil { if err != nil {