Remove the time_key from the field values in JSON parser (#4951)

This commit is contained in:
Daniel Nelson 2018-11-02 17:53:45 -07:00 committed by GitHub
parent 02ad1f46be
commit ad320ac1e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 0 deletions

View File

@ -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 the year is 0, set to current year
if nTime.Year() == 0 { if nTime.Year() == 0 {
nTime = nTime.AddDate(time.Now().Year(), 0, 0) nTime = nTime.AddDate(time.Now().Year(), 0, 0)

View File

@ -4,7 +4,10 @@ import (
"fmt" "fmt"
"log" "log"
"testing" "testing"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -724,3 +727,27 @@ func TestNameKey(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "this is my name", metrics[0].Name()) 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)
}