diff --git a/CHANGELOG.md b/CHANGELOG.md index b6f64d3b6..1743d6a67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/config/config_test.go b/config/config_test.go index 9116e365e..ed208d6f0 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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)) diff --git a/config/testdata/parsers_new.toml b/config/testdata/parsers_new.toml index 515d69243..e0e513285 100644 --- a/config/testdata/parsers_new.toml +++ b/config/testdata/parsers_new.toml @@ -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" diff --git a/config/testdata/processors_with_parsers.toml b/config/testdata/processors_with_parsers.toml index 71022210c..bdbf826c7 100644 --- a/config/testdata/processors_with_parsers.toml +++ b/config/testdata/processors_with_parsers.toml @@ -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" diff --git a/plugins/parsers/json_v2/parser.go b/plugins/parsers/json_v2/parser.go index b9506f11a..57e55ec67 100644 --- a/plugins/parsers/json_v2/parser.go +++ b/plugins/parsers/json_v2/parser.go @@ -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 == "" { diff --git a/plugins/parsers/json_v2/parser_test.go b/plugins/parsers/json_v2/parser_test.go index 7c976fc8d..d2650bda1 100644 --- a/plugins/parsers/json_v2/parser_test.go +++ b/plugins/parsers/json_v2/parser_test.go @@ -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")