From ad320ac1e060e01994553d0fe128e2812aafdb06 Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Fri, 2 Nov 2018 17:53:45 -0700 Subject: [PATCH] Remove the time_key from the field values in JSON parser (#4951) --- plugins/parsers/json/parser.go | 2 ++ plugins/parsers/json/parser_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/plugins/parsers/json/parser.go b/plugins/parsers/json/parser.go index 1d8ce2d02..19a92275b 100644 --- a/plugins/parsers/json/parser.go +++ b/plugins/parsers/json/parser.go @@ -143,6 +143,8 @@ func (p *JSONParser) parseObject(metrics []telegraf.Metric, jsonOut map[string]i } } + delete(f.Fields, p.JSONTimeKey) + //if the year is 0, set to current year if nTime.Year() == 0 { nTime = nTime.AddDate(time.Now().Year(), 0, 0) diff --git a/plugins/parsers/json/parser_test.go b/plugins/parsers/json/parser_test.go index ec9ade251..382afcd35 100644 --- a/plugins/parsers/json/parser_test.go +++ b/plugins/parsers/json/parser_test.go @@ -4,7 +4,10 @@ import ( "fmt" "log" "testing" + "time" + "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" ) @@ -724,3 +727,27 @@ func TestNameKey(t *testing.T) { require.NoError(t, err) require.Equal(t, "this is my name", metrics[0].Name()) } + +func TestTimeKeyDelete(t *testing.T) { + data := `{ + "timestamp": 1541183052, + "value": 42 + }` + + parser := JSONParser{ + MetricName: "json", + JSONTimeKey: "timestamp", + JSONTimeFormat: "unix", + } + + metrics, err := parser.Parse([]byte(data)) + require.NoError(t, err) + expected := []telegraf.Metric{ + testutil.MustMetric("json", + map[string]string{}, + map[string]interface{}{"value": 42.0}, + time.Unix(1541183052, 0)), + } + + testutil.RequireMetricsEqual(t, expected, metrics) +}