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

chore(parser.json_v2): Error out if no config is provided #15844

Merged
merged 8 commits into from
Sep 9, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
As a consequence the redunant `logtarget` setting is deprecated, `stderr` is
used if no `logfile` is provided, otherwise messages are logged to the given
file. For using the Windows `eventlog` set `logformat = "eventlog"`!
- This release contains a change in json_v2 parser config parsing -
if the config is empty (not define any rules), initialization will fail
(see PR [#15844](https://github.com/influxdata/telegraf/pull/15844)).

## v1.31.3 [2024-08-12]

Expand Down
25 changes: 25 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/influxdata/telegraf/plugins/parsers/json_v2"
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
Expand Down Expand Up @@ -833,6 +834,18 @@ func TestConfig_ParserInterface(t *testing.T) {
"ProtobufMessageType": "addressbook.AddressBook",
},
},
"json_v2": {
param: map[string]interface{}{
"Configs": []json_v2.Config{{
Fields: []json_v2.DataSet{{
Path: "",
Type: "int",
Rename: "",
Optional: false,
}},
}},
},
},
}

expected := make([]telegraf.Parser, 0, len(formats))
Expand Down Expand Up @@ -1039,6 +1052,18 @@ func TestConfig_ProcessorsWithParsers(t *testing.T) {
"ProtobufMessageType": "addressbook.AddressBook",
},
},
"json_v2": {
param: map[string]interface{}{
"Configs": []json_v2.Config{{
Fields: []json_v2.DataSet{{
Path: "",
Type: "int",
Rename: "",
Optional: false,
}},
}},
},
},
}

expected := make([]telegraf.Parser, 0, len(formats))
Expand Down
5 changes: 5 additions & 0 deletions config/testdata/parsers_new.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@

[[inputs.parser_test_new]]
data_format = "json_v2"
[[inputs.parser_test_new.json_v2]]
[[inputs.parser_test_new.json_v2.field]]
path = ""
rename = ""
type = "int"

[[inputs.parser_test_new]]
data_format = "logfmt"
Expand Down
5 changes: 5 additions & 0 deletions config/testdata/processors_with_parsers.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@

[[processors.parser_test]]
data_format = "json_v2"
[[processors.parser_test.json_v2]]
[[processors.parser_test.json_v2.field]]
path = ""
rename = ""
type = "int"

[[processors.parser_test]]
data_format = "logfmt"
Expand Down
3 changes: 3 additions & 0 deletions plugins/parsers/json_v2/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ type MetricNode struct {
}

func (p *Parser) Init() error {
if len(p.Configs) == 0 {
return errors.New("no configuration provided")
}
// Propagate the default metric name to the configs in case it is not set there
for i, cfg := range p.Configs {
if cfg.MeasurementName == "" {
Expand Down
8 changes: 8 additions & 0 deletions plugins/parsers/json_v2/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ func TestMultipleConfigs(t *testing.T) {
}
}

func TestParserEmptyConfig(t *testing.T) {
plugin := &json_v2.Parser{
Configs: []json_v2.Config{},
}

require.ErrorContains(t, plugin.Init(), "no configuration provided")
}

func BenchmarkParsingSequential(b *testing.B) {
inputFilename := filepath.Join("testdata", "benchmark", "input.json")

Expand Down
Loading