Skip to content

Commit

Permalink
chore(parser.json_v2): Error out if no config is provided (influxdata…
Browse files Browse the repository at this point in the history
  • Loading branch information
mskonovalov authored and asaharn committed Oct 16, 2024
1 parent da4c257 commit a7d19ba
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
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

0 comments on commit a7d19ba

Please sign in to comment.