-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
batch_processor.go
351 lines (297 loc) · 9.85 KB
/
batch_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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// 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 batchprocessor // import "go.opentelemetry.io/collector/processor/batchprocessor"
import (
"context"
"runtime"
"sync"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
"go.uber.org/zap"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/model/otlp"
"go.opentelemetry.io/collector/model/pdata"
)
// batch_processor is a component that accepts spans and metrics, places them
// into batches and sends downstream.
//
// batch_processor implements consumer.Traces and consumer.Metrics
//
// Batches are sent out with any of the following conditions:
// - batch size reaches cfg.SendBatchSize
// - cfg.Timeout is elapsed since the timestamp when the previous batch was sent out.
type batchProcessor struct {
logger *zap.Logger
exportCtx context.Context
timer *time.Timer
timeout time.Duration
sendBatchSize int
sendBatchMaxSize int
newItem chan interface{}
batch batch
shutdownC chan struct{}
goroutines sync.WaitGroup
telemetryLevel configtelemetry.Level
}
type batch interface {
// export the current batch
export(ctx context.Context, sendBatchMaxSize int) error
// itemCount returns the size of the current batch
itemCount() int
// size returns the size in bytes of the current batch
size() int
// add item to the current batch
add(item interface{})
}
var _ consumer.Traces = (*batchProcessor)(nil)
var _ consumer.Metrics = (*batchProcessor)(nil)
var _ consumer.Logs = (*batchProcessor)(nil)
func newBatchProcessor(set component.ProcessorCreateSettings, cfg *Config, batch batch, telemetryLevel configtelemetry.Level) (*batchProcessor, error) {
exportCtx, err := tag.New(context.Background(), tag.Insert(processorTagKey, cfg.ID().String()))
if err != nil {
return nil, err
}
return &batchProcessor{
logger: set.Logger,
exportCtx: exportCtx,
telemetryLevel: telemetryLevel,
sendBatchSize: int(cfg.SendBatchSize),
sendBatchMaxSize: int(cfg.SendBatchMaxSize),
timeout: cfg.Timeout,
newItem: make(chan interface{}, runtime.NumCPU()),
batch: batch,
shutdownC: make(chan struct{}, 1),
}, nil
}
func (bp *batchProcessor) Capabilities() consumer.Capabilities {
return consumer.Capabilities{MutatesData: true}
}
// Start is invoked during service startup.
func (bp *batchProcessor) Start(context.Context, component.Host) error {
bp.goroutines.Add(1)
go bp.startProcessingCycle()
return nil
}
// Shutdown is invoked during service shutdown.
func (bp *batchProcessor) Shutdown(context.Context) error {
close(bp.shutdownC)
// Wait until all goroutines are done.
bp.goroutines.Wait()
return nil
}
func (bp *batchProcessor) startProcessingCycle() {
defer bp.goroutines.Done()
bp.timer = time.NewTimer(bp.timeout)
for {
select {
case <-bp.shutdownC:
DONE:
for {
select {
case item := <-bp.newItem:
bp.processItem(item)
default:
break DONE
}
}
// This is the close of the channel
if bp.batch.itemCount() > 0 {
// TODO: Set a timeout on sendTraces or
// make it cancellable using the context that Shutdown gets as a parameter
bp.sendItems(statTimeoutTriggerSend)
}
return
case item := <-bp.newItem:
if item == nil {
continue
}
bp.processItem(item)
case <-bp.timer.C:
if bp.batch.itemCount() > 0 {
bp.sendItems(statTimeoutTriggerSend)
}
bp.resetTimer()
}
}
}
func (bp *batchProcessor) processItem(item interface{}) {
bp.batch.add(item)
sent := false
for bp.batch.itemCount() >= bp.sendBatchSize {
sent = true
bp.sendItems(statBatchSizeTriggerSend)
}
if sent {
bp.stopTimer()
bp.resetTimer()
}
}
func (bp *batchProcessor) stopTimer() {
if !bp.timer.Stop() {
<-bp.timer.C
}
}
func (bp *batchProcessor) resetTimer() {
bp.timer.Reset(bp.timeout)
}
func (bp *batchProcessor) sendItems(triggerMeasure *stats.Int64Measure) {
// Add that it came form the trace pipeline?
stats.Record(bp.exportCtx, triggerMeasure.M(1), statBatchSendSize.M(int64(bp.batch.itemCount())))
if bp.telemetryLevel == configtelemetry.LevelDetailed {
stats.Record(bp.exportCtx, statBatchSendSizeBytes.M(int64(bp.batch.size())))
}
if err := bp.batch.export(bp.exportCtx, bp.sendBatchMaxSize); err != nil {
bp.logger.Warn("Sender failed", zap.Error(err))
}
}
// ConsumeTraces implements TracesProcessor
func (bp *batchProcessor) ConsumeTraces(_ context.Context, td pdata.Traces) error {
bp.newItem <- td
return nil
}
// ConsumeMetrics implements MetricsProcessor
func (bp *batchProcessor) ConsumeMetrics(_ context.Context, md pdata.Metrics) error {
// First thing is convert into a different internal format
bp.newItem <- md
return nil
}
// ConsumeLogs implements LogsProcessor
func (bp *batchProcessor) ConsumeLogs(_ context.Context, ld pdata.Logs) error {
bp.newItem <- ld
return nil
}
// newBatchTracesProcessor creates a new batch processor that batches traces by size or with timeout
func newBatchTracesProcessor(set component.ProcessorCreateSettings, next consumer.Traces, cfg *Config, telemetryLevel configtelemetry.Level) (*batchProcessor, error) {
return newBatchProcessor(set, cfg, newBatchTraces(next), telemetryLevel)
}
// newBatchMetricsProcessor creates a new batch processor that batches metrics by size or with timeout
func newBatchMetricsProcessor(set component.ProcessorCreateSettings, next consumer.Metrics, cfg *Config, telemetryLevel configtelemetry.Level) (*batchProcessor, error) {
return newBatchProcessor(set, cfg, newBatchMetrics(next), telemetryLevel)
}
// newBatchLogsProcessor creates a new batch processor that batches logs by size or with timeout
func newBatchLogsProcessor(set component.ProcessorCreateSettings, next consumer.Logs, cfg *Config, telemetryLevel configtelemetry.Level) (*batchProcessor, error) {
return newBatchProcessor(set, cfg, newBatchLogs(next), telemetryLevel)
}
type batchTraces struct {
nextConsumer consumer.Traces
traceData pdata.Traces
spanCount int
sizer pdata.TracesSizer
}
func newBatchTraces(nextConsumer consumer.Traces) *batchTraces {
return &batchTraces{nextConsumer: nextConsumer, traceData: pdata.NewTraces(), sizer: otlp.NewProtobufTracesMarshaler().(pdata.TracesSizer)}
}
// add updates current batchTraces by adding new TraceData object
func (bt *batchTraces) add(item interface{}) {
td := item.(pdata.Traces)
newSpanCount := td.SpanCount()
if newSpanCount == 0 {
return
}
bt.spanCount += newSpanCount
td.ResourceSpans().MoveAndAppendTo(bt.traceData.ResourceSpans())
}
func (bt *batchTraces) export(ctx context.Context, sendBatchMaxSize int) error {
var req pdata.Traces
if sendBatchMaxSize > 0 && bt.itemCount() > sendBatchMaxSize {
req = splitTraces(sendBatchMaxSize, bt.traceData)
bt.spanCount -= sendBatchMaxSize
} else {
req = bt.traceData
bt.traceData = pdata.NewTraces()
bt.spanCount = 0
}
return bt.nextConsumer.ConsumeTraces(ctx, req)
}
func (bt *batchTraces) itemCount() int {
return bt.spanCount
}
func (bt *batchTraces) size() int {
return bt.sizer.TracesSize(bt.traceData)
}
type batchMetrics struct {
nextConsumer consumer.Metrics
metricData pdata.Metrics
dataPointCount int
sizer pdata.MetricsSizer
}
func newBatchMetrics(nextConsumer consumer.Metrics) *batchMetrics {
return &batchMetrics{nextConsumer: nextConsumer, metricData: pdata.NewMetrics(), sizer: otlp.NewProtobufMetricsMarshaler().(pdata.MetricsSizer)}
}
func (bm *batchMetrics) export(ctx context.Context, sendBatchMaxSize int) error {
var req pdata.Metrics
if sendBatchMaxSize > 0 && bm.dataPointCount > sendBatchMaxSize {
req = splitMetrics(sendBatchMaxSize, bm.metricData)
bm.dataPointCount -= sendBatchMaxSize
} else {
req = bm.metricData
bm.metricData = pdata.NewMetrics()
bm.dataPointCount = 0
}
return bm.nextConsumer.ConsumeMetrics(ctx, req)
}
func (bm *batchMetrics) itemCount() int {
return bm.dataPointCount
}
func (bm *batchMetrics) size() int {
return bm.sizer.MetricsSize(bm.metricData)
}
func (bm *batchMetrics) add(item interface{}) {
md := item.(pdata.Metrics)
newDataPointCount := md.DataPointCount()
if newDataPointCount == 0 {
return
}
bm.dataPointCount += newDataPointCount
md.ResourceMetrics().MoveAndAppendTo(bm.metricData.ResourceMetrics())
}
type batchLogs struct {
nextConsumer consumer.Logs
logData pdata.Logs
logCount int
sizer pdata.LogsSizer
}
func newBatchLogs(nextConsumer consumer.Logs) *batchLogs {
return &batchLogs{nextConsumer: nextConsumer, logData: pdata.NewLogs(), sizer: otlp.NewProtobufLogsMarshaler().(pdata.LogsSizer)}
}
func (bl *batchLogs) export(ctx context.Context, sendBatchMaxSize int) error {
var req pdata.Logs
if sendBatchMaxSize > 0 && bl.logCount > sendBatchMaxSize {
req = splitLogs(sendBatchMaxSize, bl.logData)
bl.logCount -= sendBatchMaxSize
} else {
req = bl.logData
bl.logData = pdata.NewLogs()
bl.logCount = 0
}
return bl.nextConsumer.ConsumeLogs(ctx, req)
}
func (bl *batchLogs) itemCount() int {
return bl.logCount
}
func (bl *batchLogs) size() int {
return bl.sizer.LogsSize(bl.logData)
}
func (bl *batchLogs) add(item interface{}) {
ld := item.(pdata.Logs)
newLogsCount := ld.LogRecordCount()
if newLogsCount == 0 {
return
}
bl.logCount += newLogsCount
ld.ResourceLogs().MoveAndAppendTo(bl.logData.ResourceLogs())
}