| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package xprocessorhelper // import "go.opentelemetry.io/collector/processor/processorhelper/xprocessorhelper" |
| 5 |
|
| 6 |
import ( |
| 7 |
"go.opentelemetry.io/collector/component" |
| 8 |
"go.opentelemetry.io/collector/consumer" |
| 9 |
) |
| 10 |
|
| 11 |
// Option apply changes to internalOptions. |
| 12 |
type Option interface { |
| 13 |
apply(*baseSettings) |
| 14 |
} |
| 15 |
|
| 16 |
type optionFunc func(*baseSettings) |
| 17 |
|
| 18 |
func (of optionFunc) apply(e *baseSettings) { |
| 19 |
of(e) |
| 20 |
} |
| 21 |
|
| 22 |
// WithStart overrides the default Start function for an processor. |
| 23 |
// The default shutdown function does nothing and always returns nil. |
| 24 |
func WithStart(start component.StartFunc) Option { |
| 25 |
return optionFunc(func(o *baseSettings) { |
| 26 |
o.StartFunc = start |
| 27 |
}) |
| 28 |
} |
| 29 |
|
| 30 |
// WithShutdown overrides the default Shutdown function for an processor. |
| 31 |
// The default shutdown function does nothing and always returns nil. |
| 32 |
func WithShutdown(shutdown component.ShutdownFunc) Option { |
| 33 |
return optionFunc(func(o *baseSettings) { |
| 34 |
o.ShutdownFunc = shutdown |
| 35 |
}) |
| 36 |
} |
| 37 |
|
| 38 |
// WithCapabilities overrides the default GetCapabilities function for an processor. |
| 39 |
// The default GetCapabilities function returns mutable capabilities. |
| 40 |
func WithCapabilities(capabilities consumer.Capabilities) Option { |
| 41 |
return optionFunc(func(o *baseSettings) { |
| 42 |
o.consumerOptions = append(o.consumerOptions, consumer.WithCapabilities(capabilities)) |
| 43 |
}) |
| 44 |
} |
| 45 |
|
| 46 |
type baseSettings struct { |
| 47 |
component.StartFunc |
| 48 |
component.ShutdownFunc |
| 49 |
consumerOptions []consumer.Option |
| 50 |
} |
| 51 |
|
| 52 |
// fromOptions returns the internal settings starting from the default and applying all options. |
| 53 |
func fromOptions(options []Option) *baseSettings { |
| 54 |
// Start from the default options: |
| 55 |
opts := &baseSettings{ |
| 56 |
consumerOptions: []consumer.Option{consumer.WithCapabilities(consumer.Capabilities{MutatesData: true})}, |
| 57 |
} |
| 58 |
|
| 59 |
for _, op := range options { |
| 60 |
op.apply(opts) |
| 61 |
} |
| 62 |
|
| 63 |
return opts |
| 64 |
} |