Skip to content

Commit

Permalink
fix(config): set default parser (#12076)
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj authored and pull[bot] committed Mar 4, 2024
1 parent 68d710f commit 560b53a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
29 changes: 18 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,9 +689,12 @@ func (c *Config) addAggregator(name string, table *ast.Table) error {
return nil
}

func (c *Config) probeParser(table *ast.Table) bool {
func (c *Config) probeParser(parentcategory string, parentname string, table *ast.Table) bool {
var dataformat string
c.getFieldString(table, "data_format", &dataformat)
if dataformat == "" {
dataformat = setDefaultParser(parentcategory, parentname)
}

creator, ok := parsers.Parsers[dataformat]
if !ok {
Expand All @@ -709,15 +712,10 @@ func (c *Config) probeParser(table *ast.Table) bool {
func (c *Config) addParser(parentcategory, parentname string, table *ast.Table) (*models.RunningParser, error) {
var dataformat string
c.getFieldString(table, "data_format", &dataformat)

if dataformat == "" {
if parentcategory == "inputs" && parentname == "exec" {
// Legacy support, exec plugin originally parsed JSON by default.
dataformat = "json"
} else {
dataformat = "influx"
}
dataformat = setDefaultParser(parentcategory, parentname)
}

var influxParserType string
c.getFieldString(table, "influx_parser_type", &influxParserType)
if dataformat == "influx" && influxParserType == "upstream" {
Expand Down Expand Up @@ -828,7 +826,7 @@ func (c *Config) setupProcessor(name string, creator processors.StreamingCreator
}

if t, ok := processor.(telegraf.ParserFuncPlugin); ok {
if !c.probeParser(table) {
if !c.probeParser("processors", name, table) {
return nil, false, errors.New("parser not found")
}
t.SetParserFunc(func() (telegraf.Parser, error) {
Expand Down Expand Up @@ -946,7 +944,7 @@ func (c *Config) addInput(name string, table *ast.Table) error {

if t, ok := input.(telegraf.ParserFuncPlugin); ok {
missCountThreshold = 1
if !c.probeParser(table) {
if !c.probeParser("inputs", name, table) {
return errors.New("parser not found")
}
t.SetParserFunc(func() (telegraf.Parser, error) {
Expand All @@ -957,7 +955,7 @@ func (c *Config) addInput(name string, table *ast.Table) error {
if t, ok := input.(parsers.ParserFuncInput); ok {
// DEPRECATED: Please switch your plugin to telegraf.ParserFuncPlugin.
missCountThreshold = 1
if !c.probeParser(table) {
if !c.probeParser("inputs", name, table) {
return errors.New("parser not found")
}
t.SetParserFunc(func() (parsers.Parser, error) {
Expand Down Expand Up @@ -1431,6 +1429,15 @@ func keys(m map[string]bool) []string {
return result
}

func setDefaultParser(category string, name string) string {
// Legacy support, exec plugin originally parsed JSON by default.
if category == "inputs" && name == "exec" {
return "json"
}

return "influx"
}

func (c *Config) hasErrs() bool {
return len(c.errs) > 0
}
Expand Down
14 changes: 14 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@ func TestConfig_WrongCertPath(t *testing.T) {
require.Error(t, c.LoadConfig("./testdata/wrong_cert_path.toml"))
}

func TestConfig_DefaultParser(t *testing.T) {
c := NewConfig()
require.NoError(t, c.LoadConfig("./testdata/default_parser.toml"))
}

func TestConfig_DefaultExecParser(t *testing.T) {
c := NewConfig()
require.NoError(t, c.LoadConfig("./testdata/default_parser_exec.toml"))
}

func TestConfig_LoadSpecialTypes(t *testing.T) {
c := NewConfig()
require.NoError(t, c.LoadConfig("./testdata/special_types.toml"))
Expand Down Expand Up @@ -886,6 +896,7 @@ type MockupInputPlugin struct {
Paths []string `toml:"paths"`
Port int `toml:"port"`
Command string
Files []string
PidFile string
Log telegraf.Logger `toml:"-"`
tls.ServerConfig
Expand Down Expand Up @@ -1069,6 +1080,9 @@ func init() {
inputs.Add("exec", func() telegraf.Input {
return &MockupInputPlugin{Timeout: Duration(time.Second * 5)}
})
inputs.Add("file", func() telegraf.Input {
return &MockupInputPlugin{}
})
inputs.Add("http_listener_v2", func() telegraf.Input {
return &MockupInputPlugin{}
})
Expand Down
2 changes: 2 additions & 0 deletions config/testdata/default_parser.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[[inputs.file]]
files = ["metrics"]
2 changes: 2 additions & 0 deletions config/testdata/default_parser_exec.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[[inputs.exec]]
command = '/usr/bin/echo {"value": 42}'

0 comments on commit 560b53a

Please sign in to comment.