Skip to content

Commit

Permalink
Skip measurements with NaN fields
Browse files Browse the repository at this point in the history
fixes #389
  • Loading branch information
sparrc committed Nov 23, 2015
1 parent 970bfce commit 1a1406b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
25 changes: 17 additions & 8 deletions accumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package telegraf
import (
"fmt"
"log"
"math"
"sync"
"time"

Expand Down Expand Up @@ -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) {
if ac.debug {
log.Printf("Measurement [%s] has a NaN field, skipping",
measurement)
}
return
}
}
}

if tags == nil {
tags = make(map[string]string)
}

var timestamp time.Time
if len(t) > 0 {
timestamp = t[0]
Expand All @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion plugins/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 1a1406b

Please sign in to comment.