forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
auto_observed.go
258 lines (220 loc) · 8.03 KB
/
auto_observed.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
package processor
import (
"context"
"time"
"github.com/nehal119/benthos-119/pkg/component"
"github.com/nehal119/benthos-119/pkg/component/metrics"
"github.com/nehal119/benthos-119/pkg/log"
"github.com/nehal119/benthos-119/pkg/message"
"github.com/nehal119/benthos-119/pkg/tracing"
)
// AutoObserved is a simpler processor interface to implement than V1 as it is
// not required to emit observability information within the implementation
// itself.
type AutoObserved interface {
// Process a message into one or more resulting messages, or return an error
// if one occurred during processing, in which case the message will
// continue unchanged except for having that error now affiliated with it.
//
// If zero messages are returned and the error is nil then the message is
// filtered.
Process(ctx context.Context, p *message.Part) ([]*message.Part, error)
// Close the component, blocks until either the underlying resources are
// cleaned up or the context is cancelled. Returns an error if the context
// is cancelled.
Close(ctx context.Context) error
}
// AutoObservedBatched is a simpler processor interface to implement than V1 as
// it is not required to emit observability information within the
// implementation itself.
type AutoObservedBatched interface {
// Process a batch of messages into one or more resulting batches, or return
// an error if one occurred during processing, in which case all messages
// will continue unchanged except for having that error now affiliated with
// them.
//
// In order to associate individual messages with an error please use
// ctx.OnError instead of msg.ErrorSet. They are similar, but using
// ctx.OnError ensures observability data is updated as well as the message
// being affiliated with the error.
//
// If zero message batches are returned and the error is nil then all
// messages are filtered.
ProcessBatch(ctx *BatchProcContext, b message.Batch) ([]message.Batch, error)
// Close the component, blocks until either the underlying resources are
// cleaned up or the context is cancelled. Returns an error if the context
// is cancelled.
Close(ctx context.Context) error
}
//------------------------------------------------------------------------------
// Implements V1.
type v2ToV1Processor struct {
typeStr string
p AutoObserved
mgr component.Observability
mReceived metrics.StatCounter
mBatchReceived metrics.StatCounter
mSent metrics.StatCounter
mBatchSent metrics.StatCounter
mError metrics.StatCounter
mLatency metrics.StatTimer
}
// NewAutoObservedProcessor wraps an AutoObserved processor with an
// implementation of V1 which handles observability information.
func NewAutoObservedProcessor(typeStr string, p AutoObserved, mgr component.Observability) V1 {
return &v2ToV1Processor{
typeStr: typeStr, p: p, mgr: mgr,
mReceived: mgr.Metrics().GetCounter("processor_received"),
mBatchReceived: mgr.Metrics().GetCounter("processor_batch_received"),
mSent: mgr.Metrics().GetCounter("processor_sent"),
mBatchSent: mgr.Metrics().GetCounter("processor_batch_sent"),
mError: mgr.Metrics().GetCounter("processor_error"),
mLatency: mgr.Metrics().GetTimer("processor_latency_ns"),
}
}
func (a *v2ToV1Processor) ProcessBatch(ctx context.Context, msg message.Batch) ([]message.Batch, error) {
a.mReceived.Incr(int64(msg.Len()))
a.mBatchReceived.Incr(1)
tStarted := time.Now()
newParts := make([]*message.Part, 0, msg.Len())
_ = msg.Iter(func(i int, part *message.Part) error {
_, span := tracing.WithChildSpan(a.mgr.Tracer(), a.typeStr, part)
nextParts, err := a.p.Process(ctx, part)
if err != nil {
a.mError.Incr(1)
a.mgr.Logger().Debugf("Processor failed: %v", err)
MarkErr(part, span, err)
nextParts = append(nextParts, part)
}
span.Finish()
if len(nextParts) > 0 {
newParts = append(newParts, nextParts...)
}
return nil
})
a.mLatency.Timing(time.Since(tStarted).Nanoseconds())
if len(newParts) == 0 {
return nil, nil
}
a.mSent.Incr(int64(len(newParts)))
a.mBatchSent.Incr(1)
return []message.Batch{newParts}, nil
}
func (a *v2ToV1Processor) Close(ctx context.Context) error {
return a.p.Close(ctx)
}
//------------------------------------------------------------------------------
// TestBatchProcContext creates a context for batch processors. It's safe to
// provide nil spans and parts functions for testing purposes.
func TestBatchProcContext(ctx context.Context, spans []*tracing.Span, parts []*message.Part) *BatchProcContext {
return &BatchProcContext{
ctx: ctx,
spans: spans,
parts: parts,
}
}
// BatchProcContext provides methods for triggering observability updates and
// accessing processor specific spans.
type BatchProcContext struct {
ctx context.Context
spans []*tracing.Span
parts []*message.Part
mError metrics.StatCounter
logger log.Modular
}
// Context returns the underlying processor context.Context.
func (b *BatchProcContext) Context() context.Context {
return b.ctx
}
// Span returns a span created specifically for the invocation of the processor.
// This can be used in order to add context to what the processor did.
func (b *BatchProcContext) Span(index int) *tracing.Span {
if len(b.spans) <= index {
return nil
}
return b.spans[index]
}
// OnError should be called when an individual message has encountered an error,
// this should be used instead of .ErrorSet() as it includes observability
// updates.
//
// This method can be called with index -1 in order to set generalised
// observability information without marking specific message errors.
func (b *BatchProcContext) OnError(err error, index int, p *message.Part) {
if b.mError != nil {
b.mError.Incr(1)
}
if b.logger != nil {
b.logger.Debugf("Processor failed: %v", err)
}
var span *tracing.Span
if len(b.spans) > index && index >= 0 {
span = b.spans[index]
}
if p == nil && len(b.parts) > index && index >= 0 {
p = b.parts[index]
}
MarkErr(p, span, err)
}
// Implements types.Processor.
type v2BatchedToV1Processor struct {
typeStr string
p AutoObservedBatched
mgr component.Observability
mReceived metrics.StatCounter
mBatchReceived metrics.StatCounter
mSent metrics.StatCounter
mBatchSent metrics.StatCounter
mError metrics.StatCounter
mLatency metrics.StatTimer
}
// NewAutoObservedBatchProcessor wraps an AutoObservedBatched processor with an
// implementation of V1 which handles observability information.
func NewAutoObservedBatchedProcessor(typeStr string, p AutoObservedBatched, mgr component.Observability) V1 {
return &v2BatchedToV1Processor{
typeStr: typeStr, p: p, mgr: mgr,
mReceived: mgr.Metrics().GetCounter("processor_received"),
mBatchReceived: mgr.Metrics().GetCounter("processor_batch_received"),
mSent: mgr.Metrics().GetCounter("processor_sent"),
mBatchSent: mgr.Metrics().GetCounter("processor_batch_sent"),
mError: mgr.Metrics().GetCounter("processor_error"),
mLatency: mgr.Metrics().GetTimer("processor_latency_ns"),
}
}
func (a *v2BatchedToV1Processor) ProcessBatch(ctx context.Context, msg message.Batch) ([]message.Batch, error) {
a.mReceived.Incr(int64(msg.Len()))
a.mBatchReceived.Incr(1)
tStarted := time.Now()
_, spans := tracing.WithChildSpans(a.mgr.Tracer(), a.typeStr, msg)
outputBatches, err := a.p.ProcessBatch(&BatchProcContext{
ctx: ctx,
spans: spans,
parts: msg,
mError: a.mError,
logger: a.mgr.Logger(),
}, msg)
if err != nil {
a.mError.Incr(int64(msg.Len()))
a.mgr.Logger().Debugf("Processor failed: %v", err)
_ = msg.Iter(func(i int, p *message.Part) error {
MarkErr(p, spans[i], err)
return nil
})
outputBatches = append(outputBatches, msg)
}
for _, s := range spans {
s.Finish()
}
a.mLatency.Timing(time.Since(tStarted).Nanoseconds())
if len(outputBatches) == 0 {
return nil, nil
}
for _, m := range outputBatches {
a.mSent.Incr(int64(m.Len()))
}
a.mBatchSent.Incr(int64(len(outputBatches)))
return outputBatches, nil
}
func (a *v2BatchedToV1Processor) Close(ctx context.Context) error {
return a.p.Close(ctx)
}