| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
//go:generate mdatagen metadata.yaml |
| 5 |
|
| 6 |
package processorhelper // import "go.opentelemetry.io/collector/processor/processorhelper" |
| 7 |
|
| 8 |
import ( |
| 9 |
"errors" |
| 10 |
|
| 11 |
"go.opentelemetry.io/otel/attribute" |
| 12 |
"go.opentelemetry.io/otel/trace" |
| 13 |
|
| 14 |
"go.opentelemetry.io/collector/component" |
| 15 |
"go.opentelemetry.io/collector/consumer" |
| 16 |
"go.opentelemetry.io/collector/processor/internal" |
| 17 |
) |
| 18 |
|
| 19 |
// ErrSkipProcessingData is a sentinel value to indicate when traces or metrics should intentionally be dropped |
| 20 |
// from further processing in the pipeline because the data is determined to be irrelevant. A processor can return this error |
| 21 |
// to stop further processing without propagating an error back up the pipeline to logs. |
| 22 |
var ErrSkipProcessingData = errors.New("sentinel error to skip processing data from the remainder of the pipeline") |
| 23 |
|
| 24 |
// Option apply changes to internalOptions. |
| 25 |
type Option interface { |
| 26 |
apply(*baseSettings) |
| 27 |
} |
| 28 |
|
| 29 |
type optionFunc func(*baseSettings) |
| 30 |
|
| 31 |
func (of optionFunc) apply(e *baseSettings) { |
| 32 |
of(e) |
| 33 |
} |
| 34 |
|
| 35 |
// WithStart overrides the default Start function for an processor. |
| 36 |
// The default shutdown function does nothing and always returns nil. |
| 37 |
func WithStart(start component.StartFunc) Option { |
| 38 |
return optionFunc(func(o *baseSettings) { |
| 39 |
o.StartFunc = start |
| 40 |
}) |
| 41 |
} |
| 42 |
|
| 43 |
// WithShutdown overrides the default Shutdown function for an processor. |
| 44 |
// The default shutdown function does nothing and always returns nil. |
| 45 |
func WithShutdown(shutdown component.ShutdownFunc) Option { |
| 46 |
return optionFunc(func(o *baseSettings) { |
| 47 |
o.ShutdownFunc = shutdown |
| 48 |
}) |
| 49 |
} |
| 50 |
|
| 51 |
// WithCapabilities overrides the default GetCapabilities function for an processor. |
| 52 |
// The default GetCapabilities function returns mutable capabilities. |
| 53 |
func WithCapabilities(capabilities consumer.Capabilities) Option { |
| 54 |
return optionFunc(func(o *baseSettings) { |
| 55 |
o.consumerOptions = append(o.consumerOptions, consumer.WithCapabilities(capabilities)) |
| 56 |
}) |
| 57 |
} |
| 58 |
|
| 59 |
type baseSettings struct { |
| 60 |
component.StartFunc |
| 61 |
component.ShutdownFunc |
| 62 |
consumerOptions []consumer.Option |
| 63 |
} |
| 64 |
|
| 65 |
// fromOptions returns the internal settings starting from the default and applying all options. |
| 66 |
func fromOptions(options []Option) *baseSettings { |
| 67 |
// Start from the default options: |
| 68 |
opts := &baseSettings{ |
| 69 |
consumerOptions: []consumer.Option{consumer.WithCapabilities(consumer.Capabilities{MutatesData: true})}, |
| 70 |
} |
| 71 |
|
| 72 |
for _, op := range options { |
| 73 |
op.apply(opts) |
| 74 |
} |
| 75 |
|
| 76 |
return opts |
| 77 |
} |
| 78 |
|
| 79 |
func spanAttributes(id component.ID) trace.EventOption { |
| 80 |
return trace.WithAttributes(attribute.String(internal.ProcessorKey, id.String())) |
| 81 |
} |