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"
"net/http"
"regexp"
"strconv"
"strings"
"github.com/influxdata/telegraf"
@ -26,12 +27,14 @@ var (
type PrometheusClient struct {
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 {