Skip to content

Commit

Permalink
Ignore fields with NaN or Inf floats in the JSON serializer (#7426)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson authored Apr 28, 2020
1 parent 7ee776d commit c4e9f72
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
23 changes: 21 additions & 2 deletions plugins/serializers/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package json

import (
"encoding/json"
"math"
"time"

"github.com/influxdata/telegraf"
Expand Down Expand Up @@ -49,8 +50,26 @@ func (s *serializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error) {

func (s *serializer) createObject(metric telegraf.Metric) map[string]interface{} {
m := make(map[string]interface{}, 4)
m["tags"] = metric.Tags()
m["fields"] = metric.Fields()

tags := make(map[string]string, len(metric.TagList()))
for _, tag := range metric.TagList() {
tags[tag.Key] = tag.Value
}
m["tags"] = tags

fields := make(map[string]interface{}, len(metric.FieldList()))
for _, field := range metric.FieldList() {
switch fv := field.Value.(type) {
case float64:
// JSON does not support these special values
if math.IsNaN(fv) || math.IsInf(fv, 0) {
continue
}
}
fields[field.Key] = field.Value
}
m["fields"] = fields

m["name"] = metric.Name()
m["timestamp"] = metric.Time().UnixNano() / int64(s.TimestampUnits)
return m
Expand Down
46 changes: 43 additions & 3 deletions plugins/serializers/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package json

import (
"fmt"
"math"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func MustMetric(v telegraf.Metric, err error) telegraf.Metric {
Expand Down Expand Up @@ -193,3 +194,42 @@ func TestSerializeBatch(t *testing.T) {
require.NoError(t, err)
require.Equal(t, []byte(`{"metrics":[{"fields":{"value":42},"name":"cpu","tags":{},"timestamp":0},{"fields":{"value":42},"name":"cpu","tags":{},"timestamp":0}]}`), buf)
}

func TestSerializeBatchSkipInf(t *testing.T) {
metrics := []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"inf": math.Inf(1),
"time_idle": 42,
},
time.Unix(0, 0),
),
}

s, err := NewSerializer(0)
require.NoError(t, err)
buf, err := s.SerializeBatch(metrics)
require.NoError(t, err)
require.Equal(t, []byte(`{"metrics":[{"fields":{"time_idle":42},"name":"cpu","tags":{},"timestamp":0}]}`), buf)
}

func TestSerializeBatchSkipInfAllFields(t *testing.T) {
metrics := []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"inf": math.Inf(1),
},
time.Unix(0, 0),
),
}

s, err := NewSerializer(0)
require.NoError(t, err)
buf, err := s.SerializeBatch(metrics)
require.NoError(t, err)
require.Equal(t, []byte(`{"metrics":[{"fields":{},"name":"cpu","tags":{},"timestamp":0}]}`), buf)
}

0 comments on commit c4e9f72

Please sign in to comment.