-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathprocessor.go
196 lines (159 loc) · 5.01 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package logstransformprocessor // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/logstransformprocessor"
import (
"context"
"errors"
"math"
"runtime"
"sync"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"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 logsTransformProcessor struct {
logger *zap.Logger
config *Config
consumer consumer.Logs
pipe *pipeline.DirectedPipeline
firstOperator operator.Operator
emitter *adapter.LogEmitter
converter *adapter.Converter
fromConverter *adapter.FromPdataConverter
wg sync.WaitGroup
}
func newProcessor(config *Config, nextConsumer consumer.Logs, logger *zap.Logger) (*logsTransformProcessor, error) {
p := &logsTransformProcessor{
logger: logger,
config: config,
consumer: nextConsumer,
}
baseCfg := p.config.BaseConfig
p.emitter = adapter.NewLogEmitter(p.logger.Sugar())
pipe, err := pipeline.Config{
Operators: baseCfg.Operators,
DefaultOutput: p.emitter,
}.Build(p.logger.Sugar())
if err != nil {
return nil, err
}
p.pipe = pipe
return p, nil
}
func (ltp *logsTransformProcessor) Capabilities() consumer.Capabilities {
return consumer.Capabilities{MutatesData: true}
}
func (ltp *logsTransformProcessor) Shutdown(_ 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, _ component.Host) error {
// There is no need for this processor to use storage
err := ltp.pipe.Start(storage.NewNopClient())
if err != nil {
return err
}
pipelineOperators := ltp.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())))
ltp.converter = adapter.NewConverter(ltp.logger)
ltp.converter.Start()
ltp.fromConverter = adapter.NewFromPdataConverter(wkrCount, ltp.logger)
ltp.fromConverter.Start()
// 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 next consumer
ltp.wg.Add(1)
go ltp.consumerLoop(ctx)
return nil
}
func (ltp *logsTransformProcessor) ConsumeLogs(_ context.Context, ld plog.Logs) error {
// Add the logs to the chain
return ltp.fromConverter.Batch(ld)
}
// 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.logger.Error("processor encountered an issue with the pipeline", zap.Error(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.logger.Error("processor encountered an issue with the converter", zap.Error(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
}
if err := ltp.consumer.ConsumeLogs(ctx, pLogs); err != nil {
ltp.logger.Error("processor encountered an issue with next consumer", zap.Error(err))
}
}
}
}