-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
emitter.go
156 lines (131 loc) · 4.14 KB
/
emitter.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
// 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 adapter // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter"
import (
"context"
"sync"
"time"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
)
// LogEmitter is a stanza operator that emits log entries to a channel
type LogEmitter struct {
helper.OutputOperator
logChan chan []*entry.Entry
stopOnce sync.Once
cancel context.CancelFunc
batchMux sync.Mutex
batch []*entry.Entry
wg sync.WaitGroup
maxBatchSize uint
flushInterval time.Duration
}
var (
defaultFlushInterval = 100 * time.Millisecond
defaultMaxBatchSize uint = 100
)
// NewLogEmitter creates a new receiver output
func NewLogEmitter(logger *zap.SugaredLogger) *LogEmitter {
return &LogEmitter{
OutputOperator: helper.OutputOperator{
BasicOperator: helper.BasicOperator{
OperatorID: "log_emitter",
OperatorType: "log_emitter",
SugaredLogger: logger,
},
},
logChan: make(chan []*entry.Entry),
maxBatchSize: defaultMaxBatchSize,
batch: make([]*entry.Entry, 0, defaultMaxBatchSize),
flushInterval: defaultFlushInterval,
cancel: func() {},
}
}
// Start starts the goroutine(s) required for this operator
func (e *LogEmitter) Start(_ operator.Persister) error {
ctx, cancel := context.WithCancel(context.Background())
e.cancel = cancel
e.wg.Add(1)
go e.flusher(ctx)
return nil
}
// Stop will close the log channel and stop running goroutines
func (e *LogEmitter) Stop() error {
e.stopOnce.Do(func() {
e.cancel()
e.wg.Wait()
close(e.logChan)
})
return nil
}
// OutChannel returns the channel on which entries will be sent to.
func (e *LogEmitter) OutChannel() <-chan []*entry.Entry {
return e.logChan
}
// Process will emit an entry to the output channel
func (e *LogEmitter) Process(ctx context.Context, ent *entry.Entry) error {
if oldBatch := e.appendEntry(ent); len(oldBatch) > 0 {
e.flush(ctx, oldBatch)
}
return nil
}
// appendEntry appends the entry to the current batch. If maxBatchSize is reached, a new batch will be made, and the old batch
// (which should be flushed) will be returned
func (e *LogEmitter) appendEntry(ent *entry.Entry) []*entry.Entry {
e.batchMux.Lock()
defer e.batchMux.Unlock()
e.batch = append(e.batch, ent)
if uint(len(e.batch)) >= e.maxBatchSize {
var oldBatch []*entry.Entry
oldBatch, e.batch = e.batch, make([]*entry.Entry, 0, e.maxBatchSize)
return oldBatch
}
return nil
}
// flusher flushes the current batch every flush interval. Intended to be run as a goroutine
func (e *LogEmitter) flusher(ctx context.Context) {
defer e.wg.Done()
ticker := time.NewTicker(e.flushInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if oldBatch := e.makeNewBatch(); len(oldBatch) > 0 {
e.flush(ctx, oldBatch)
}
case <-ctx.Done():
return
}
}
}
// flush flushes the provided batch to the log channel.
func (e *LogEmitter) flush(ctx context.Context, batch []*entry.Entry) {
select {
case e.logChan <- batch:
case <-ctx.Done():
}
}
// makeNewBatch replaces the current batch on the log emitter with a new batch, returning the old one
func (e *LogEmitter) makeNewBatch() []*entry.Entry {
e.batchMux.Lock()
defer e.batchMux.Unlock()
if len(e.batch) == 0 {
return nil
}
var oldBatch []*entry.Entry
oldBatch, e.batch = e.batch, make([]*entry.Entry, 0, e.maxBatchSize)
return oldBatch
}