fix(json_v2): use raw values for timestamps (#10413)

This commit is contained in:
Sebastian Spaink 2022-01-11 10:08:59 -06:00 committed by GitHub
parent 57948ccdcd
commit 18704de6ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 3 deletions

View File

@ -119,7 +119,7 @@ func (p *Parser) Parse(input []byte) ([]telegraf.Metric, error) {
}
var err error
p.timestamp, err = internal.ParseTimestamp(c.TimestampFormat, result.Value(), c.TimestampTimezone)
p.timestamp, err = internal.ParseTimestamp(c.TimestampFormat, result.Raw, c.TimestampTimezone)
if err != nil {
return nil, err
}
@ -318,7 +318,7 @@ func (p *Parser) expandArray(result MetricNode) ([]telegraf.Metric, error) {
err := fmt.Errorf("use of 'timestamp_query' requires 'timestamp_format'")
return nil, err
}
timestamp, err := internal.ParseTimestamp(p.objectConfig.TimestampFormat, result.Value(), p.objectConfig.TimestampTimezone)
timestamp, err := internal.ParseTimestamp(p.objectConfig.TimestampFormat, result.Raw, p.objectConfig.TimestampTimezone)
if err != nil {
return nil, err
}

View File

@ -5,7 +5,9 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
@ -47,7 +49,18 @@ func TestMultipleConfigs(t *testing.T) {
// Process expected metrics and compare with resulting metrics
expectedOutputs, err := readMetricFile(fmt.Sprintf("testdata/%s/expected.out", f.Name()))
require.NoError(t, err)
testutil.RequireMetricsEqual(t, expectedOutputs, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
resultingMetrics := acc.GetTelegrafMetrics()
testutil.RequireMetricsEqual(t, expectedOutputs, resultingMetrics, testutil.IgnoreTime())
// Folder with timestamp prefixed will also check for matching timestamps to make sure they are parsed correctly
// The milliseconds weren't matching, seemed like a rounding difference between the influx parser
// Compares each metrics times separately and ignores milliseconds
if strings.HasPrefix(f.Name(), "timestamp") {
require.Equal(t, len(expectedOutputs), len(resultingMetrics))
for i, m := range resultingMetrics {
require.Equal(t, expectedOutputs[i].Time().Truncate(time.Second), m.Time().Truncate(time.Second))
}
}
})
}
}
@ -66,6 +79,8 @@ func readMetricFile(path string) ([]telegraf.Metric, error) {
line := scanner.Text()
if line != "" {
m, err := parser.ParseLine(line)
// The timezone needs to be UTC to match the timestamp test results
m.SetTime(m.Time().UTC())
if err != nil {
return nil, fmt.Errorf("unable to parse metric in %q failed: %v", line, err)
}

View File

@ -0,0 +1,2 @@
test value=0 1631202459121654321
test value=1 1631202459121654321

View File

@ -0,0 +1,7 @@
{
"test": [
{ "value": 0 },
{ "value": 1 }
],
"timestamp": 1631202459121654321
}

View File

@ -0,0 +1,11 @@
# Example taken from: https://github.com/influxdata/telegraf/issues/5940
[[inputs.file]]
files = ["./testdata/timestamp_ns/input.json"]
data_format = "json_v2"
[[inputs.file.json_v2]]
measurement_name = "test"
timestamp_path = "timestamp"
timestamp_format = "unix_ns"
[[inputs.file.json_v2.object]]
path = "test"