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

This commit is contained in:
Mikhail Konovalov 2024-09-10 01:04:37 +10:00 committed by GitHub
parent 2e056a8701
commit 53fb5adac2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 49 additions and 0 deletions

View File

@ -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]

View File

@ -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"
@ -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))
@ -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))

View File

@ -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"

View File

@ -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"

View File

@ -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 == "" {

View File

@ -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")