Skip to content

Commit

Permalink
Deprecate Config.Validate() in favor of component.ValidateConfig (#6572)
Browse files Browse the repository at this point in the history
* Deprecate Config.Validate() in favor of component.ValidateConfig

This PR is "split" from #6544, to avoid breaking changes and go via deprecation.

It is a bit hacky, but better than breaking everyone who uses this interface.

Signed-off-by: Bogdan Drutu <[email protected]>

* Update config/receiver.go

Co-authored-by: Dmitrii Anoshin <[email protected]>

Signed-off-by: Bogdan Drutu <[email protected]>
Co-authored-by: Dmitrii Anoshin <[email protected]>
  • Loading branch information
bogdandrutu and dmitryax authored Nov 17, 2022
1 parent 6a117bf commit 86d886c
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 32 deletions.
11 changes: 11 additions & 0 deletions .chloggen/deprecatevalidate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: deprecation

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: component

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Deprecate `component.Config.Validate` in favor of `component.ValidateConfig`

# One or more tracking issues or pull requests related to the change
issues: [6572]
29 changes: 24 additions & 5 deletions component/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,34 @@ import (
"go.opentelemetry.io/collector/confmap"
)

// Type is the component type as it is used in the config.
type Type string
type Config interface {
identifiable
// Deprecated: [v0.65.0] use component.ValidateConfig.
Validate() error

// validatable defines the interface for the configuration validation.
type validatable interface {
// Validate validates the configuration and returns an error if invalid.
privateConfig()
}

// ConfigValidator defines an optional interface for configurations to implement to do validation.
type ConfigValidator interface {
// Validate the configuration and returns an error if invalid.
Validate() error
}

// ValidateConfig validates a config, by doing this:
// - Call Validate on the config itself if the config implements ConfigValidator.
func ValidateConfig(cfg Config) error {
validator, ok := cfg.(ConfigValidator)
if !ok {
return nil
}

return validator.Validate()
}

// Type is the component type as it is used in the config.
type Type string

// DataType is a special Type that represents the data types supported by the collector. We currently support
// collecting metrics, traces and logs, this can expand in the future.
type DataType = Type
Expand Down
5 changes: 1 addition & 4 deletions component/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ import (
// ExporterConfig is the configuration of a component.Exporter. Specific Exporter must implement
// this interface and must embed config.ExporterSettings struct or a struct that extends it.
type ExporterConfig interface {
identifiable
validatable

privateConfigExporter()
Config
}

// UnmarshalExporterConfig helper function to unmarshal an ExporterConfig.
Expand Down
5 changes: 1 addition & 4 deletions component/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ import (
// ExtensionConfig is the configuration of a component.Extension. Specific Extension must implement
// this interface and must embed config.ExtensionSettings struct or a struct that extends it.
type ExtensionConfig interface {
identifiable
validatable

privateConfigExtension()
Config
}

// UnmarshalExtensionConfig helper function to unmarshal an ExtensionConfig.
Expand Down
5 changes: 1 addition & 4 deletions component/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ import (
// ProcessorConfig is the configuration of a component.Processor. Specific Processor must implement
// this interface and must embed ProcessorSettings struct or a struct that extends it.
type ProcessorConfig interface {
identifiable
validatable

privateConfigProcessor()
Config
}

// UnmarshalProcessorConfig helper function to unmarshal a ProcessorConfig.
Expand Down
5 changes: 1 addition & 4 deletions component/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ import (
// ReceiverConfig is the configuration of a component.Receiver. Specific Extension must implement
// this interface and must embed ReceiverSettings struct or a struct that extends it.
type ReceiverConfig interface {
identifiable
validatable

privateConfigReceiver()
Config
}

// UnmarshalReceiverConfig helper function to unmarshal a ReceiverConfig.
Expand Down
2 changes: 1 addition & 1 deletion config/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (es *ExporterSettings) SetIDName(idName string) {
es.id = component.NewIDWithName(es.id.Type(), idName)
}

// Validate validates the configuration and returns an error if invalid.
// Deprecated: [v0.65.0] use component.ValidateConfig.
func (es *ExporterSettings) Validate() error {
return nil
}
2 changes: 1 addition & 1 deletion config/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (es *ExtensionSettings) SetIDName(idName string) {
es.id = component.NewIDWithName(es.id.Type(), idName)
}

// Validate validates the configuration and returns an error if invalid.
// Deprecated: [v0.65.0] use component.ValidateConfig.
func (es *ExtensionSettings) Validate() error {
return nil
}
2 changes: 1 addition & 1 deletion config/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (ps *ProcessorSettings) SetIDName(idName string) {
ps.id = component.NewIDWithName(ps.id.Type(), idName)
}

// Validate validates the configuration and returns an error if invalid.
// Deprecated: [v0.65.0] use component.ValidateConfig.
func (ps *ProcessorSettings) Validate() error {
return nil
}
2 changes: 1 addition & 1 deletion config/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (rs *ReceiverSettings) SetIDName(idName string) {
rs.id = component.NewIDWithName(rs.id.Type(), idName)
}

// Validate validates the configuration and returns an error if invalid.
// Deprecated: [v0.65.0] Not needed anymore since the Validate() will be moved from Config interface to the optional ConfigValidator interface.
func (rs *ReceiverSettings) Validate() error {
return nil
}
2 changes: 1 addition & 1 deletion exporter/otlphttpexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestUnmarshalDefaultConfig(t *testing.T) {
assert.NoError(t, component.UnmarshalExporterConfig(confmap.New(), cfg))
assert.Equal(t, factory.CreateDefaultConfig(), cfg)
// Default/Empty config is invalid.
assert.Error(t, cfg.Validate())
assert.Error(t, component.ValidateConfig(cfg))
}

func TestUnmarshalConfig(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions receiver/otlpreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@ func TestUnmarshalConfigEmptyProtocols(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalReceiverConfig(cm, cfg))
assert.EqualError(t, cfg.Validate(), "must specify at least one protocol when using the OTLP receiver")
assert.EqualError(t, component.ValidateConfig(cfg), "must specify at least one protocol when using the OTLP receiver")
}

func TestUnmarshalConfigEmpty(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalReceiverConfig(confmap.New(), cfg))
assert.EqualError(t, cfg.Validate(), "must specify at least one protocol when using the OTLP receiver")
assert.EqualError(t, component.ValidateConfig(cfg), "must specify at least one protocol when using the OTLP receiver")
}
8 changes: 4 additions & 4 deletions service/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (cfg *Config) Validate() error {

// Validate the receiver configuration.
for recvID, recvCfg := range cfg.Receivers {
if err := recvCfg.Validate(); err != nil {
if err := component.ValidateConfig(recvCfg); err != nil {
return fmt.Errorf("receiver %q has invalid configuration: %w", recvID, err)
}
}
Expand All @@ -73,21 +73,21 @@ func (cfg *Config) Validate() error {

// Validate the exporter configuration.
for expID, expCfg := range cfg.Exporters {
if err := expCfg.Validate(); err != nil {
if err := component.ValidateConfig(expCfg); err != nil {
return fmt.Errorf("exporter %q has invalid configuration: %w", expID, err)
}
}

// Validate the processor configuration.
for procID, procCfg := range cfg.Processors {
if err := procCfg.Validate(); err != nil {
if err := component.ValidateConfig(procCfg); err != nil {
return fmt.Errorf("processor %q has invalid configuration: %w", procID, err)
}
}

// Validate the extension configuration.
for extID, extCfg := range cfg.Extensions {
if err := extCfg.Validate(); err != nil {
if err := component.ValidateConfig(extCfg); err != nil {
return fmt.Errorf("extension %q has invalid configuration: %w", extID, err)
}
}
Expand Down

0 comments on commit 86d886c

Please sign in to comment.