forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.go
237 lines (202 loc) · 6.32 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
// 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
//
// https://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 logstransformprocessor // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/logstransformprocessor"
import (
"context"
"errors"
"fmt"
"math"
"runtime"
"sync"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/extension/experimental/storage"
"go.opentelemetry.io/collector/pdata/plog"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/pipeline"
)
type outputType struct {
logs plog.Logs
err error
}
type logsTransformProcessor struct {
logger *zap.Logger
config *Config
id config.ComponentID
pipe *pipeline.DirectedPipeline
firstOperator operator.Operator
emitter *adapter.LogEmitter
converter *adapter.Converter
fromConverter *adapter.FromPdataConverter
wg sync.WaitGroup
outputChannel chan outputType
}
func (ltp *logsTransformProcessor) Shutdown(ctx context.Context) error {
ltp.logger.Info("Stopping logs transform processor")
pipelineErr := ltp.pipe.Stop()
ltp.converter.Stop()
ltp.fromConverter.Stop()
ltp.wg.Wait()
return pipelineErr
}
func (ltp *logsTransformProcessor) Start(ctx context.Context, host component.Host) error {
baseCfg := ltp.config.BaseConfig
operators, err := baseCfg.DecodeOperatorConfigs()
if err != nil {
return err
}
emitterOpts := []adapter.LogEmitterOption{
adapter.LogEmitterWithLogger(ltp.logger.Sugar()),
}
if baseCfg.Converter.MaxFlushCount > 0 {
emitterOpts = append(emitterOpts, adapter.LogEmitterWithMaxBatchSize(baseCfg.Converter.MaxFlushCount))
}
if baseCfg.Converter.FlushInterval > 0 {
emitterOpts = append(emitterOpts, adapter.LogEmitterWithFlushInterval(baseCfg.Converter.FlushInterval))
}
ltp.emitter = adapter.NewLogEmitter(emitterOpts...)
pipe, err := pipeline.Config{
Operators: operators,
DefaultOutput: ltp.emitter,
}.Build(ltp.logger.Sugar())
if err != nil {
return err
}
// There is no need for this processor to use storage
err = pipe.Start(adapter.GetPersister(storage.NewNopClient()))
if err != nil {
return err
}
ltp.pipe = pipe
pipelineOperators := pipe.Operators()
if len(pipelineOperators) == 0 {
return errors.New("processor requires at least one operator to be configured")
}
ltp.firstOperator = pipelineOperators[0]
wkrCount := int(math.Max(1, float64(runtime.NumCPU())))
if baseCfg.Converter.WorkerCount > 0 {
wkrCount = baseCfg.Converter.WorkerCount
}
ltp.converter = adapter.NewConverter(
adapter.WithLogger(ltp.logger),
adapter.WithWorkerCount(wkrCount),
)
ltp.converter.Start()
ltp.fromConverter = adapter.NewFromPdataConverter(wkrCount, ltp.logger)
ltp.fromConverter.Start()
ltp.outputChannel = make(chan outputType)
// Below we're starting 3 loops:
// * first which reads all the logs translated by the fromConverter and then forwards
// them to pipeline
// ...
ltp.wg.Add(1)
go ltp.converterLoop(ctx)
// * second which reads all the logs modified by the pipeline and then forwards
// them to converter
// ...
ltp.wg.Add(1)
go ltp.emitterLoop(ctx)
// ...
// * third which reads all the logs produced by the converter
// (aggregated by Resource) and then places them on the outputChannel
ltp.wg.Add(1)
go ltp.consumerLoop(ctx)
return nil
}
func (ltp *logsTransformProcessor) processLogs(ctx context.Context, ld plog.Logs) (plog.Logs, error) {
// Add the logs to the chain
err := ltp.fromConverter.Batch(ld)
if err != nil {
return ld, err
}
doneChan := ctx.Done()
for {
select {
case <-doneChan:
ltp.logger.Debug("loop stopped")
return ld, errors.New("processor interrupted")
case output, ok := <-ltp.outputChannel:
if !ok {
return ld, errors.New("processor encountered an issue receiving logs from stanza operators pipeline")
}
if output.err != nil {
return ld, err
}
return output.logs, nil
}
}
}
// converterLoop reads the log entries produced by the fromConverter and sends them
// into the pipeline
func (ltp *logsTransformProcessor) converterLoop(ctx context.Context) {
defer ltp.wg.Done()
for {
select {
case <-ctx.Done():
ltp.logger.Debug("converter loop stopped")
return
case entries, ok := <-ltp.fromConverter.OutChannel():
if !ok {
ltp.logger.Debug("fromConverter channel got closed")
return
}
for _, e := range entries {
// Add item to the first operator of the pipeline manually
if err := ltp.firstOperator.Process(ctx, e); err != nil {
ltp.outputChannel <- outputType{err: fmt.Errorf("processor encountered an issue with the pipeline: %w", err)}
break
}
}
}
}
}
// emitterLoop reads the log entries produced by the emitter and batches them
// in converter.
func (ltp *logsTransformProcessor) emitterLoop(ctx context.Context) {
defer ltp.wg.Done()
for {
select {
case <-ctx.Done():
ltp.logger.Debug("emitter loop stopped")
return
case e, ok := <-ltp.emitter.OutChannel():
if !ok {
ltp.logger.Debug("emitter channel got closed")
return
}
if err := ltp.converter.Batch(e); err != nil {
ltp.outputChannel <- outputType{err: fmt.Errorf("processor encountered an issue with the converter: %w", err)}
}
}
}
}
// consumerLoop reads converter log entries and calls the consumer to consumer them.
func (ltp *logsTransformProcessor) consumerLoop(ctx context.Context) {
defer ltp.wg.Done()
for {
select {
case <-ctx.Done():
ltp.logger.Debug("consumer loop stopped")
return
case pLogs, ok := <-ltp.converter.OutChannel():
if !ok {
ltp.logger.Debug("converter channel got closed")
return
}
ltp.outputChannel <- outputType{logs: pLogs, err: nil}
}
}
}