Remove the time_key from the field values in JSON parser (#4951)
This commit is contained in:
parent
02ad1f46be
commit
ad320ac1e0
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue