Skip to content

Commit 18704de

Browse files
authored
fix(json_v2): use raw values for timestamps (#10413)
1 parent 57948cc commit 18704de

5 files changed

Lines changed: 38 additions & 3 deletions

File tree

plugins/parsers/json_v2/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (p *Parser) Parse(input []byte) ([]telegraf.Metric, error) {
119119
}
120120

121121
var err error
122-
p.timestamp, err = internal.ParseTimestamp(c.TimestampFormat, result.Value(), c.TimestampTimezone)
122+
p.timestamp, err = internal.ParseTimestamp(c.TimestampFormat, result.Raw, c.TimestampTimezone)
123123
if err != nil {
124124
return nil, err
125125
}
@@ -318,7 +318,7 @@ func (p *Parser) expandArray(result MetricNode) ([]telegraf.Metric, error) {
318318
err := fmt.Errorf("use of 'timestamp_query' requires 'timestamp_format'")
319319
return nil, err
320320
}
321-
timestamp, err := internal.ParseTimestamp(p.objectConfig.TimestampFormat, result.Value(), p.objectConfig.TimestampTimezone)
321+
timestamp, err := internal.ParseTimestamp(p.objectConfig.TimestampFormat, result.Raw, p.objectConfig.TimestampTimezone)
322322
if err != nil {
323323
return nil, err
324324
}

plugins/parsers/json_v2/parser_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"fmt"
66
"io/ioutil"
77
"os"
8+
"strings"
89
"testing"
10+
"time"
911

1012
"github.com/influxdata/telegraf"
1113
"github.com/influxdata/telegraf/config"
@@ -47,7 +49,18 @@ func TestMultipleConfigs(t *testing.T) {
4749
// Process expected metrics and compare with resulting metrics
4850
expectedOutputs, err := readMetricFile(fmt.Sprintf("testdata/%s/expected.out", f.Name()))
4951
require.NoError(t, err)
50-
testutil.RequireMetricsEqual(t, expectedOutputs, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
52+
resultingMetrics := acc.GetTelegrafMetrics()
53+
testutil.RequireMetricsEqual(t, expectedOutputs, resultingMetrics, testutil.IgnoreTime())
54+
55+
// Folder with timestamp prefixed will also check for matching timestamps to make sure they are parsed correctly
56+
// The milliseconds weren't matching, seemed like a rounding difference between the influx parser
57+
// Compares each metrics times separately and ignores milliseconds
58+
if strings.HasPrefix(f.Name(), "timestamp") {
59+
require.Equal(t, len(expectedOutputs), len(resultingMetrics))
60+
for i, m := range resultingMetrics {
61+
require.Equal(t, expectedOutputs[i].Time().Truncate(time.Second), m.Time().Truncate(time.Second))
62+
}
63+
}
5164
})
5265
}
5366
}
@@ -66,6 +79,8 @@ func readMetricFile(path string) ([]telegraf.Metric, error) {
6679
line := scanner.Text()
6780
if line != "" {
6881
m, err := parser.ParseLine(line)
82+
// The timezone needs to be UTC to match the timestamp test results
83+
m.SetTime(m.Time().UTC())
6984
if err != nil {
7085
return nil, fmt.Errorf("unable to parse metric in %q failed: %v", line, err)
7186
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test value=0 1631202459121654321
2+
test value=1 1631202459121654321
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"test": [
3+
{ "value": 0 },
4+
{ "value": 1 }
5+
],
6+
"timestamp": 1631202459121654321
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Example taken from: https://github.com/influxdata/telegraf/issues/5940
2+
3+
[[inputs.file]]
4+
files = ["./testdata/timestamp_ns/input.json"]
5+
data_format = "json_v2"
6+
[[inputs.file.json_v2]]
7+
measurement_name = "test"
8+
timestamp_path = "timestamp"
9+
timestamp_format = "unix_ns"
10+
[[inputs.file.json_v2.object]]
11+
path = "test"

0 commit comments

Comments
 (0)