Skip to content

Commit

Permalink
Don't use panic-happy prometheus client With() function
Browse files Browse the repository at this point in the history
Deals with part of #405, although it doesn't deal with the underlying
label error that is occuring.
  • Loading branch information
sparrc committed Dec 1, 2015
1 parent ca222a1 commit 2be7fc0
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions outputs/prometheus_client/prometheus_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package prometheus_client

import (
"fmt"
"log"
"net/http"

"github.com/influxdb/influxdb/client/v2"
Expand Down Expand Up @@ -64,8 +65,7 @@ func (p *PrometheusClient) Write(points []*client.Point) error {

for _, point := range points {
var labels []string
name := point.Name()
key := name
key := point.Name()

for k, _ := range point.Tags() {
if len(k) > 0 {
Expand All @@ -77,7 +77,7 @@ func (p *PrometheusClient) Write(points []*client.Point) error {
p.metrics[key] = prometheus.NewUntypedVec(
prometheus.UntypedOpts{
Name: key,
Help: fmt.Sprintf("Telegraf collected point '%s'", name),
Help: fmt.Sprintf("Telegraf collected point '%s'", key),
},
labels,
)
Expand All @@ -90,12 +90,28 @@ func (p *PrometheusClient) Write(points []*client.Point) error {
}

for _, val := range point.Fields() {
switch val.(type) {
switch val := val.(type) {
default:
log.Printf("Prometheus output, unsupported type. key: %s, type: %T\n",
key, val)
case int64:
ival := val.(int64)
p.metrics[key].With(l).Set(float64(ival))
m, err := p.metrics[key].GetMetricWith(l)
if err != nil {
log.Printf("ERROR Getting metric in Prometheus output, "+
"key: %s, labels: %v,\nerr: %s\n",
key, l, err.Error())
continue
}
m.Set(float64(val))
case float64:
p.metrics[key].With(l).Set(val.(float64))
m, err := p.metrics[key].GetMetricWith(l)
if err != nil {
log.Printf("ERROR Getting metric in Prometheus output, "+
"key: %s, labels: %v,\nerr: %s\n",
key, l, err.Error())
continue
}
m.Set(val)
}
}
}
Expand Down

0 comments on commit 2be7fc0

Please sign in to comment.