fix(json_v2): use raw values for timestamps (#10413)
This commit is contained in:
parent
57948ccdcd
commit
18704de6ed
|
|
@ -119,7 +119,7 @@ func (p *Parser) Parse(input []byte) ([]telegraf.Metric, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var err 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 {
|
if err != nil {
|
||||||
return nil, err
|
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'")
|
err := fmt.Errorf("use of 'timestamp_query' requires 'timestamp_format'")
|
||||||
return nil, err
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,9 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/config"
|
"github.com/influxdata/telegraf/config"
|
||||||
|
|
@ -47,7 +49,18 @@ func TestMultipleConfigs(t *testing.T) {
|
||||||
// Process expected metrics and compare with resulting metrics
|
// Process expected metrics and compare with resulting metrics
|
||||||
expectedOutputs, err := readMetricFile(fmt.Sprintf("testdata/%s/expected.out", f.Name()))
|
expectedOutputs, err := readMetricFile(fmt.Sprintf("testdata/%s/expected.out", f.Name()))
|
||||||
require.NoError(t, err)
|
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()
|
line := scanner.Text()
|
||||||
if line != "" {
|
if line != "" {
|
||||||
m, err := parser.ParseLine(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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to parse metric in %q failed: %v", line, err)
|
return nil, fmt.Errorf("unable to parse metric in %q failed: %v", line, err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
test value=0 1631202459121654321
|
||||||
|
test value=1 1631202459121654321
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"test": [
|
||||||
|
{ "value": 0 },
|
||||||
|
{ "value": 1 }
|
||||||
|
],
|
||||||
|
"timestamp": 1631202459121654321
|
||||||
|
}
|
||||||
|
|
@ -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"
|
||||||
Loading…
Reference in New Issue