Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move some unmarshaling errors to the Validate method #2929

Merged
merged 2 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions receiver/hostmetricsreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,19 @@ var _ config.CustomUnmarshable = (*Config)(nil)

// Validate checks the receiver configuration is valid
func (cfg *Config) Validate() error {
if len(cfg.Scrapers) == 0 {
return errors.New("must specify at least one scraper when using hostmetrics receiver")
}

return nil
}

// Unmarshal a config.Parser into the config struct.
func (cfg *Config) Unmarshal(componentParser *config.Parser) error {
if componentParser == nil {
return nil
}

// load the non-dynamic config normally
err := componentParser.Unmarshal(cfg)
if err != nil {
Expand All @@ -59,9 +67,6 @@ func (cfg *Config) Unmarshal(componentParser *config.Parser) error {
if err != nil {
return err
}
if len(scrapersSection.AllKeys()) == 0 {
return errors.New("must specify at least one scraper when using hostmetrics receiver")
}

for key := range cast.ToStringMap(componentParser.Get(scrapersKey)) {
factory, ok := getScraperFactory(key)
Expand Down
2 changes: 1 addition & 1 deletion receiver/hostmetricsreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestLoadInvalidConfig_NoScrapers(t *testing.T) {
factories.Receivers[typeStr] = factory
_, err = configtest.LoadConfigFile(t, path.Join(".", "testdata", "config-noscrapers.yaml"), factories)

require.EqualError(t, err, "error reading receivers configuration for hostmetrics: must specify at least one scraper when using hostmetrics receiver")
require.EqualError(t, err, "receiver \"hostmetrics\" has invalid configuration: must specify at least one scraper when using hostmetrics receiver")
}

func TestLoadInvalidConfig_InvalidScraperKey(t *testing.T) {
Expand Down
10 changes: 6 additions & 4 deletions receiver/jaegerreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ var _ config.CustomUnmarshable = (*Config)(nil)

// Validate checks the receiver configuration is valid
func (cfg *Config) Validate() error {
if cfg.GRPC == nil &&
cfg.ThriftHTTP == nil &&
cfg.ThriftBinary == nil &&
cfg.ThriftCompact == nil {
return fmt.Errorf("must specify at least one protocol when using the Jaeger receiver")
}
return nil
}

Expand All @@ -103,10 +109,6 @@ func (cfg *Config) Unmarshal(componentParser *config.Parser) error {
}

protocols := cast.ToStringMap(componentParser.Get(protocolsFieldName))
if len(protocols) == 0 {
return fmt.Errorf("must specify at least one protocol when using the Jaeger receiver")
}

knownProtocols := 0
if _, ok := protocols[protoGRPC]; !ok {
cfg.GRPC = nil
Expand Down
2 changes: 1 addition & 1 deletion receiver/jaegerreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func TestFailedLoadConfig(t *testing.T) {
assert.EqualError(t, err, "error reading receivers configuration for jaeger: 1 error(s) decoding:\n\n* 'protocols' has invalid keys: thrift_htttp")

_, err = configtest.LoadConfigFile(t, path.Join(".", "testdata", "bad_no_proto_config.yaml"), factories)
assert.EqualError(t, err, "error reading receivers configuration for jaeger: must specify at least one protocol when using the Jaeger receiver")
assert.EqualError(t, err, "receiver \"jaeger\" has invalid configuration: must specify at least one protocol when using the Jaeger receiver")

_, err = configtest.LoadConfigFile(t, path.Join(".", "testdata", "bad_empty_config.yaml"), factories)
assert.EqualError(t, err, "error reading receivers configuration for jaeger: empty config for Jaeger receiver")
Expand Down
9 changes: 4 additions & 5 deletions receiver/otlpreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ var _ config.CustomUnmarshable = (*Config)(nil)

// Validate checks the receiver configuration is valid
func (cfg *Config) Validate() error {
if cfg.GRPC == nil &&
cfg.HTTP == nil {
return fmt.Errorf("must specify at least one protocol when using the OTLP receiver")
}
return nil
}

Expand Down Expand Up @@ -86,10 +90,5 @@ func (cfg *Config) Unmarshal(componentParser *config.Parser) error {
if len(protocols) != knownProtocols {
return fmt.Errorf("unknown protocols in the OTLP receiver")
}

if cfg.GRPC == nil && cfg.HTTP == nil {
return fmt.Errorf("must specify at least one protocol when using the OTLP receiver")
}

return nil
}
2 changes: 1 addition & 1 deletion receiver/otlpreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func TestFailedLoadConfig(t *testing.T) {
assert.EqualError(t, err, "error reading receivers configuration for otlp: 1 error(s) decoding:\n\n* 'protocols' has invalid keys: thrift")

_, err = configtest.LoadConfigFile(t, path.Join(".", "testdata", "bad_no_proto_config.yaml"), factories)
assert.EqualError(t, err, "error reading receivers configuration for otlp: must specify at least one protocol when using the OTLP receiver")
assert.EqualError(t, err, "receiver \"otlp\" has invalid configuration: must specify at least one protocol when using the OTLP receiver")

_, err = configtest.LoadConfigFile(t, path.Join(".", "testdata", "bad_empty_config.yaml"), factories)
assert.EqualError(t, err, "error reading receivers configuration for otlp: empty config for OTLP receiver")
Expand Down
6 changes: 3 additions & 3 deletions receiver/prometheusreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ var _ config.CustomUnmarshable = (*Config)(nil)

// Validate checks the receiver configuration is valid
func (cfg *Config) Validate() error {
if cfg.PrometheusConfig != nil && len(cfg.PrometheusConfig.ScrapeConfigs) == 0 {
return errNilScrapeConfig
}
return nil
}

Expand Down Expand Up @@ -80,8 +83,5 @@ func (cfg *Config) Unmarshal(componentParser *config.Parser) error {
if err != nil {
return fmt.Errorf("prometheus receiver failed to unmarshal yaml to prometheus config: %s", err)
}
if len(cfg.PrometheusConfig.ScrapeConfigs) == 0 {
return errNilScrapeConfig
}
return nil
}