Debian Codegraph @

processor/processor.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package processor // import "go.opentelemetry.io/collector/processor"
5
6 import (
7 "context"
8
9 "go.opentelemetry.io/collector/component"
10 "go.opentelemetry.io/collector/consumer"
11 "go.opentelemetry.io/collector/pipeline"
12 "go.opentelemetry.io/collector/processor/internal"
13 )
14
15 // Traces is a processor that can consume traces.
16 type Traces interface {
17 component.Component
18 consumer.Traces
19 }
20
21 // Metrics is a processor that can consume metrics.
22 type Metrics interface {
23 component.Component
24 consumer.Metrics
25 }
26
27 // Logs is a processor that can consume logs.
28 type Logs interface {
29 component.Component
30 consumer.Logs
31 }
32
33 // Settings is passed to Create* functions in Factory.
34 type Settings struct {
35 // ID returns the ID of the component that will be created.
36 ID component.ID
37
38 component.TelemetrySettings
39
40 // BuildInfo can be used by components for informational purposes
41 BuildInfo component.BuildInfo
42
43 // prevent unkeyed literal initialization
44 _ struct{}
45 }
46
47 // Factory is Factory interface for processors.
48 //
49 // This interface cannot be directly implemented. Implementations must
50 // use the NewFactory to implement it.
51 type Factory interface {
52 component.Factory
53
54 // CreateTraces creates a Traces processor based on this config.
55 // If the processor type does not support traces,
56 // this function returns the error [pipeline.ErrSignalNotSupported].
57 // Implementers can assume `next` is never nil.
58 CreateTraces(ctx context.Context, set Settings, cfg component.Config, next consumer.Traces) (Traces, error)
59
60 // TracesStability gets the stability level of the Traces processor.
61 TracesStability() component.StabilityLevel
62
63 // CreateMetrics creates a Metrics processor based on this config.
64 // If the processor type does not support metrics,
65 // this function returns the error [pipeline.ErrSignalNotSupported].
66 // Implementers can assume `next` is never nil.
67 CreateMetrics(ctx context.Context, set Settings, cfg component.Config, next consumer.Metrics) (Metrics, error)
68
69 // MetricsStability gets the stability level of the Metrics processor.
70 MetricsStability() component.StabilityLevel
71
72 // CreateLogs creates a Logs processor based on the config.
73 // If the processor type does not support logs,
74 // this function returns the error [pipeline.ErrSignalNotSupported].
75 // Implementers can assume `next` is never nil.
76 CreateLogs(ctx context.Context, set Settings, cfg component.Config, next consumer.Logs) (Logs, error)
77
78 // LogsStability gets the stability level of the Logs processor.
79 LogsStability() component.StabilityLevel
80
81 unexportedFactoryFunc()
82 }
83
84 // FactoryOption apply changes to Options.
85 type FactoryOption interface {
86 // applyOption applies the option.
87 applyOption(o *factory)
88 }
89
90 var _ FactoryOption = (*factoryOptionFunc)(nil)
91
92 // factoryOptionFunc is a FactoryOption created through a function.
93 type factoryOptionFunc func(*factory)
94
95 func (f factoryOptionFunc) applyOption(o *factory) {
96 f(o)
97 }
98
99 type factory struct {
100 cfgType component.Type
101 component.CreateDefaultConfigFunc
102 createTracesFunc CreateTracesFunc
103 tracesStabilityLevel component.StabilityLevel
104 createMetricsFunc CreateMetricsFunc
105 metricsStabilityLevel component.StabilityLevel
106 createLogsFunc CreateLogsFunc
107 logsStabilityLevel component.StabilityLevel
108 }
109
110 func (f *factory) Type() component.Type {
111 return f.cfgType
112 }
113
114 func (f *factory) unexportedFactoryFunc() {}
115
116 func (f *factory) TracesStability() component.StabilityLevel {
117 return f.tracesStabilityLevel
118 }
119
120 func (f *factory) MetricsStability() component.StabilityLevel {
121 return f.metricsStabilityLevel
122 }
123
124 func (f *factory) LogsStability() component.StabilityLevel {
125 return f.logsStabilityLevel
126 }
127
128 func (f *factory) CreateTraces(ctx context.Context, set Settings, cfg component.Config, next consumer.Traces) (Traces, error) {
129 if f.createTracesFunc == nil {
130 return nil, pipeline.ErrSignalNotSupported
131 }
132
133 if set.ID.Type() != f.Type() {
134 return nil, internal.ErrIDMismatch(set.ID, f.Type())
135 }
136
137 return f.createTracesFunc(ctx, set, cfg, next)
138 }
139
140 func (f *factory) CreateMetrics(ctx context.Context, set Settings, cfg component.Config, next consumer.Metrics) (Metrics, error) {
141 if f.createMetricsFunc == nil {
142 return nil, pipeline.ErrSignalNotSupported
143 }
144
145 if set.ID.Type() != f.Type() {
146 return nil, internal.ErrIDMismatch(set.ID, f.Type())
147 }
148
149 return f.createMetricsFunc(ctx, set, cfg, next)
150 }
151
152 func (f *factory) CreateLogs(ctx context.Context, set Settings, cfg component.Config, next consumer.Logs) (Logs, error) {
153 if f.createLogsFunc == nil {
154 return nil, pipeline.ErrSignalNotSupported
155 }
156
157 if set.ID.Type() != f.Type() {
158 return nil, internal.ErrIDMismatch(set.ID, f.Type())
159 }
160
161 return f.createLogsFunc(ctx, set, cfg, next)
162 }
163
164 // CreateTracesFunc is the equivalent of Factory.CreateTraces().
165 type CreateTracesFunc func(context.Context, Settings, component.Config, consumer.Traces) (Traces, error)
166
167 // CreateMetricsFunc is the equivalent of Factory.CreateMetrics().
168 type CreateMetricsFunc func(context.Context, Settings, component.Config, consumer.Metrics) (Metrics, error)
169
170 // CreateLogsFunc is the equivalent of Factory.CreateLogs.
171 type CreateLogsFunc func(context.Context, Settings, component.Config, consumer.Logs) (Logs, error)
172
173 // WithTraces overrides the default "error not supported" implementation for CreateTraces and the default "undefined" stability level.
174 func WithTraces(createTraces CreateTracesFunc, sl component.StabilityLevel) FactoryOption {
175 return factoryOptionFunc(func(o *factory) {
176 o.tracesStabilityLevel = sl
177 o.createTracesFunc = createTraces
178 })
179 }
180
181 // WithMetrics overrides the default "error not supported" implementation for CreateMetrics and the default "undefined" stability level.
182 func WithMetrics(createMetrics CreateMetricsFunc, sl component.StabilityLevel) FactoryOption {
183 return factoryOptionFunc(func(o *factory) {
184 o.metricsStabilityLevel = sl
185 o.createMetricsFunc = createMetrics
186 })
187 }
188
189 // WithLogs overrides the default "error not supported" implementation for CreateLogs and the default "undefined" stability level.
190 func WithLogs(createLogs CreateLogsFunc, sl component.StabilityLevel) FactoryOption {
191 return factoryOptionFunc(func(o *factory) {
192 o.logsStabilityLevel = sl
193 o.createLogsFunc = createLogs
194 })
195 }
196
197 // NewFactory returns a Factory.
198 func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory {
199 f := &factory{
200 cfgType: cfgType,
201 CreateDefaultConfigFunc: createDefaultConfig,
202 }
203 for _, opt := range options {
204 opt.applyOption(f)
205 }
206 return f
207 }