diff --git a/config/config.go b/config/config.go index 97772a6ca2d37..af00e2810dbeb 100644 --- a/config/config.go +++ b/config/config.go @@ -65,9 +65,10 @@ var ( // will be logging to, as well as all the plugins that the user has // specified type Config struct { - toml *toml.Config - errs []error // config load errors. - UnusedFields map[string]bool + toml *toml.Config + errs []error // config load errors. + UnusedFields map[string]bool + AllowUnusedFields bool Tags map[string]string InputFilters []string @@ -764,10 +765,9 @@ func (c *Config) LoadDirectory(path string) error { } // Try to find a default config file at these locations (in order): -// 1. $TELEGRAF_CONFIG_PATH -// 2. $HOME/.telegraf/telegraf.conf -// 3. /etc/telegraf/telegraf.conf -// +// 1. $TELEGRAF_CONFIG_PATH +// 2. $HOME/.telegraf/telegraf.conf +// 3. /etc/telegraf/telegraf.conf func getDefaultConfigPath() (string, error) { envfile := os.Getenv("TELEGRAF_CONFIG_PATH") homefile := os.ExpandEnv("${HOME}/.telegraf/telegraf.conf") @@ -869,7 +869,7 @@ func (c *Config) LoadConfigData(data []byte) error { c.Agent.SnmpTranslator = "netsnmp" } - if len(c.UnusedFields) > 0 { + if !c.AllowUnusedFields && len(c.UnusedFields) > 0 { return fmt.Errorf("line %d: configuration specified the fields %q, but they weren't used", tbl.Line, keys(c.UnusedFields)) } @@ -900,7 +900,7 @@ func (c *Config) LoadConfigData(data []byte) error { return fmt.Errorf("unsupported config format: %s", pluginName) } - if len(c.UnusedFields) > 0 { + if !c.AllowUnusedFields && len(c.UnusedFields) > 0 { return fmt.Errorf("plugin %s.%s: line %d: configuration specified the fields %q, but they weren't used", name, pluginName, subTable.Line, keys(c.UnusedFields)) } } @@ -922,7 +922,7 @@ func (c *Config) LoadConfigData(data []byte) error { return fmt.Errorf("Unsupported config format: %s", pluginName) } - if len(c.UnusedFields) > 0 { + if !c.AllowUnusedFields && len(c.UnusedFields) > 0 { return fmt.Errorf("plugin %s.%s: line %d: configuration specified the fields %q, but they weren't used", name, pluginName, subTable.Line, keys(c.UnusedFields)) } } @@ -939,7 +939,7 @@ func (c *Config) LoadConfigData(data []byte) error { return fmt.Errorf("Unsupported config format: %s", pluginName) } - if len(c.UnusedFields) > 0 { + if !c.AllowUnusedFields && len(c.UnusedFields) > 0 { return fmt.Errorf("plugin %s.%s: line %d: configuration specified the fields %q, but they weren't used", name, pluginName, subTable.Line, keys(c.UnusedFields)) } } @@ -956,7 +956,7 @@ func (c *Config) LoadConfigData(data []byte) error { return fmt.Errorf("Unsupported config format: %s", pluginName) } - if len(c.UnusedFields) > 0 { + if !c.AllowUnusedFields && len(c.UnusedFields) > 0 { return fmt.Errorf("plugin %s.%s: line %d: configuration specified the fields %q, but they weren't used", name, pluginName, subTable.Line, keys(c.UnusedFields)) } } diff --git a/config/config_test.go b/config/config_test.go index 5a64cabcad424..75237dd9d3488 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -261,6 +261,18 @@ func TestConfig_WrongFieldType(t *testing.T) { require.Equal(t, "Error loading config file ./testdata/wrong_field_type2.toml: error parsing http_listener_v2, line 2: (config.MockupInputPlugin.Methods) cannot unmarshal TOML string into []string", err.Error()) } +func TestConfig_UnusedFields(t *testing.T) { + c := NewConfig() + err := c.LoadConfig("./testdata/unused_fields.toml") + require.Error(t, err, "unused fields") + require.Equal(t, "Error loading config file ./testdata/unused_fields.toml: line 1: configuration specified the fields [\"other_field\"], but they weren't used", err.Error()) + + c = NewConfig() + c.AllowUnusedFields = true + err = c.LoadConfig("./testdata/unused_fields.toml") + require.NoError(t, err, "unused fields") +} + func TestConfig_InlineTables(t *testing.T) { // #4098 c := NewConfig() diff --git a/config/testdata/unused_fields.toml b/config/testdata/unused_fields.toml new file mode 100644 index 0000000000000..5758180c51639 --- /dev/null +++ b/config/testdata/unused_fields.toml @@ -0,0 +1,5 @@ +[agent] + interval = "10s" + debug = false + hostname = "" + other_field = "test" \ No newline at end of file