From 317a352a657c1e51f9600b83a65b5296c5f62c8b Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Mon, 23 Nov 2015 14:16:20 -0700 Subject: [PATCH] Skip measurements with NaN fields fixes #389 --- accumulator.go | 25 +++++++++++++++++-------- plugins/prometheus/prometheus.go | 3 ++- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/accumulator.go b/accumulator.go index 78bf38dde..ae6af52e5 100644 --- a/accumulator.go +++ b/accumulator.go @@ -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()) diff --git a/plugins/prometheus/prometheus.go b/plugins/prometheus/prometheus.go index 8134f4f81..cb824e3f2 100644 --- a/plugins/prometheus/prometheus.go +++ b/plugins/prometheus/prometheus.go @@ -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) } }