/
processor.go
240 lines (206 loc) · 7.4 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
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package processorhelper
import (
"context"
"errors"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/obsreport"
)
// Start specifies the function invoked when the processor is being started.
type Start func(context.Context, component.Host) error
// Shutdown specifies the function invoked when the processor is being shutdown.
type Shutdown func(context.Context) error
// TProcessor is a helper interface that allows avoiding implementing all functions in TraceProcessor by using NewTraceProcessor.
type TProcessor interface {
// ProcessTraces is a helper function that processes the incoming data and returns the data to be sent to the next component.
// If error is returned then returned data are ignored. It MUST not call the next component.
ProcessTraces(context.Context, pdata.Traces) (pdata.Traces, error)
}
// MProcessor is a helper interface that allows avoiding implementing all functions in MetricsProcessor by using NewTraceProcessor.
type MProcessor interface {
// ProcessMetrics is a helper function that processes the incoming data and returns the data to be sent to the next component.
// If error is returned then returned data are ignored. It MUST not call the next component.
ProcessMetrics(context.Context, pdata.Metrics) (pdata.Metrics, error)
}
// LProcessor is a helper interface that allows avoiding implementing all functions in LogsProcessor by using NewLogsProcessor.
type LProcessor interface {
// ProcessLogs is a helper function that processes the incoming data and returns the data to be sent to the next component.
// If error is returned then returned data are ignored. It MUST not call the next component.
ProcessLogs(context.Context, pdata.Logs) (pdata.Logs, error)
}
// Option apply changes to internalOptions.
type Option func(*baseProcessor)
// WithStart overrides the default Start function for an processor.
// The default shutdown function does nothing and always returns nil.
func WithStart(start Start) Option {
return func(o *baseProcessor) {
o.start = start
}
}
// WithShutdown overrides the default Shutdown function for an processor.
// The default shutdown function does nothing and always returns nil.
func WithShutdown(shutdown Shutdown) Option {
return func(o *baseProcessor) {
o.shutdown = shutdown
}
}
// WithShutdown overrides the default GetCapabilities function for an processor.
// The default GetCapabilities function returns mutable capabilities.
func WithCapabilities(capabilities component.ProcessorCapabilities) Option {
return func(o *baseProcessor) {
o.capabilities = capabilities
}
}
// internalOptions contains internalOptions concerning how an Processor is configured.
type baseProcessor struct {
fullName string
start Start
shutdown Shutdown
capabilities component.ProcessorCapabilities
}
// Construct the internalOptions from multiple Option.
func newBaseProcessor(fullName string, options ...Option) baseProcessor {
be := baseProcessor{
fullName: fullName,
capabilities: component.ProcessorCapabilities{MutatesConsumedData: true},
}
for _, op := range options {
op(&be)
}
return be
}
// Start the processor, invoked during service start.
func (bp *baseProcessor) Start(ctx context.Context, host component.Host) error {
if bp.start != nil {
return bp.start(ctx, host)
}
return nil
}
func (bp *baseProcessor) GetCapabilities() component.ProcessorCapabilities {
return bp.capabilities
}
// Shutdown the processor, invoked during service shutdown.
func (bp *baseProcessor) Shutdown(ctx context.Context) error {
if bp.shutdown != nil {
return bp.shutdown(ctx)
}
return nil
}
type tracesProcessor struct {
baseProcessor
processor TProcessor
nextConsumer consumer.TraceConsumer
}
func (mp *tracesProcessor) ConsumeTraces(ctx context.Context, td pdata.Traces) error {
processorCtx := obsreport.ProcessorContext(ctx, mp.fullName)
var err error
td, err = mp.processor.ProcessTraces(processorCtx, td)
if err != nil {
return err
}
return mp.nextConsumer.ConsumeTraces(ctx, td)
}
// NewTraceProcessor creates a TraceProcessor that ensure context propagation and the right tags are set.
// TODO: Add observability metrics support
func NewTraceProcessor(
config configmodels.Processor,
nextConsumer consumer.TraceConsumer,
processor TProcessor,
options ...Option,
) (component.TraceProcessor, error) {
if processor == nil {
return nil, errors.New("nil processor")
}
if nextConsumer == nil {
return nil, componenterror.ErrNilNextConsumer
}
return &tracesProcessor{
baseProcessor: newBaseProcessor(config.Name(), options...),
processor: processor,
nextConsumer: nextConsumer,
}, nil
}
type metricsProcessor struct {
baseProcessor
processor MProcessor
nextConsumer consumer.MetricsConsumer
}
func (mp *metricsProcessor) ConsumeMetrics(ctx context.Context, md pdata.Metrics) error {
processorCtx := obsreport.ProcessorContext(ctx, mp.fullName)
var err error
md, err = mp.processor.ProcessMetrics(processorCtx, md)
if err != nil {
return err
}
return mp.nextConsumer.ConsumeMetrics(ctx, md)
}
// NewMetricsProcessor creates a MetricsProcessor that ensure context propagation and the right tags are set.
// TODO: Add observability metrics support
func NewMetricsProcessor(
config configmodels.Processor,
nextConsumer consumer.MetricsConsumer,
processor MProcessor,
options ...Option,
) (component.MetricsProcessor, error) {
if processor == nil {
return nil, errors.New("nil processor")
}
if nextConsumer == nil {
return nil, componenterror.ErrNilNextConsumer
}
return &metricsProcessor{
baseProcessor: newBaseProcessor(config.Name(), options...),
processor: processor,
nextConsumer: nextConsumer,
}, nil
}
type logProcessor struct {
baseProcessor
processor LProcessor
nextConsumer consumer.LogsConsumer
}
func (lp *logProcessor) ConsumeLogs(ctx context.Context, ld pdata.Logs) error {
processorCtx := obsreport.ProcessorContext(ctx, lp.fullName)
var err error
ld, err = lp.processor.ProcessLogs(processorCtx, ld)
if err != nil {
return err
}
return lp.nextConsumer.ConsumeLogs(ctx, ld)
}
// NewLogsProcessor creates a LogsProcessor that ensure context propagation and the right tags are set.
// TODO: Add observability metrics support
func NewLogsProcessor(
config configmodels.Processor,
nextConsumer consumer.LogsConsumer,
processor LProcessor,
options ...Option,
) (component.LogsProcessor, error) {
if processor == nil {
return nil, errors.New("nil processor")
}
if nextConsumer == nil {
return nil, componenterror.ErrNilNextConsumer
}
return &logProcessor{
baseProcessor: newBaseProcessor(config.Name(), options...),
processor: processor,
nextConsumer: nextConsumer,
}, nil
}