Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions plugins/parsers/json_v2/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 16 additions & 1 deletion plugins/parsers/json_v2/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"time"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
Expand Down Expand Up @@ -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))
}
}
})
}
}
Expand All @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions plugins/parsers/json_v2/testdata/timestamp_ns/expected.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test value=0 1631202459121654321
test value=1 1631202459121654321
7 changes: 7 additions & 0 deletions plugins/parsers/json_v2/testdata/timestamp_ns/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"test": [
{ "value": 0 },
{ "value": 1 }
],
"timestamp": 1631202459121654321
}
11 changes: 11 additions & 0 deletions plugins/parsers/json_v2/testdata/timestamp_ns/telegraf.conf
Original file line number Diff line number Diff line change
@@ -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"