Skip to content

Commit

Permalink
Remove the time_key from the field values in JSON parser (influxdata#…
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson authored and Jean-Louis Dupond committed Apr 22, 2019
1 parent 748e214 commit 7a7234e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions plugins/parsers/json/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions plugins/parsers/json/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"fmt"
"log"
"testing"
"time"

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

Expand Down Expand Up @@ -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)
}

0 comments on commit 7a7234e

Please sign in to comment.