diff --git a/CHANGELOG.md b/CHANGELOG.md index 9101db9154e..9c3c2153776 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,12 @@ - Deprecated `exporterhelper.WithMetrics` in favour of `component.WithMetricsExporter` - Deprecated `exporterhelper.WithLogs` in favour of `component.WithLogsExporter` - Deprecated `exporterhelper.NewFactory` in favour of `component.NewExporterFactory` +- Move helpers from receiverhelper to component (#4891) + - Deprecated `receiverhelper.CreateDefaultConfig` in favour of `component.ReceiverDefaultConfigFunc` + - Deprecated `receiverhelper.WithTraces` in favour of `component.WithTracesReceiver` + - Deprecated `receiverhelper.WithMetrics` in favour of `component.WithMetricsReceiver` + - Deprecated `receiverhelper.WithLogs` in favour of `component.WithLogsReceiver` + - Deprecated `receiverhelper.NewFactory` in favour of `component.NewReceiverFactory` ### 💡 Enhancements 💡 diff --git a/component/componenttest/nop_receiver.go b/component/componenttest/nop_receiver.go index f4eb64c23f9..fd574aa4fa4 100644 --- a/component/componenttest/nop_receiver.go +++ b/component/componenttest/nop_receiver.go @@ -20,7 +20,6 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config" "go.opentelemetry.io/collector/consumer" - "go.opentelemetry.io/collector/internal/internalinterface" ) // NewNopReceiverCreateSettings returns a new nop settings for Create*Receiver functions. @@ -35,57 +34,31 @@ type nopReceiverConfig struct { config.ReceiverSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct } -// nopReceiverFactory is factory for nopReceiver. -type nopReceiverFactory struct { - internalinterface.BaseInternal -} - -var nopReceiverFactoryInstance = &nopReceiverFactory{} +var nopReceiverFactory = component.NewReceiverFactory( + "nop", + func() config.Receiver { + return &nopReceiverConfig{ + ReceiverSettings: config.NewReceiverSettings(config.NewComponentID("nop")), + } + }, + component.WithTracesReceiver(createTracesReceiver), + component.WithMetricsReceiver(createMetricsReceiver), + component.WithLogsReceiver(createLogsReceiver)) // NewNopReceiverFactory returns a component.ReceiverFactory that constructs nop receivers. func NewNopReceiverFactory() component.ReceiverFactory { - return nopReceiverFactoryInstance -} - -// Type gets the type of the Receiver config created by this factory. -func (f *nopReceiverFactory) Type() config.Type { - return config.NewComponentID("nop").Type() -} - -// CreateDefaultConfig creates the default configuration for the Receiver. -func (f *nopReceiverFactory) CreateDefaultConfig() config.Receiver { - return &nopReceiverConfig{ - ReceiverSettings: config.NewReceiverSettings(config.NewComponentID("nop")), - } + return nopReceiverFactory } -// CreateTracesReceiver implements component.ReceiverFactory interface. -func (f *nopReceiverFactory) CreateTracesReceiver( - _ context.Context, - _ component.ReceiverCreateSettings, - _ config.Receiver, - _ consumer.Traces, -) (component.TracesReceiver, error) { +func createTracesReceiver(context.Context, component.ReceiverCreateSettings, config.Receiver, consumer.Traces) (component.TracesReceiver, error) { return nopReceiverInstance, nil } -// CreateMetricsReceiver implements component.ReceiverFactory interface. -func (f *nopReceiverFactory) CreateMetricsReceiver( - _ context.Context, - _ component.ReceiverCreateSettings, - _ config.Receiver, - _ consumer.Metrics, -) (component.MetricsReceiver, error) { +func createMetricsReceiver(context.Context, component.ReceiverCreateSettings, config.Receiver, consumer.Metrics) (component.MetricsReceiver, error) { return nopReceiverInstance, nil } -// CreateLogsReceiver implements component.ReceiverFactory interface. -func (f *nopReceiverFactory) CreateLogsReceiver( - _ context.Context, - _ component.ReceiverCreateSettings, - _ config.Receiver, - _ consumer.Logs, -) (component.LogsReceiver, error) { +func createLogsReceiver(context.Context, component.ReceiverCreateSettings, config.Receiver, consumer.Logs) (component.LogsReceiver, error) { return nopReceiverInstance, nil } diff --git a/component/factories_test.go b/component/factories_test.go index ec1b904d17b..503f2a665c4 100644 --- a/component/factories_test.go +++ b/component/factories_test.go @@ -59,6 +59,44 @@ func TestMakeExtensionFactoryMap(t *testing.T) { } } +func TestMakeReceiverFactoryMap(t *testing.T) { + type testCase struct { + name string + in []ReceiverFactory + out map[config.Type]ReceiverFactory + } + + p1 := NewReceiverFactory("p1", nil) + p2 := NewReceiverFactory("p2", nil) + testCases := []testCase{ + { + name: "different names", + in: []ReceiverFactory{p1, p2}, + out: map[config.Type]ReceiverFactory{ + p1.Type(): p1, + p2.Type(): p2, + }, + }, + { + name: "same name", + in: []ReceiverFactory{p1, p2, NewReceiverFactory("p1", nil)}, + }, + } + + for i := range testCases { + tt := testCases[i] + t.Run(tt.name, func(t *testing.T) { + out, err := MakeReceiverFactoryMap(tt.in...) + if tt.out == nil { + assert.Error(t, err) + return + } + assert.NoError(t, err) + assert.Equal(t, tt.out, out) + }) + } +} + func TestMakeProcessorFactoryMap(t *testing.T) { type testCase struct { name string diff --git a/component/receiver.go b/component/receiver.go index 6143f0142d2..8c3092f880e 100644 --- a/component/receiver.go +++ b/component/receiver.go @@ -17,8 +17,10 @@ package component // import "go.opentelemetry.io/collector/component" import ( "context" + "go.opentelemetry.io/collector/component/componenterror" "go.opentelemetry.io/collector/config" "go.opentelemetry.io/collector/consumer" + "go.opentelemetry.io/collector/internal/internalinterface" ) // Receiver allows the collector to receive metrics, traces and logs. @@ -97,11 +99,10 @@ type ReceiverCreateSettings struct { BuildInfo BuildInfo } -// ReceiverFactory can create TracesReceiver, MetricsReceiver and -// and LogsReceiver. This is the new preferred factory type to create receivers. +// ReceiverFactory is factory interface for receivers. // // This interface cannot be directly implemented. Implementations must -// use the receiverhelper.NewFactory to implement it. +// use the NewReceiverFactory to implement it. type ReceiverFactory interface { Factory @@ -132,3 +133,108 @@ type ReceiverFactory interface { CreateLogsReceiver(ctx context.Context, set ReceiverCreateSettings, cfg config.Receiver, nextConsumer consumer.Logs) (LogsReceiver, error) } + +// ReceiverFactoryOption apply changes to ReceiverOptions. +type ReceiverFactoryOption func(o *receiverFactory) + +// ReceiverCreateDefaultConfigFunc is the equivalent of ReceiverFactory.CreateDefaultConfig(). +type ReceiverCreateDefaultConfigFunc func() config.Receiver + +// CreateDefaultConfig implements ReceiverFactory.CreateDefaultConfig(). +func (f ReceiverCreateDefaultConfigFunc) CreateDefaultConfig() config.Receiver { + return f() +} + +// CreateTracesReceiverFunc is the equivalent of ReceiverFactory.CreateTracesReceiver(). +type CreateTracesReceiverFunc func(context.Context, ReceiverCreateSettings, config.Receiver, consumer.Traces) (TracesReceiver, error) + +// CreateTracesReceiver implements ReceiverFactory.CreateTracesReceiver(). +func (f CreateTracesReceiverFunc) CreateTracesReceiver( + ctx context.Context, + set ReceiverCreateSettings, + cfg config.Receiver, + nextConsumer consumer.Traces) (TracesReceiver, error) { + if f == nil { + return nil, componenterror.ErrDataTypeIsNotSupported + } + return f(ctx, set, cfg, nextConsumer) +} + +// CreateMetricsReceiverFunc is the equivalent of ReceiverFactory.CreateMetricsReceiver(). +type CreateMetricsReceiverFunc func(context.Context, ReceiverCreateSettings, config.Receiver, consumer.Metrics) (MetricsReceiver, error) + +// CreateMetricsReceiver implements ReceiverFactory.CreateMetricsReceiver(). +func (f CreateMetricsReceiverFunc) CreateMetricsReceiver( + ctx context.Context, + set ReceiverCreateSettings, + cfg config.Receiver, + nextConsumer consumer.Metrics, +) (MetricsReceiver, error) { + if f == nil { + return nil, componenterror.ErrDataTypeIsNotSupported + } + return f(ctx, set, cfg, nextConsumer) +} + +// CreateLogsReceiverFunc is the equivalent of ReceiverFactory.CreateLogsReceiver(). +type CreateLogsReceiverFunc func(context.Context, ReceiverCreateSettings, config.Receiver, consumer.Logs) (LogsReceiver, error) + +// CreateLogsReceiver implements ReceiverFactory.CreateLogsReceiver(). +func (f CreateLogsReceiverFunc) CreateLogsReceiver( + ctx context.Context, + set ReceiverCreateSettings, + cfg config.Receiver, + nextConsumer consumer.Logs, +) (LogsReceiver, error) { + if f == nil { + return nil, componenterror.ErrDataTypeIsNotSupported + } + return f(ctx, set, cfg, nextConsumer) +} + +type receiverFactory struct { + internalinterface.BaseInternal + cfgType config.Type + ReceiverCreateDefaultConfigFunc + CreateTracesReceiverFunc + CreateMetricsReceiverFunc + CreateLogsReceiverFunc +} + +// WithTracesReceiver overrides the default "error not supported" implementation for CreateTracesReceiver. +func WithTracesReceiver(createTracesReceiver CreateTracesReceiverFunc) ReceiverFactoryOption { + return func(o *receiverFactory) { + o.CreateTracesReceiverFunc = createTracesReceiver + } +} + +// WithMetricsReceiver overrides the default "error not supported" implementation for CreateMetricsReceiver. +func WithMetricsReceiver(createMetricsReceiver CreateMetricsReceiverFunc) ReceiverFactoryOption { + return func(o *receiverFactory) { + o.CreateMetricsReceiverFunc = createMetricsReceiver + } +} + +// WithLogsReceiver overrides the default "error not supported" implementation for CreateLogsReceiver. +func WithLogsReceiver(createLogsReceiver CreateLogsReceiverFunc) ReceiverFactoryOption { + return func(o *receiverFactory) { + o.CreateLogsReceiverFunc = createLogsReceiver + } +} + +// NewReceiverFactory returns a ReceiverFactory. +func NewReceiverFactory(cfgType config.Type, createDefaultConfig ReceiverCreateDefaultConfigFunc, options ...ReceiverFactoryOption) ReceiverFactory { + f := &receiverFactory{ + cfgType: cfgType, + ReceiverCreateDefaultConfigFunc: createDefaultConfig, + } + for _, opt := range options { + opt(f) + } + return f +} + +// Type returns the type of the Receiver created by this ReceiverFactory. +func (f *receiverFactory) Type() config.Type { + return f.cfgType +} diff --git a/component/receiver_test.go b/component/receiver_test.go index bbec820e41c..f5a52e1fd1f 100644 --- a/component/receiver_test.go +++ b/component/receiver_test.go @@ -15,55 +15,61 @@ package component import ( + "context" "testing" "github.com/stretchr/testify/assert" "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/consumer" ) -type TestReceiverFactory struct { - ReceiverFactory - name config.Type +func TestNewReceiverFactory(t *testing.T) { + const typeStr = "test" + defaultCfg := config.NewReceiverSettings(config.NewComponentID(typeStr)) + factory := NewReceiverFactory( + typeStr, + func() config.Receiver { return &defaultCfg }) + assert.EqualValues(t, typeStr, factory.Type()) + assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig()) + _, err := factory.CreateTracesReceiver(context.Background(), ReceiverCreateSettings{}, &defaultCfg, nil) + assert.Error(t, err) + _, err = factory.CreateMetricsReceiver(context.Background(), ReceiverCreateSettings{}, &defaultCfg, nil) + assert.Error(t, err) + _, err = factory.CreateLogsReceiver(context.Background(), ReceiverCreateSettings{}, &defaultCfg, nil) + assert.Error(t, err) } -// Type gets the type of the Receiver config created by this factory. -func (f *TestReceiverFactory) Type() config.Type { - return f.name +func TestNewReceiverFactory_WithOptions(t *testing.T) { + const typeStr = "test" + defaultCfg := config.NewReceiverSettings(config.NewComponentID(typeStr)) + factory := NewReceiverFactory( + typeStr, + func() config.Receiver { return &defaultCfg }, + WithTracesReceiver(createTracesReceiver), + WithMetricsReceiver(createMetricsReceiver), + WithLogsReceiver(createLogsReceiver)) + assert.EqualValues(t, typeStr, factory.Type()) + assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig()) + + _, err := factory.CreateTracesReceiver(context.Background(), ReceiverCreateSettings{}, &defaultCfg, nil) + assert.NoError(t, err) + + _, err = factory.CreateMetricsReceiver(context.Background(), ReceiverCreateSettings{}, &defaultCfg, nil) + assert.NoError(t, err) + + _, err = factory.CreateLogsReceiver(context.Background(), ReceiverCreateSettings{}, &defaultCfg, nil) + assert.NoError(t, err) } -func TestBuildReceivers(t *testing.T) { - type testCase struct { - in []ReceiverFactory - out map[config.Type]ReceiverFactory - } +func createTracesReceiver(context.Context, ReceiverCreateSettings, config.Receiver, consumer.Traces) (TracesReceiver, error) { + return nil, nil +} - testCases := []testCase{ - { - in: []ReceiverFactory{ - &TestReceiverFactory{name: "e1"}, - &TestReceiverFactory{name: "e2"}, - }, - out: map[config.Type]ReceiverFactory{ - "e1": &TestReceiverFactory{name: "e1"}, - "e2": &TestReceiverFactory{name: "e2"}, - }, - }, - { - in: []ReceiverFactory{ - &TestReceiverFactory{name: "e1"}, - &TestReceiverFactory{name: "e1"}, - }, - }, - } +func createMetricsReceiver(context.Context, ReceiverCreateSettings, config.Receiver, consumer.Metrics) (MetricsReceiver, error) { + return nil, nil +} - for _, c := range testCases { - out, err := MakeReceiverFactoryMap(c.in...) - if c.out == nil { - assert.Error(t, err) - continue - } - assert.NoError(t, err) - assert.Equal(t, c.out, out) - } +func createLogsReceiver(context.Context, ReceiverCreateSettings, config.Receiver, consumer.Logs) (LogsReceiver, error) { + return nil, nil } diff --git a/internal/testcomponents/example_receiver.go b/internal/testcomponents/example_receiver.go index 3c574a69bc2..ec9789e371d 100644 --- a/internal/testcomponents/example_receiver.go +++ b/internal/testcomponents/example_receiver.go @@ -21,7 +21,6 @@ import ( "go.opentelemetry.io/collector/config" "go.opentelemetry.io/collector/config/confignet" "go.opentelemetry.io/collector/consumer" - "go.opentelemetry.io/collector/receiver/receiverhelper" ) // ExampleReceiver is for testing purposes. We are defining an example config and factory @@ -36,15 +35,15 @@ type ExampleReceiver struct { ExtraListSetting []string `mapstructure:"extra_list"` } -var receiverType = config.Type("examplereceiver") +const receiverType = config.Type("examplereceiver") // ExampleReceiverFactory is factory for ExampleReceiver. -var ExampleReceiverFactory = receiverhelper.NewFactory( +var ExampleReceiverFactory = component.NewReceiverFactory( receiverType, createReceiverDefaultConfig, - receiverhelper.WithTraces(createTracesReceiver), - receiverhelper.WithMetrics(createMetricsReceiver), - receiverhelper.WithLogs(createLogsReceiver)) + component.WithTracesReceiver(createTracesReceiver), + component.WithMetricsReceiver(createMetricsReceiver), + component.WithLogsReceiver(createLogsReceiver)) func createReceiverDefaultConfig() config.Receiver { return &ExampleReceiver{ diff --git a/receiver/otlpreceiver/factory.go b/receiver/otlpreceiver/factory.go index acc820df318..ca4dc3ec3e3 100644 --- a/receiver/otlpreceiver/factory.go +++ b/receiver/otlpreceiver/factory.go @@ -24,7 +24,6 @@ import ( "go.opentelemetry.io/collector/config/confignet" "go.opentelemetry.io/collector/consumer" "go.opentelemetry.io/collector/internal/sharedcomponent" - "go.opentelemetry.io/collector/receiver/receiverhelper" ) const ( @@ -37,12 +36,12 @@ const ( // NewFactory creates a new OTLP receiver factory. func NewFactory() component.ReceiverFactory { - return receiverhelper.NewFactory( + return component.NewReceiverFactory( typeStr, createDefaultConfig, - receiverhelper.WithTraces(createTracesReceiver), - receiverhelper.WithMetrics(createMetricsReceiver), - receiverhelper.WithLogs(createLogReceiver)) + component.WithTracesReceiver(createTracesReceiver), + component.WithMetricsReceiver(createMetricsReceiver), + component.WithLogsReceiver(createLogReceiver)) } // createDefaultConfig creates the default configuration for receiver. diff --git a/receiver/receiverhelper/factory.go b/receiver/receiverhelper/factory.go index 357e7a3ee2a..6602c31d1e2 100644 --- a/receiver/receiverhelper/factory.go +++ b/receiver/receiverhelper/factory.go @@ -15,118 +15,32 @@ package receiverhelper // import "go.opentelemetry.io/collector/receiver/receiverhelper" import ( - "context" - "go.opentelemetry.io/collector/component" - "go.opentelemetry.io/collector/component/componenterror" - "go.opentelemetry.io/collector/config" - "go.opentelemetry.io/collector/consumer" - "go.opentelemetry.io/collector/internal/internalinterface" ) -// FactoryOption apply changes to ReceiverOptions. -type FactoryOption func(o *factory) - -// WithTraces overrides the default "error not supported" implementation for CreateTracesReceiver. -func WithTraces(createTracesReceiver CreateTracesReceiver) FactoryOption { - return func(o *factory) { - o.createTracesReceiver = createTracesReceiver - } -} - -// WithMetrics overrides the default "error not supported" implementation for CreateMetricsReceiver. -func WithMetrics(createMetricsReceiver CreateMetricsReceiver) FactoryOption { - return func(o *factory) { - o.createMetricsReceiver = createMetricsReceiver - } -} - -// WithLogs overrides the default "error not supported" implementation for CreateLogsReceiver. -func WithLogs(createLogsReceiver CreateLogsReceiver) FactoryOption { - return func(o *factory) { - o.createLogsReceiver = createLogsReceiver - } -} - -// CreateDefaultConfig is the equivalent of component.ReceiverFactory.CreateDefaultConfig() -type CreateDefaultConfig func() config.Receiver - -// CreateTracesReceiver is the equivalent of component.ReceiverFactory.CreateTracesReceiver() -type CreateTracesReceiver func(context.Context, component.ReceiverCreateSettings, config.Receiver, consumer.Traces) (component.TracesReceiver, error) - -// CreateMetricsReceiver is the equivalent of component.ReceiverFactory.CreateMetricsReceiver() -type CreateMetricsReceiver func(context.Context, component.ReceiverCreateSettings, config.Receiver, consumer.Metrics) (component.MetricsReceiver, error) +// Deprecated: [v0.46.0] use component.ReceiverFactoryOption. +type FactoryOption = component.ReceiverFactoryOption -// CreateLogsReceiver is the equivalent of component.ReceiverFactory.CreateLogsReceiver() -type CreateLogsReceiver func(context.Context, component.ReceiverCreateSettings, config.Receiver, consumer.Logs) (component.LogsReceiver, error) +// Deprecated: [v0.46.0] use component.WithTracesReceiver. +var WithTraces = component.WithTracesReceiver -type factory struct { - internalinterface.BaseInternal - cfgType config.Type - createDefaultConfig CreateDefaultConfig - createTracesReceiver CreateTracesReceiver - createMetricsReceiver CreateMetricsReceiver - createLogsReceiver CreateLogsReceiver -} +// Deprecated: [v0.46.0] use component.WithMetricsReceiver. +var WithMetrics = component.WithMetricsReceiver -// NewFactory returns a component.ReceiverFactory. -func NewFactory( - cfgType config.Type, - createDefaultConfig CreateDefaultConfig, - options ...FactoryOption) component.ReceiverFactory { - f := &factory{ - cfgType: cfgType, - createDefaultConfig: createDefaultConfig, - } - for _, opt := range options { - opt(f) - } - return f -} +// Deprecated: [v0.46.0] use component.WithLogsReceiver. +var WithLogs = component.WithLogsReceiver -// Type gets the type of the Receiver config created by this factory. -func (f *factory) Type() config.Type { - return f.cfgType -} +// Deprecated: [v0.46.0] use component.ReceiverCreateDefaultConfigFunc. +type CreateDefaultConfig = component.ReceiverCreateDefaultConfigFunc -// CreateDefaultConfig creates the default configuration for receiver. -func (f *factory) CreateDefaultConfig() config.Receiver { - return f.createDefaultConfig() -} +// Deprecated: [v0.46.0] use component.CreateTracesReceiverFunc. +type CreateTracesReceiver = component.CreateTracesReceiverFunc -// CreateTracesReceiver creates a component.TracesReceiver based on this config. -func (f *factory) CreateTracesReceiver( - ctx context.Context, - set component.ReceiverCreateSettings, - cfg config.Receiver, - nextConsumer consumer.Traces) (component.TracesReceiver, error) { - if f.createTracesReceiver != nil { - return f.createTracesReceiver(ctx, set, cfg, nextConsumer) - } - return nil, componenterror.ErrDataTypeIsNotSupported -} +// Deprecated: [v0.46.0] use component.CreateMetricsReceiverFunc. +type CreateMetricsReceiver = component.CreateMetricsReceiverFunc -// CreateMetricsReceiver creates a component.MetricsReceiver based on this config. -func (f *factory) CreateMetricsReceiver( - ctx context.Context, - set component.ReceiverCreateSettings, - cfg config.Receiver, - nextConsumer consumer.Metrics) (component.MetricsReceiver, error) { - if f.createMetricsReceiver != nil { - return f.createMetricsReceiver(ctx, set, cfg, nextConsumer) - } - return nil, componenterror.ErrDataTypeIsNotSupported -} +// Deprecated: [v0.46.0] use component.CreateLogsReceiverFunc. +type CreateLogsReceiver = component.CreateLogsReceiverFunc -// CreateLogsReceiver creates a component.LogsReceiver based on this config. -func (f *factory) CreateLogsReceiver( - ctx context.Context, - set component.ReceiverCreateSettings, - cfg config.Receiver, - nextConsumer consumer.Logs, -) (component.LogsReceiver, error) { - if f.createLogsReceiver != nil { - return f.createLogsReceiver(ctx, set, cfg, nextConsumer) - } - return nil, componenterror.ErrDataTypeIsNotSupported -} +// Deprecated: [v0.46.0] use component.NewReceiverFactory. +var NewFactory = component.NewReceiverFactory diff --git a/receiver/receiverhelper/factory_test.go b/receiver/receiverhelper/factory_test.go deleted file mode 100644 index aba3d006e28..00000000000 --- a/receiver/receiverhelper/factory_test.go +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package receiverhelper - -import ( - "context" - "testing" - - "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 = config.NewReceiverSettings(config.NewComponentID(typeStr)) - -func TestNewFactory(t *testing.T) { - factory := NewFactory( - typeStr, - defaultConfig) - assert.EqualValues(t, typeStr, factory.Type()) - assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig()) - _, err := factory.CreateTracesReceiver(context.Background(), componenttest.NewNopReceiverCreateSettings(), factory.CreateDefaultConfig(), nil) - assert.Error(t, err) - _, err = factory.CreateMetricsReceiver(context.Background(), componenttest.NewNopReceiverCreateSettings(), factory.CreateDefaultConfig(), nil) - assert.Error(t, err) - _, err = factory.CreateLogsReceiver(context.Background(), componenttest.NewNopReceiverCreateSettings(), factory.CreateDefaultConfig(), nil) - assert.Error(t, err) -} - -func TestNewFactory_WithConstructors(t *testing.T) { - factory := NewFactory( - typeStr, - defaultConfig, - WithTraces(createTracesReceiver), - WithMetrics(createMetricsReceiver), - WithLogs(createLogsReceiver)) - assert.EqualValues(t, typeStr, factory.Type()) - assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig()) - - _, err := factory.CreateTracesReceiver(context.Background(), componenttest.NewNopReceiverCreateSettings(), factory.CreateDefaultConfig(), nil) - assert.NoError(t, err) - - _, err = factory.CreateMetricsReceiver(context.Background(), componenttest.NewNopReceiverCreateSettings(), factory.CreateDefaultConfig(), nil) - assert.NoError(t, err) - - _, err = factory.CreateLogsReceiver(context.Background(), componenttest.NewNopReceiverCreateSettings(), factory.CreateDefaultConfig(), nil) - assert.NoError(t, err) -} - -func defaultConfig() config.Receiver { - return &defaultCfg -} - -func createTracesReceiver(context.Context, component.ReceiverCreateSettings, config.Receiver, consumer.Traces) (component.TracesReceiver, error) { - return nil, nil -} - -func createMetricsReceiver(context.Context, component.ReceiverCreateSettings, config.Receiver, consumer.Metrics) (component.MetricsReceiver, error) { - return nil, nil -} - -func createLogsReceiver(context.Context, component.ReceiverCreateSettings, config.Receiver, consumer.Logs) (component.LogsReceiver, error) { - return nil, nil -} diff --git a/service/internal/builder/factories_test.go b/service/internal/builder/factories_test.go index 9c60c60a78f..988997d838e 100644 --- a/service/internal/builder/factories_test.go +++ b/service/internal/builder/factories_test.go @@ -18,7 +18,6 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config" "go.opentelemetry.io/collector/internal/testcomponents" - "go.opentelemetry.io/collector/receiver/receiverhelper" ) func createTestFactories() component.Factories { @@ -48,7 +47,7 @@ func createTestFactories() component.Factories { } func newBadReceiverFactory() component.ReceiverFactory { - return receiverhelper.NewFactory("bf", func() config.Receiver { + return component.NewReceiverFactory("bf", func() config.Receiver { return &struct { config.ReceiverSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct }{