forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.go
291 lines (239 loc) · 9.55 KB
/
processor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package processor // import "go.opentelemetry.io/collector/processor"
import (
"context"
"fmt"
"go.uber.org/zap"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
)
// Traces is a processor that can consume traces.
type Traces interface {
component.Component
consumer.Traces
}
// Metrics is a processor that can consume metrics.
type Metrics interface {
component.Component
consumer.Metrics
}
// Logs is a processor that can consume logs.
type Logs interface {
component.Component
consumer.Logs
}
// CreateSettings is passed to Create* functions in Factory.
type CreateSettings struct {
// ID returns the ID of the component that will be created.
ID component.ID
component.TelemetrySettings
// BuildInfo can be used by components for informational purposes
BuildInfo component.BuildInfo
}
// Factory is Factory interface for processors.
//
// This interface cannot be directly implemented. Implementations must
// use the NewProcessorFactory to implement it.
type Factory interface {
component.Factory
// CreateTracesProcessor creates a TracesProcessor based on this config.
// If the processor type does not support tracing or if the config is not valid,
// an error will be returned instead.
CreateTracesProcessor(ctx context.Context, set CreateSettings, cfg component.Config, nextConsumer consumer.Traces) (Traces, error)
// TracesProcessorStability gets the stability level of the TracesProcessor.
TracesProcessorStability() component.StabilityLevel
// CreateMetricsProcessor creates a MetricsProcessor based on this config.
// If the processor type does not support metrics or if the config is not valid,
// an error will be returned instead.
CreateMetricsProcessor(ctx context.Context, set CreateSettings, cfg component.Config, nextConsumer consumer.Metrics) (Metrics, error)
// MetricsProcessorStability gets the stability level of the MetricsProcessor.
MetricsProcessorStability() component.StabilityLevel
// CreateLogsProcessor creates a LogsProcessor based on the config.
// If the processor type does not support logs or if the config is not valid,
// an error will be returned instead.
CreateLogsProcessor(ctx context.Context, set CreateSettings, cfg component.Config, nextConsumer consumer.Logs) (Logs, error)
// LogsProcessorStability gets the stability level of the LogsProcessor.
LogsProcessorStability() component.StabilityLevel
unexportedFactoryFunc()
}
// FactoryOption apply changes to Options.
type FactoryOption interface {
// applyProcessorFactoryOption applies the option.
applyProcessorFactoryOption(o *factory)
}
var _ FactoryOption = (*factoryOptionFunc)(nil)
// factoryOptionFunc is a FactoryOption created through a function.
type factoryOptionFunc func(*factory)
func (f factoryOptionFunc) applyProcessorFactoryOption(o *factory) {
f(o)
}
// CreateTracesFunc is the equivalent of Factory.CreateTraces().
type CreateTracesFunc func(context.Context, CreateSettings, component.Config, consumer.Traces) (Traces, error)
// CreateTracesProcessor implements Factory.CreateTracesProcessor().
func (f CreateTracesFunc) CreateTracesProcessor(
ctx context.Context,
set CreateSettings,
cfg component.Config,
nextConsumer consumer.Traces) (Traces, error) {
if f == nil {
return nil, component.ErrDataTypeIsNotSupported
}
return f(ctx, set, cfg, nextConsumer)
}
// CreateMetricsFunc is the equivalent of Factory.CreateMetrics().
type CreateMetricsFunc func(context.Context, CreateSettings, component.Config, consumer.Metrics) (Metrics, error)
// CreateMetricsProcessor implements Factory.CreateMetricsProcessor().
func (f CreateMetricsFunc) CreateMetricsProcessor(
ctx context.Context,
set CreateSettings,
cfg component.Config,
nextConsumer consumer.Metrics,
) (Metrics, error) {
if f == nil {
return nil, component.ErrDataTypeIsNotSupported
}
return f(ctx, set, cfg, nextConsumer)
}
// CreateLogsFunc is the equivalent of Factory.CreateLogs().
type CreateLogsFunc func(context.Context, CreateSettings, component.Config, consumer.Logs) (Logs, error)
// CreateLogsProcessor implements Factory.CreateLogsProcessor().
func (f CreateLogsFunc) CreateLogsProcessor(
ctx context.Context,
set CreateSettings,
cfg component.Config,
nextConsumer consumer.Logs,
) (Logs, error) {
if f == nil {
return nil, component.ErrDataTypeIsNotSupported
}
return f(ctx, set, cfg, nextConsumer)
}
type factory struct {
cfgType component.Type
component.CreateDefaultConfigFunc
CreateTracesFunc
tracesStabilityLevel component.StabilityLevel
CreateMetricsFunc
metricsStabilityLevel component.StabilityLevel
CreateLogsFunc
logsStabilityLevel component.StabilityLevel
}
func (f *factory) Type() component.Type {
return f.cfgType
}
func (f *factory) unexportedFactoryFunc() {}
func (f factory) TracesProcessorStability() component.StabilityLevel {
return f.tracesStabilityLevel
}
func (f factory) MetricsProcessorStability() component.StabilityLevel {
return f.metricsStabilityLevel
}
func (f factory) LogsProcessorStability() component.StabilityLevel {
return f.logsStabilityLevel
}
// WithTraces overrides the default "error not supported" implementation for CreateTraces and the default "undefined" stability level.
func WithTraces(createTraces CreateTracesFunc, sl component.StabilityLevel) FactoryOption {
return factoryOptionFunc(func(o *factory) {
o.tracesStabilityLevel = sl
o.CreateTracesFunc = createTraces
})
}
// WithMetrics overrides the default "error not supported" implementation for CreateMetrics and the default "undefined" stability level.
func WithMetrics(createMetrics CreateMetricsFunc, sl component.StabilityLevel) FactoryOption {
return factoryOptionFunc(func(o *factory) {
o.metricsStabilityLevel = sl
o.CreateMetricsFunc = createMetrics
})
}
// WithLogs overrides the default "error not supported" implementation for CreateLogs and the default "undefined" stability level.
func WithLogs(createLogs CreateLogsFunc, sl component.StabilityLevel) FactoryOption {
return factoryOptionFunc(func(o *factory) {
o.logsStabilityLevel = sl
o.CreateLogsFunc = createLogs
})
}
// NewFactory returns a Factory.
func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory {
f := &factory{
cfgType: cfgType,
CreateDefaultConfigFunc: createDefaultConfig,
}
for _, opt := range options {
opt.applyProcessorFactoryOption(f)
}
return f
}
// MakeFactoryMap takes a list of factories and returns a map with Factory type as keys.
// It returns a non-nil error when there are factories with duplicate type.
func MakeFactoryMap(factories ...Factory) (map[component.Type]Factory, error) {
fMap := map[component.Type]Factory{}
for _, f := range factories {
if _, ok := fMap[f.Type()]; ok {
return fMap, fmt.Errorf("duplicate processor factory %q", f.Type())
}
fMap[f.Type()] = f
}
return fMap, nil
}
// Builder processor is a helper struct that given a set of Configs and Factories helps with creating processors.
type Builder struct {
cfgs map[component.ID]component.Config
factories map[component.Type]Factory
}
// NewBuilder creates a new processor.Builder to help with creating components form a set of configs and factories.
func NewBuilder(cfgs map[component.ID]component.Config, factories map[component.Type]Factory) *Builder {
return &Builder{cfgs: cfgs, factories: factories}
}
// CreateTraces creates a Traces processor based on the settings and config.
func (b *Builder) CreateTraces(ctx context.Context, set CreateSettings, next consumer.Traces) (Traces, error) {
cfg, existsCfg := b.cfgs[set.ID]
if !existsCfg {
return nil, fmt.Errorf("processor %q is not configured", set.ID)
}
f, existsFactory := b.factories[set.ID.Type()]
if !existsFactory {
return nil, fmt.Errorf("processor factory not available for: %q", set.ID)
}
logStabilityLevel(set.Logger, f.TracesProcessorStability())
return f.CreateTracesProcessor(ctx, set, cfg, next)
}
// CreateMetrics creates a Metrics processor based on the settings and config.
func (b *Builder) CreateMetrics(ctx context.Context, set CreateSettings, next consumer.Metrics) (Metrics, error) {
cfg, existsCfg := b.cfgs[set.ID]
if !existsCfg {
return nil, fmt.Errorf("processor %q is not configured", set.ID)
}
f, existsFactory := b.factories[set.ID.Type()]
if !existsFactory {
return nil, fmt.Errorf("processor factory not available for: %q", set.ID)
}
logStabilityLevel(set.Logger, f.MetricsProcessorStability())
return f.CreateMetricsProcessor(ctx, set, cfg, next)
}
// CreateLogs creates a Logs processor based on the settings and config.
func (b *Builder) CreateLogs(ctx context.Context, set CreateSettings, next consumer.Logs) (Logs, error) {
cfg, existsCfg := b.cfgs[set.ID]
if !existsCfg {
return nil, fmt.Errorf("processor %q is not configured", set.ID)
}
f, existsFactory := b.factories[set.ID.Type()]
if !existsFactory {
return nil, fmt.Errorf("processor factory not available for: %q", set.ID)
}
logStabilityLevel(set.Logger, f.LogsProcessorStability())
return f.CreateLogsProcessor(ctx, set, cfg, next)
}
func (b *Builder) Factory(componentType component.Type) component.Factory {
return b.factories[componentType]
}
// logStabilityLevel logs the stability level of a component. The log level is set to info for
// undefined, unmaintained, deprecated and development. The log level is set to debug
// for alpha, beta and stable.
func logStabilityLevel(logger *zap.Logger, sl component.StabilityLevel) {
if sl >= component.StabilityLevelAlpha {
logger.Debug(sl.LogMessage())
} else {
logger.Info(sl.LogMessage())
}
}