Skip measurements with NaN fields

fixes #389
This commit is contained in:
Cameron Sparr 2015-11-23 14:16:20 -07:00
parent 970bfce997
commit 317a352a65
2 changed files with 19 additions and 9 deletions

View File

@ -3,6 +3,7 @@ package telegraf
import (
"fmt"
"log"
"math"
"sync"
"time"
@ -66,25 +67,32 @@ func (ac *accumulator) AddFields(
tags map[string]string,
t ...time.Time,
) {
if tags == nil {
tags = make(map[string]string)
}
// InfluxDB client/points does not support writing uint64
// TODO fix when it does
// https://github.com/influxdb/influxdb/pull/4508
// Validate uint64 and float64 fields
for k, v := range fields {
switch val := v.(type) {
case uint64:
// InfluxDB does not support writing uint64
if val < uint64(9223372036854775808) {
fields[k] = int64(val)
} else {
fields[k] = int64(9223372036854775807)
}
case float64:
// NaNs are invalid values in influxdb, skip measurement
if math.IsNaN(val) || math.IsInf(val, 0) {
if ac.debug {
log.Printf("Measurement [%s] has a NaN or Inf field, skipping",
measurement)
}
return
}
}
}
if tags == nil {
tags = make(map[string]string)
}
var timestamp time.Time
if len(t) > 0 {
timestamp = t[0]
@ -111,6 +119,7 @@ func (ac *accumulator) AddFields(
pt, err := client.NewPoint(measurement, tags, fields, timestamp)
if err != nil {
log.Printf("Error adding point [%s]: %s\n", measurement, err.Error())
return
}
if ac.debug {
fmt.Println("> " + pt.String())

View File

@ -87,7 +87,8 @@ func (g *Prometheus) gatherURL(url string, acc plugins.Accumulator) error {
}
tags[string(key)] = string(value)
}
acc.Add(string(sample.Metric[model.MetricNameLabel]), float64(sample.Value), tags)
acc.Add(string(sample.Metric[model.MetricNameLabel]),
float64(sample.Value), tags)
}
}