Skip to content

Commit

Permalink
Remove BaseProcessorFactory, force using helper (open-telemetry#4175)
Browse files Browse the repository at this point in the history
* Add internalinterface package

This adds a general InternalInterface to make an interface impossible
to implement.

* Use internalinterface.InternalInterface for Processor as a test

* Add Changelog note

* need to use -> must use

* Add small unit test

Co-authored-by: Bogdan Drutu <[email protected]>
  • Loading branch information
mx-psi and bogdandrutu authored Nov 2, 2021
1 parent 05c56d5 commit 883afc5
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 63 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

## 🛑 Breaking changes 🛑

- Remove `component.BaseProcessorFactory`, use `processorhelper.NewFactory` instead (#4175)
- Move `service/parserprovider` package to `config/configmapprovider` (#4206)

## v0.38.0 Beta
Expand Down
3 changes: 2 additions & 1 deletion component/componenttest/nop_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/internal/internalinterface"
)

// NewNopProcessorCreateSettings returns a new nop settings for Create*Processor functions.
Expand All @@ -38,7 +39,7 @@ type nopProcessorConfig struct {

// nopProcessorFactory is factory for nopProcessor.
type nopProcessorFactory struct {
component.BaseProcessorFactory
internalinterface.BaseInternal
}

var nopProcessorFactoryInstance = &nopProcessorFactory{}
Expand Down
42 changes: 4 additions & 38 deletions component/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ 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"
)

// Processor defines the common functions that must be implemented by TracesProcessor
Expand Down Expand Up @@ -57,9 +57,10 @@ type ProcessorCreateSettings struct {
// ProcessorFactory is factory interface for processors. This is the
// new factory type that can create new style processors.
//
// This interface cannot be directly implemented. Implementations need to embed
// the BaseProcessorFactory or use the processorhelper.NewFactory to implement it.
// This interface cannot be directly implemented. Implementations must
// use the processorhelper.NewFactory to implement it.
type ProcessorFactory interface {
internalinterface.InternalInterface
Factory

// CreateDefaultConfig creates the default configuration for the Processor.
Expand Down Expand Up @@ -100,39 +101,4 @@ type ProcessorFactory interface {
cfg config.Processor,
nextConsumer consumer.Logs,
) (LogsProcessor, error)

// unexportedProcessor is a dummy method to force this interface to not be implemented.
unexportedProcessor()
}

// BaseProcessorFactory is the interface that must be embedded by all ProcessorFactory implementations.
type BaseProcessorFactory struct{}

var _ ProcessorFactory = (*BaseProcessorFactory)(nil)

// Type must be overridden.
func (b BaseProcessorFactory) Type() config.Type {
panic("implement me")
}

// CreateDefaultConfig must be overridden.
func (b BaseProcessorFactory) CreateDefaultConfig() config.Processor {
panic("implement me")
}

// CreateTracesProcessor default implemented as not supported data type.
func (b BaseProcessorFactory) CreateTracesProcessor(context.Context, ProcessorCreateSettings, config.Processor, consumer.Traces) (TracesProcessor, error) {
return nil, componenterror.ErrDataTypeIsNotSupported
}

// CreateMetricsProcessor default implemented as not supported data type.
func (b BaseProcessorFactory) CreateMetricsProcessor(context.Context, ProcessorCreateSettings, config.Processor, consumer.Metrics) (MetricsProcessor, error) {
return nil, componenterror.ErrDataTypeIsNotSupported
}

// CreateLogsProcessor default implemented as not supported data type.
func (b BaseProcessorFactory) CreateLogsProcessor(context.Context, ProcessorCreateSettings, config.Processor, consumer.Logs) (LogsProcessor, error) {
return nil, componenterror.ErrDataTypeIsNotSupported
}

func (b BaseProcessorFactory) unexportedProcessor() {}
40 changes: 20 additions & 20 deletions component/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ import (

"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/internal/internalinterface"
)

var _ ProcessorFactory = (*TestProcessorFactory)(nil)

type TestProcessorFactory struct {
BaseProcessorFactory
internalinterface.BaseInternal
name string
}

Expand All @@ -40,6 +43,21 @@ func (f *TestProcessorFactory) CreateDefaultConfig() config.Processor {
return nil
}

// CreateTracesProcessor default implemented as not supported data type.
func (f *TestProcessorFactory) CreateTracesProcessor(context.Context, ProcessorCreateSettings, config.Processor, consumer.Traces) (TracesProcessor, error) {
return nil, componenterror.ErrDataTypeIsNotSupported
}

// CreateMetricsProcessor default implemented as not supported data type.
func (f *TestProcessorFactory) CreateMetricsProcessor(context.Context, ProcessorCreateSettings, config.Processor, consumer.Metrics) (MetricsProcessor, error) {
return nil, componenterror.ErrDataTypeIsNotSupported
}

// CreateLogsProcessor default implemented as not supported data type.
func (f *TestProcessorFactory) CreateLogsProcessor(context.Context, ProcessorCreateSettings, config.Processor, consumer.Logs) (LogsProcessor, error) {
return nil, componenterror.ErrDataTypeIsNotSupported
}

func TestMakeProcessorFactoryMap(t *testing.T) {
type testCase struct {
in []ProcessorFactory
Expand Down Expand Up @@ -75,21 +93,3 @@ func TestMakeProcessorFactoryMap(t *testing.T) {
assert.Equal(t, c.out, out)
}
}

func TestBaseProcessorFactory(t *testing.T) {
bpf := BaseProcessorFactory{}
assert.Panics(t, func() {
bpf.Type()
})
assert.Panics(t, func() {
bpf.CreateDefaultConfig()
})
assert.NotPanics(t, bpf.unexportedProcessor)
defaultCfg := config.NewProcessorSettings(config.NewComponentID("nop"))
_, err := bpf.CreateTracesProcessor(context.Background(), ProcessorCreateSettings{}, &defaultCfg, consumertest.NewNop())
assert.ErrorIs(t, err, componenterror.ErrDataTypeIsNotSupported)
_, err = bpf.CreateMetricsProcessor(context.Background(), ProcessorCreateSettings{}, &defaultCfg, consumertest.NewNop())
assert.ErrorIs(t, err, componenterror.ErrDataTypeIsNotSupported)
_, err = bpf.CreateLogsProcessor(context.Background(), ProcessorCreateSettings{}, &defaultCfg, consumertest.NewNop())
assert.ErrorIs(t, err, componenterror.ErrDataTypeIsNotSupported)
}
30 changes: 30 additions & 0 deletions internal/internalinterface/internalinterface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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 internalinterface // import "go.opentelemetry.io/collector/internal/internalinterface"

// InternalInterface is an interface used to prevent library consumers
// to implement a given interface.
type InternalInterface interface {
// unexportedMethod is a dummy method to force this interface to not be implemented.
unexportedMethod()
}

var _ InternalInterface = (*BaseInternal)(nil)

// BaseInternal must be embedded on structs implementing InternalInterface.
type BaseInternal struct{}

// unexportedMethod implements the internal interface.
func (*BaseInternal) unexportedMethod() {}
26 changes: 26 additions & 0 deletions internal/internalinterface/internalinterface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 internalinterface // import "go.opentelemetry.io/collector/internal/internalinterface"

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestBaseInternal(t *testing.T) {
base := BaseInternal{}
assert.NotPanics(t, base.unexportedMethod)
}
10 changes: 6 additions & 4 deletions processor/processorhelper/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ 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 ProcessorOptions.
Expand All @@ -38,7 +40,7 @@ type CreateMetricsProcessor func(context.Context, component.ProcessorCreateSetti
type CreateLogsProcessor func(context.Context, component.ProcessorCreateSettings, config.Processor, consumer.Logs) (component.LogsProcessor, error)

type factory struct {
component.BaseProcessorFactory
internalinterface.BaseInternal
cfgType config.Type
createDefaultConfig CreateDefaultConfig
createTracesProcessor CreateTracesProcessor
Expand Down Expand Up @@ -100,7 +102,7 @@ func (f *factory) CreateTracesProcessor(
nextConsumer consumer.Traces,
) (component.TracesProcessor, error) {
if f.createTracesProcessor == nil {
return f.BaseProcessorFactory.CreateTracesProcessor(ctx, set, cfg, nextConsumer)
return nil, componenterror.ErrDataTypeIsNotSupported
}
return f.createTracesProcessor(ctx, set, cfg, nextConsumer)
}
Expand All @@ -113,7 +115,7 @@ func (f *factory) CreateMetricsProcessor(
nextConsumer consumer.Metrics,
) (component.MetricsProcessor, error) {
if f.createMetricsProcessor == nil {
return f.BaseProcessorFactory.CreateMetricsProcessor(ctx, set, cfg, nextConsumer)
return nil, componenterror.ErrDataTypeIsNotSupported
}
return f.createMetricsProcessor(ctx, set, cfg, nextConsumer)
}
Expand All @@ -126,7 +128,7 @@ func (f *factory) CreateLogsProcessor(
nextConsumer consumer.Logs,
) (component.LogsProcessor, error) {
if f.createLogsProcessor == nil {
return f.BaseProcessorFactory.CreateLogsProcessor(ctx, set, cfg, nextConsumer)
return nil, componenterror.ErrDataTypeIsNotSupported
}
return f.createLogsProcessor(ctx, set, cfg, nextConsumer)
}

0 comments on commit 883afc5

Please sign in to comment.