/
processor.go
237 lines (193 loc) · 8.17 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
// SPDX-License-Identifier: Apache-2.0
package groupbytraceprocessor // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/groupbytraceprocessor"
import (
"context"
"fmt"
"time"
"go.opencensus.io/stats"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/collector/processor"
"go.uber.org/multierr"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchpersignal"
)
// groupByTraceProcessor is a processor that keeps traces in memory for a given duration, with the expectation
// that the trace will be complete once this duration expires. After the duration, the trace is sent to the next consumer.
// This processor uses a buffered event machine, which converts operations into events for non-blocking processing, but
// keeping all operations serialized per worker scope. This ensures that we don't need locks but that the state is consistent across go routines.
// Initially, all incoming batches are split into different traces and distributed among workers by a hash of traceID in eventMachine.consume method.
// Afterwards, the trace is registered with a go routine, which will be called after the given duration and dispatched to the event
// machine for further processing.
// The typical data flow looks like this:
// ConsumeTraces -> eventMachine.consume(trace) -> event(traceReceived) -> onTraceReceived -> AfterFunc(duration, event(traceExpired)) -> onTraceExpired
// async markAsReleased -> event(traceReleased) -> onTraceReleased -> nextConsumer
// Each worker in the eventMachine also uses a ring buffer to hold the in-flight trace IDs, so that we don't hold more than the given maximum number
// of traces in memory/storage. Items that are evicted from the buffer are discarded without warning.
type groupByTraceProcessor struct {
nextConsumer consumer.Traces
config Config
logger *zap.Logger
// the event machine handling all operations for this processor
eventMachine *eventMachine
// the trace storage
st storage
}
var _ processor.Traces = (*groupByTraceProcessor)(nil)
const bufferSize = 10_000
// newGroupByTraceProcessor returns a new processor.
func newGroupByTraceProcessor(logger *zap.Logger, st storage, nextConsumer consumer.Traces, config Config) *groupByTraceProcessor {
// the event machine will buffer up to N concurrent events before blocking
eventMachine := newEventMachine(logger, 10000, config.NumWorkers, config.NumTraces)
sp := &groupByTraceProcessor{
logger: logger,
nextConsumer: nextConsumer,
config: config,
eventMachine: eventMachine,
st: st,
}
// register the callbacks
eventMachine.onTraceReceived = sp.onTraceReceived
eventMachine.onTraceExpired = sp.onTraceExpired
eventMachine.onTraceReleased = sp.onTraceReleased
eventMachine.onTraceRemoved = sp.onTraceRemoved
return sp
}
func (sp *groupByTraceProcessor) ConsumeTraces(_ context.Context, td ptrace.Traces) error {
var errs error
for _, singleTrace := range batchpersignal.SplitTraces(td) {
errs = multierr.Append(errs, sp.eventMachine.consume(singleTrace))
}
return errs
}
func (sp *groupByTraceProcessor) Capabilities() consumer.Capabilities {
return consumer.Capabilities{MutatesData: true}
}
// Start is invoked during service startup.
func (sp *groupByTraceProcessor) Start(context.Context, component.Host) error {
// start these metrics, as it might take a while for them to receive their first event
stats.Record(context.Background(), mTracesEvicted.M(0))
stats.Record(context.Background(), mIncompleteReleases.M(0))
stats.Record(context.Background(), mNumTracesConf.M(int64(sp.config.NumTraces)))
sp.eventMachine.startInBackground()
return sp.st.start()
}
// Shutdown is invoked during service shutdown.
func (sp *groupByTraceProcessor) Shutdown(_ context.Context) error {
sp.eventMachine.shutdown()
return sp.st.shutdown()
}
func (sp *groupByTraceProcessor) onTraceReceived(trace tracesWithID, worker *eventMachineWorker) error {
traceID := trace.id
if worker.buffer.contains(traceID) {
sp.logger.Debug("trace is already in memory storage")
// it exists in memory already, just append the spans to the trace in the storage
if err := sp.addSpans(traceID, trace.td); err != nil {
return fmt.Errorf("couldn't add spans to existing trace: %w", err)
}
// we are done with this trace, move on
return nil
}
// at this point, we determined that we haven't seen the trace yet, so, record the
// traceID in the map and the spans to the storage
// place the trace ID in the buffer, and check if an item had to be evicted
evicted := worker.buffer.put(traceID)
if !evicted.IsEmpty() {
// delete from the storage
worker.fire(event{
typ: traceRemoved,
payload: evicted,
})
stats.Record(context.Background(), mTracesEvicted.M(1))
sp.logger.Info("trace evicted: in order to avoid this in the future, adjust the wait duration and/or number of traces to keep in memory",
zap.Stringer("traceID", evicted))
}
// we have the traceID in the memory, place the spans in the storage too
if err := sp.addSpans(traceID, trace.td); err != nil {
return fmt.Errorf("couldn't add spans to existing trace: %w", err)
}
sp.logger.Debug("scheduled to release trace", zap.Duration("duration", sp.config.WaitDuration))
time.AfterFunc(sp.config.WaitDuration, func() {
// if the event machine has stopped, it will just discard the event
worker.fire(event{
typ: traceExpired,
payload: traceID,
})
})
return nil
}
func (sp *groupByTraceProcessor) onTraceExpired(traceID pcommon.TraceID, worker *eventMachineWorker) error {
sp.logger.Debug("processing expired", zap.Stringer("traceID", traceID))
if !worker.buffer.contains(traceID) {
// we likely received multiple batches with spans for the same trace
// and released this trace already
sp.logger.Debug("skipping the processing of expired trace", zap.Stringer("traceID", traceID))
stats.Record(context.Background(), mIncompleteReleases.M(1))
return nil
}
// delete from the map and erase its memory entry
worker.buffer.delete(traceID)
// this might block, but we don't need to wait
sp.logger.Debug("marking the trace as released", zap.Stringer("traceID", traceID))
go func() {
_ = sp.markAsReleased(traceID, worker.fire)
}()
return nil
}
func (sp *groupByTraceProcessor) markAsReleased(traceID pcommon.TraceID, fire func(...event)) error {
// #get is a potentially blocking operation
trace, err := sp.st.get(traceID)
if err != nil {
return fmt.Errorf("couldn't retrieve trace %q from the storage: %w", traceID, err)
}
if trace == nil {
return fmt.Errorf("the trace %q couldn't be found at the storage", traceID)
}
// signal that the trace is ready to be released
sp.logger.Debug("trace marked as released", zap.Stringer("traceID", traceID))
// atomically fire the two events, so that a concurrent shutdown won't leave
// an orphaned trace in the storage
fire(event{
typ: traceReleased,
payload: trace,
}, event{
typ: traceRemoved,
payload: traceID,
})
return nil
}
func (sp *groupByTraceProcessor) onTraceReleased(rss []ptrace.ResourceSpans) error {
trace := ptrace.NewTraces()
for _, rs := range rss {
trs := trace.ResourceSpans().AppendEmpty()
rs.CopyTo(trs)
}
stats.Record(context.Background(),
mReleasedSpans.M(int64(trace.SpanCount())),
mReleasedTraces.M(1),
)
// Do async consuming not to block event worker
go func() {
if err := sp.nextConsumer.ConsumeTraces(context.Background(), trace); err != nil {
sp.logger.Error("consume failed", zap.Error(err))
}
}()
return nil
}
func (sp *groupByTraceProcessor) onTraceRemoved(traceID pcommon.TraceID) error {
trace, err := sp.st.delete(traceID)
if err != nil {
return fmt.Errorf("couldn't delete trace %q from the storage: %w", traceID, err)
}
if trace == nil {
return fmt.Errorf("trace %q not found at the storage", traceID)
}
return nil
}
func (sp *groupByTraceProcessor) addSpans(traceID pcommon.TraceID, trace ptrace.Traces) error {
sp.logger.Debug("creating trace at the storage", zap.Stringer("traceID", traceID))
return sp.st.createOrAppend(traceID, trace)
}