Skip to content

Commit 4ae0fc1

Browse files
authored
Use Option pattern for componenthelper (#2778)
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent b78ff24 commit 4ae0fc1

File tree

11 files changed

+86
-61
lines changed

11 files changed

+86
-61
lines changed

CHANGELOG.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
## 🛑 Breaking changes 🛑
66

77
- Rename pdata.DoubleSummary to pdata.Summary (#2774)
8-
- Refactored `consumererror` package (#2768)
9-
- Eliminated `PartialError` type in favor of signal-specific types
10-
- Renamed `CombineErrors` to `Combine`
8+
- Refactor `consumererror` package (#2768)
9+
- Remove `PartialError` type in favor of signal-specific types
10+
- Rename `CombineErrors()` to `Combine()`
11+
- Refactor `componenthelper` package (#2778)
12+
- Remove `ComponentSettings` and `DefaultComponentSettings()`
13+
- Rename `NewComponent()` to `New()`
1114

1215
## v0.23.0 Beta
1316

component/componenthelper/component.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,28 @@ type Start func(context.Context, component.Host) error
2626
// Shutdown specifies the function invoked when the exporter is being shutdown.
2727
type Shutdown func(context.Context) error
2828

29-
// ComponentSettings represents a settings struct to create components.
30-
type ComponentSettings struct {
29+
// baseSettings represents a settings struct to create components.
30+
type baseSettings struct {
3131
Start
3232
Shutdown
3333
}
3434

35-
// DefaultComponentSettings returns the default settings for a component. The Start and Shutdown are no-op.
36-
func DefaultComponentSettings() *ComponentSettings {
37-
return &ComponentSettings{
38-
Start: func(ctx context.Context, host component.Host) error { return nil },
39-
Shutdown: func(ctx context.Context) error { return nil },
35+
// Option represents the possible options for New.
36+
type Option func(*baseSettings)
37+
38+
// WithStart overrides the default Start function for a processor.
39+
// The default shutdown function does nothing and always returns nil.
40+
func WithStart(start Start) Option {
41+
return func(o *baseSettings) {
42+
o.Start = start
43+
}
44+
}
45+
46+
// WithShutdown overrides the default Shutdown function for a processor.
47+
// The default shutdown function does nothing and always returns nil.
48+
func WithShutdown(shutdown Shutdown) Option {
49+
return func(o *baseSettings) {
50+
o.Shutdown = shutdown
4051
}
4152
}
4253

@@ -55,10 +66,25 @@ func (be *baseComponent) Shutdown(ctx context.Context) error {
5566
return be.shutdown(ctx)
5667
}
5768

58-
// NewComponent returns a component.Component that calls the given Start and Shutdown.
59-
func NewComponent(s *ComponentSettings) component.Component {
69+
// fromOptions returns the internal settings starting from the default and applying all options.
70+
func fromOptions(options []Option) *baseSettings {
71+
opts := &baseSettings{
72+
Start: func(ctx context.Context, host component.Host) error { return nil },
73+
Shutdown: func(ctx context.Context) error { return nil },
74+
}
75+
76+
for _, op := range options {
77+
op(opts)
78+
}
79+
80+
return opts
81+
}
82+
83+
// New returns a component.Component configured with the provided Options.
84+
func New(options ...Option) component.Component {
85+
bs := fromOptions(options)
6086
return &baseComponent{
61-
start: s.Start,
62-
shutdown: s.Shutdown,
87+
start: bs.Start,
88+
shutdown: bs.Shutdown,
6389
}
6490
}

component/componenthelper/component_test.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,43 +28,37 @@ import (
2828
)
2929

3030
func TestDefaultSettings(t *testing.T) {
31-
st := componenthelper.DefaultComponentSettings()
32-
require.NotNil(t, st)
33-
cp := componenthelper.NewComponent(st)
31+
cp := componenthelper.New()
3432
require.NoError(t, cp.Start(context.Background(), componenttest.NewNopHost()))
3533
require.NoError(t, cp.Shutdown(context.Background()))
3634
}
3735

3836
func TestWithStart(t *testing.T) {
3937
startCalled := false
40-
st := componenthelper.DefaultComponentSettings()
41-
st.Start = func(context.Context, component.Host) error { startCalled = true; return nil }
42-
cp := componenthelper.NewComponent(st)
38+
start := func(context.Context, component.Host) error { startCalled = true; return nil }
39+
cp := componenthelper.New(componenthelper.WithStart(start))
4340
assert.NoError(t, cp.Start(context.Background(), componenttest.NewNopHost()))
4441
assert.True(t, startCalled)
4542
}
4643

4744
func TestWithStart_ReturnError(t *testing.T) {
4845
want := errors.New("my_error")
49-
st := componenthelper.DefaultComponentSettings()
50-
st.Start = func(context.Context, component.Host) error { return want }
51-
cp := componenthelper.NewComponent(st)
46+
start := func(context.Context, component.Host) error { return want }
47+
cp := componenthelper.New(componenthelper.WithStart(start))
5248
assert.Equal(t, want, cp.Start(context.Background(), componenttest.NewNopHost()))
5349
}
5450

5551
func TestWithShutdown(t *testing.T) {
5652
shutdownCalled := false
57-
st := componenthelper.DefaultComponentSettings()
58-
st.Shutdown = func(context.Context) error { shutdownCalled = true; return nil }
59-
cp := componenthelper.NewComponent(st)
53+
shutdown := func(context.Context) error { shutdownCalled = true; return nil }
54+
cp := componenthelper.New(componenthelper.WithShutdown(shutdown))
6055
assert.NoError(t, cp.Shutdown(context.Background()))
6156
assert.True(t, shutdownCalled)
6257
}
6358

6459
func TestWithShutdown_ReturnError(t *testing.T) {
6560
want := errors.New("my_error")
66-
st := componenthelper.DefaultComponentSettings()
67-
st.Shutdown = func(context.Context) error { return want }
68-
cp := componenthelper.NewComponent(st)
61+
shutdown := func(context.Context) error { return want }
62+
cp := componenthelper.New(componenthelper.WithShutdown(shutdown))
6963
assert.Equal(t, want, cp.Shutdown(context.Background()))
7064
}

component/componenttest/nop_exporter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (f *nopExporterFactory) CreateLogsExporter(
7474
}
7575

7676
var nopExporterInstance = &nopExporter{
77-
Component: componenthelper.NewComponent(componenthelper.DefaultComponentSettings()),
77+
Component: componenthelper.New(),
7878
Traces: consumertest.NewTracesNop(),
7979
Metrics: consumertest.NewMetricsNop(),
8080
Logs: consumertest.NewLogsNop(),

component/componenttest/nop_extension.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (f *nopExtensionFactory) CreateExtension(
5454
}
5555

5656
var nopExtensionInstance = &nopExtension{
57-
Component: componenthelper.NewComponent(componenthelper.DefaultComponentSettings()),
57+
Component: componenthelper.New(),
5858
}
5959

6060
// nopExtension stores consumed traces and metrics for testing purposes.

component/componenttest/nop_processor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (f *nopProcessorFactory) CreateLogsProcessor(
7777
}
7878

7979
var nopProcessorInstance = &nopProcessor{
80-
Component: componenthelper.NewComponent(componenthelper.DefaultComponentSettings()),
80+
Component: componenthelper.New(),
8181
Traces: consumertest.NewTracesNop(),
8282
Metrics: consumertest.NewMetricsNop(),
8383
Logs: consumertest.NewLogsNop(),

component/componenttest/nop_receiver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (f *nopReceiverFactory) CreateLogsReceiver(
7676
}
7777

7878
var nopReceiverInstance = &nopReceiver{
79-
Component: componenthelper.NewComponent(componenthelper.DefaultComponentSettings()),
79+
Component: componenthelper.New(),
8080
}
8181

8282
// nopReceiver stores consumed traces and metrics for testing purposes.

exporter/exporterhelper/common.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"go.opentelemetry.io/collector/config/configmodels"
2626
)
2727

28-
// ComponentSettings for timeout. The timeout applies to individual attempts to send data to the backend.
28+
// TimeoutSettings for timeout. The timeout applies to individual attempts to send data to the backend.
2929
type TimeoutSettings struct {
3030
// Timeout is the timeout for every attempt to send data to the backend.
3131
Timeout time.Duration `mapstructure:"timeout"`
@@ -72,7 +72,7 @@ func (req *baseRequest) setContext(ctx context.Context) {
7272

7373
// baseSettings represents all the options that users can configure.
7474
type baseSettings struct {
75-
*componenthelper.ComponentSettings
75+
componentOptions []componenthelper.Option
7676
TimeoutSettings
7777
QueueSettings
7878
RetrySettings
@@ -83,8 +83,7 @@ type baseSettings struct {
8383
func fromOptions(options []Option) *baseSettings {
8484
// Start from the default options:
8585
opts := &baseSettings{
86-
ComponentSettings: componenthelper.DefaultComponentSettings(),
87-
TimeoutSettings: DefaultTimeoutSettings(),
86+
TimeoutSettings: DefaultTimeoutSettings(),
8887
// TODO: Enable queuing by default (call DefaultQueueSettings)
8988
QueueSettings: QueueSettings{Enabled: false},
9089
// TODO: Enable retry by default (call DefaultRetrySettings)
@@ -102,19 +101,19 @@ func fromOptions(options []Option) *baseSettings {
102101
// Option apply changes to baseSettings.
103102
type Option func(*baseSettings)
104103

105-
// WithShutdown overrides the default Shutdown function for an exporter.
104+
// WithStart overrides the default Start function for an exporter.
106105
// The default shutdown function does nothing and always returns nil.
107-
func WithShutdown(shutdown componenthelper.Shutdown) Option {
106+
func WithStart(start componenthelper.Start) Option {
108107
return func(o *baseSettings) {
109-
o.Shutdown = shutdown
108+
o.componentOptions = append(o.componentOptions, componenthelper.WithStart(start))
110109
}
111110
}
112111

113-
// WithStart overrides the default Start function for an exporter.
112+
// WithShutdown overrides the default Shutdown function for an exporter.
114113
// The default shutdown function does nothing and always returns nil.
115-
func WithStart(start componenthelper.Start) Option {
114+
func WithShutdown(shutdown componenthelper.Shutdown) Option {
116115
return func(o *baseSettings) {
117-
o.Start = start
116+
o.componentOptions = append(o.componentOptions, componenthelper.WithShutdown(shutdown))
118117
}
119118
}
120119

@@ -162,7 +161,7 @@ type baseExporter struct {
162161
func newBaseExporter(cfg configmodels.Exporter, logger *zap.Logger, options ...Option) *baseExporter {
163162
bs := fromOptions(options)
164163
be := &baseExporter{
165-
Component: componenthelper.NewComponent(bs.ComponentSettings),
164+
Component: componenthelper.New(bs.componentOptions...),
166165
cfg: cfg,
167166
convertResourceToTelemetry: bs.ResourceToTelemetrySettings.Enabled,
168167
}

internal/testcomponents/example_extension.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,5 @@ func createExtensionDefaultConfig() configmodels.Extension {
5252

5353
// CreateExtension creates an Extension based on this config.
5454
func createExtension(context.Context, component.ExtensionCreateParams, configmodels.Extension) (component.Extension, error) {
55-
return componenthelper.NewComponent(componenthelper.DefaultComponentSettings()), nil
55+
return componenthelper.New(), nil
5656
}

processor/processorhelper/processor.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ type Option func(*baseSettings)
6262
// The default shutdown function does nothing and always returns nil.
6363
func WithStart(start componenthelper.Start) Option {
6464
return func(o *baseSettings) {
65-
o.Start = start
65+
o.componentOptions = append(o.componentOptions, componenthelper.WithStart(start))
6666
}
6767
}
6868

6969
// WithShutdown overrides the default Shutdown function for an processor.
7070
// The default shutdown function does nothing and always returns nil.
7171
func WithShutdown(shutdown componenthelper.Shutdown) Option {
7272
return func(o *baseSettings) {
73-
o.Shutdown = shutdown
73+
o.componentOptions = append(o.componentOptions, componenthelper.WithShutdown(shutdown))
7474
}
7575
}
7676

@@ -83,16 +83,15 @@ func WithCapabilities(capabilities component.ProcessorCapabilities) Option {
8383
}
8484

8585
type baseSettings struct {
86-
*componenthelper.ComponentSettings
87-
capabilities component.ProcessorCapabilities
86+
componentOptions []componenthelper.Option
87+
capabilities component.ProcessorCapabilities
8888
}
8989

9090
// fromOptions returns the internal settings starting from the default and applying all options.
9191
func fromOptions(options []Option) *baseSettings {
9292
// Start from the default options:
9393
opts := &baseSettings{
94-
ComponentSettings: componenthelper.DefaultComponentSettings(),
95-
capabilities: component.ProcessorCapabilities{MutatesConsumedData: true},
94+
capabilities: component.ProcessorCapabilities{MutatesConsumedData: true},
9695
}
9796

9897
for _, op := range options {
@@ -114,7 +113,7 @@ type baseProcessor struct {
114113
func newBaseProcessor(fullName string, options ...Option) baseProcessor {
115114
bs := fromOptions(options)
116115
be := baseProcessor{
117-
Component: componenthelper.NewComponent(bs.ComponentSettings),
116+
Component: componenthelper.New(bs.componentOptions...),
118117
fullName: fullName,
119118
capabilities: bs.capabilities,
120119
traceAttributes: []trace.Attribute{

0 commit comments

Comments
 (0)