Skip to content

Commit

Permalink
feat: add optional field to allow unused fields in configuration (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
jefchien authored Aug 15, 2024
1 parent b556989 commit a51192f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
24 changes: 12 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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))
}

Expand Down Expand Up @@ -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))
}
}
Expand All @@ -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))
}
}
Expand All @@ -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))
}
}
Expand All @@ -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))
}
}
Expand Down
12 changes: 12 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions config/testdata/unused_fields.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[agent]
interval = "10s"
debug = false
hostname = ""
other_field = "test"

0 comments on commit a51192f

Please sign in to comment.