| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package xprocessor // import "go.opentelemetry.io/collector/processor/xprocessor" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
|
| 9 |
"go.opentelemetry.io/collector/component" |
| 10 |
"go.opentelemetry.io/collector/consumer/xconsumer" |
| 11 |
"go.opentelemetry.io/collector/pipeline" |
| 12 |
"go.opentelemetry.io/collector/processor" |
| 13 |
"go.opentelemetry.io/collector/processor/internal" |
| 14 |
) |
| 15 |
|
| 16 |
// Factory is a component.Factory interface for processors. |
| 17 |
// |
| 18 |
// This interface cannot be directly implemented. Implementations must |
| 19 |
// use the NewFactory to implement it. |
| 20 |
type Factory interface { |
| 21 |
processor.Factory |
| 22 |
|
| 23 |
// CreateProfiles creates a Profiles processor based on this config. |
| 24 |
// If the processor type does not support tracing or if the config is not valid, |
| 25 |
// an error will be returned instead. |
| 26 |
CreateProfiles(ctx context.Context, set processor.Settings, cfg component.Config, next xconsumer.Profiles) (Profiles, error) |
| 27 |
|
| 28 |
// ProfilesStability gets the stability level of the Profiles processor. |
| 29 |
ProfilesStability() component.StabilityLevel |
| 30 |
} |
| 31 |
|
| 32 |
// Profiles is a processor that can consume profiles. |
| 33 |
type Profiles interface { |
| 34 |
component.Component |
| 35 |
xconsumer.Profiles |
| 36 |
} |
| 37 |
|
| 38 |
// CreateProfilesFunc is the equivalent of Factory.CreateProfiles(). |
| 39 |
// CreateProfilesFunc is the equivalent of Factory.CreateProfiles(). |
| 40 |
type CreateProfilesFunc func(context.Context, processor.Settings, component.Config, xconsumer.Profiles) (Profiles, error) |
| 41 |
|
| 42 |
// FactoryOption apply changes to ReceiverOptions. |
| 43 |
type FactoryOption interface { |
| 44 |
// applyOption applies the option. |
| 45 |
applyOption(o *factoryOpts) |
| 46 |
} |
| 47 |
|
| 48 |
// factoryOptionFunc is an ReceiverFactoryOption created through a function. |
| 49 |
type factoryOptionFunc func(*factoryOpts) |
| 50 |
|
| 51 |
func (f factoryOptionFunc) applyOption(o *factoryOpts) { |
| 52 |
f(o) |
| 53 |
} |
| 54 |
|
| 55 |
type factory struct { |
| 56 |
processor.Factory |
| 57 |
createProfilesFunc CreateProfilesFunc |
| 58 |
profilesStabilityLevel component.StabilityLevel |
| 59 |
} |
| 60 |
|
| 61 |
func (f factory) ProfilesStability() component.StabilityLevel { |
| 62 |
return f.profilesStabilityLevel |
| 63 |
} |
| 64 |
|
| 65 |
func (f factory) CreateProfiles(ctx context.Context, set processor.Settings, cfg component.Config, next xconsumer.Profiles) (Profiles, error) { |
| 66 |
if f.createProfilesFunc == nil { |
| 67 |
return nil, pipeline.ErrSignalNotSupported |
| 68 |
} |
| 69 |
if set.ID.Type() != f.Type() { |
| 70 |
return nil, internal.ErrIDMismatch(set.ID, f.Type()) |
| 71 |
} |
| 72 |
return f.createProfilesFunc(ctx, set, cfg, next) |
| 73 |
} |
| 74 |
|
| 75 |
type factoryOpts struct { |
| 76 |
opts []processor.FactoryOption |
| 77 |
*factory |
| 78 |
} |
| 79 |
|
| 80 |
// WithTraces overrides the default "error not supported" implementation for CreateTraces and the default "undefined" stability level. |
| 81 |
func WithTraces(createTraces processor.CreateTracesFunc, sl component.StabilityLevel) FactoryOption { |
| 82 |
return factoryOptionFunc(func(o *factoryOpts) { |
| 83 |
o.opts = append(o.opts, processor.WithTraces(createTraces, sl)) |
| 84 |
}) |
| 85 |
} |
| 86 |
|
| 87 |
// WithMetrics overrides the default "error not supported" implementation for CreateMetrics and the default "undefined" stability level. |
| 88 |
func WithMetrics(createMetrics processor.CreateMetricsFunc, sl component.StabilityLevel) FactoryOption { |
| 89 |
return factoryOptionFunc(func(o *factoryOpts) { |
| 90 |
o.opts = append(o.opts, processor.WithMetrics(createMetrics, sl)) |
| 91 |
}) |
| 92 |
} |
| 93 |
|
| 94 |
// WithLogs overrides the default "error not supported" implementation for CreateLogs and the default "undefined" stability level. |
| 95 |
func WithLogs(createLogs processor.CreateLogsFunc, sl component.StabilityLevel) FactoryOption { |
| 96 |
return factoryOptionFunc(func(o *factoryOpts) { |
| 97 |
o.opts = append(o.opts, processor.WithLogs(createLogs, sl)) |
| 98 |
}) |
| 99 |
} |
| 100 |
|
| 101 |
// WithProfiles overrides the default "error not supported" implementation for CreateProfiles and the default "undefined" stability level. |
| 102 |
func WithProfiles(createProfiles CreateProfilesFunc, sl component.StabilityLevel) FactoryOption { |
| 103 |
return factoryOptionFunc(func(o *factoryOpts) { |
| 104 |
o.profilesStabilityLevel = sl |
| 105 |
o.createProfilesFunc = createProfiles |
| 106 |
}) |
| 107 |
} |
| 108 |
|
| 109 |
// NewFactory returns a Factory. |
| 110 |
func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory { |
| 111 |
opts := factoryOpts{factory: &factory{}} |
| 112 |
for _, opt := range options { |
| 113 |
opt.applyOption(&opts) |
| 114 |
} |
| 115 |
opts.Factory = processor.NewFactory(cfgType, createDefaultConfig, opts.opts...) |
| 116 |
return opts.factory |
| 117 |
} |