Skip to content

Commit

Permalink
Nicer error message when passing an empty configuration file (#9762)
Browse files Browse the repository at this point in the history
This PR checks if `cfg.Validate()` error is `errMissingReceivers` error
then returns a nicely formated error.
  • Loading branch information
Kimbohlovette authored Mar 21, 2024
1 parent fc4c13d commit ef5d8f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions otelcol/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
)

var (
errMissingExporters = errors.New("no exporter configuration specified in config")
errMissingReceivers = errors.New("no receiver configuration specified in config")
errMissingExporters = errors.New("no exporter configuration specified in config")
errMissingReceivers = errors.New("no receiver configuration specified in config")
errEmptyConfigurationFile = errors.New("empty configuration file")
)

// Config defines the configuration for the various elements of collector or agent.
Expand Down Expand Up @@ -42,6 +43,11 @@ type Config struct {
// invalid cases that we currently don't check for but which we may want to add in
// the future (e.g. disallowing receiving and exporting on the same endpoint).
func (cfg *Config) Validate() error {
// There must be at least one property set in the configuration file.
if len(cfg.Receivers) == 0 && len(cfg.Exporters) == 0 && len(cfg.Processors) == 0 && len(cfg.Connectors) == 0 && len(cfg.Extensions) == 0 {
return errEmptyConfigurationFile
}

// Currently, there is no default receiver enabled.
// The configuration must specify at least one receiver to be valid.
if len(cfg.Receivers) == 0 {
Expand Down
13 changes: 13 additions & 0 deletions otelcol/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ func TestConfigValidate(t *testing.T) {
},
expected: nil,
},
{
name: "empty configuration file",
cfgFn: func() *Config {
cfg := generateConfig()
cfg.Receivers = nil
cfg.Connectors = nil
cfg.Processors = nil
cfg.Exporters = nil
cfg.Extensions = nil
return cfg
},
expected: errEmptyConfigurationFile,
},
{
name: "missing-exporters",
cfgFn: func() *Config {
Expand Down

0 comments on commit ef5d8f1

Please sign in to comment.