Skip to content

Commit

Permalink
Implement Validate() in ReceiverSettings, easy transition (open-telem…
Browse files Browse the repository at this point in the history
…etry#2865)

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored and pjanotti committed Apr 6, 2021
1 parent 52ea047 commit 5a2429b
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 35 deletions.
19 changes: 2 additions & 17 deletions component/componenttest/nop_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package componenttest

import (
"context"
"errors"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenthelper"
Expand All @@ -39,24 +38,10 @@ func (f *nopReceiverFactory) Type() config.Type {
return "nop"
}

// TODO: Consider to remove this or move it to configtest.
type NopConfig struct {
config.ReceiverSettings
}

func (nc *NopConfig) Validate() error {
if nc.TypeVal != "nop" {
return errors.New("invalid receiver config")
}
return nil
}

// CreateDefaultConfig creates the default configuration for the Receiver.
func (f *nopReceiverFactory) CreateDefaultConfig() config.Receiver {
return &NopConfig{
ReceiverSettings: config.ReceiverSettings{
TypeVal: f.Type(),
},
return &config.ReceiverSettings{
TypeVal: f.Type(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion component/componenttest/nop_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestNewNopReceiverFactory(t *testing.T) {
require.NotNil(t, factory)
assert.Equal(t, config.Type("nop"), factory.Type())
cfg := factory.CreateDefaultConfig()
assert.Equal(t, &NopConfig{ReceiverSettings: config.ReceiverSettings{TypeVal: factory.Type()}}, cfg)
assert.Equal(t, &config.ReceiverSettings{TypeVal: factory.Type()}, cfg)

traces, err := factory.CreateTracesReceiver(context.Background(), component.ReceiverCreateParams{}, cfg, consumertest.NewTracesNop())
require.NoError(t, err)
Expand Down
8 changes: 7 additions & 1 deletion config/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ type Receivers map[string]Receiver

// ReceiverSettings defines common settings for a receiver configuration.
// Specific receivers can embed this struct and extend it with more fields if needed.
// It is highly recommended to "override" the Validate() function.
type ReceiverSettings struct {
TypeVal Type `mapstructure:"-"`
NameVal string `mapstructure:"-"`
}

var _ NamedEntity = (*ReceiverSettings)(nil)
var _ Receiver = (*ReceiverSettings)(nil)

// Name gets the receiver name.
func (rs *ReceiverSettings) Name() string {
Expand All @@ -48,3 +49,8 @@ func (rs *ReceiverSettings) SetName(name string) {
func (rs *ReceiverSettings) Type() Type {
return rs.TypeVal
}

// Validate validates the configuration and returns an error if invalid.
func (rs *ReceiverSettings) Validate() error {
return nil
}
4 changes: 0 additions & 4 deletions internal/testcomponents/example_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ type ExampleReceiver struct {
ExtraListSetting []string `mapstructure:"extra_list"`
}

func (ecfg *ExampleReceiver) Validate() error {
return nil
}

const recvType = "examplereceiver"

// ExampleReceiverFactory is factory for ExampleReceiver.
Expand Down
9 changes: 3 additions & 6 deletions receiver/receiverhelper/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,15 @@ import (
"github.com/stretchr/testify/assert"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer"
)

const typeStr = "test"

var defaultCfg = &componenttest.NopConfig{
ReceiverSettings: config.ReceiverSettings{
TypeVal: typeStr,
NameVal: typeStr,
},
var defaultCfg = &config.ReceiverSettings{
TypeVal: typeStr,
NameVal: typeStr,
}

func TestNewFactory(t *testing.T) {
Expand Down
7 changes: 2 additions & 5 deletions service/internal/builder/factories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"

"go.opentelemetry.io/collector/component"
componenttest "go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/extension/extensionhelper"
Expand Down Expand Up @@ -59,9 +58,7 @@ func createTestFactories() component.Factories {

func newBadReceiverFactory() component.ReceiverFactory {
return receiverhelper.NewFactory("bf", func() config.Receiver {
return &componenttest.NopConfig{
ReceiverSettings: config.ReceiverSettings{TypeVal: "bf"},
}
return &config.ReceiverSettings{TypeVal: "bf"}
})
}

Expand All @@ -85,7 +82,7 @@ func newBadExtensionFactory() component.ExtensionFactory {
return extensionhelper.NewFactory(
"bf",
func() config.Extension {
return &config.ExporterSettings{
return &config.ExtensionSettings{
TypeVal: "bf",
}
},
Expand Down
3 changes: 2 additions & 1 deletion service/internal/builder/receivers_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ func TestBuildReceivers_NotSupportedDataType(t *testing.T) {
t.Run(test.configFile, func(t *testing.T) {

cfg, err := configtest.LoadConfigFile(t, path.Join("testdata", test.configFile), factories)
assert.Error(t, err)
assert.NoError(t, err)
require.NotNil(t, cfg)

allExporters, err := BuildExporters(zap.NewNop(), component.DefaultApplicationStartInfo(), cfg, factories.Exporters)
assert.NoError(t, err)
Expand Down

0 comments on commit 5a2429b

Please sign in to comment.