forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.go
63 lines (54 loc) · 1.34 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
package tracing
import (
"sync/atomic"
"time"
"github.com/dafanshu/benthos/v3/lib/processor"
"github.com/dafanshu/benthos/v3/lib/types"
)
type tracedProcessor struct {
e *events
errCtr *uint64
wrapped types.Processor
}
func traceProcessor(e *events, errCtr *uint64, p types.Processor) types.Processor {
t := &tracedProcessor{
e: e,
errCtr: errCtr,
wrapped: p,
}
return t
}
func (t *tracedProcessor) ProcessMessage(m types.Message) ([]types.Message, types.Response) {
prevErrs := make([]string, m.Len())
_ = m.Iter(func(i int, part types.Part) error {
t.e.Add(EventConsume, string(part.Get()))
prevErrs[i] = processor.GetFail(part)
return nil
})
outMsgs, res := t.wrapped.ProcessMessage(m)
for _, outMsg := range outMsgs {
_ = outMsg.Iter(func(i int, part types.Part) error {
t.e.Add(EventProduce, string(part.Get()))
failStr := processor.GetFail(part)
if failStr == "" {
return nil
}
if len(prevErrs) <= i || prevErrs[i] == failStr {
return nil
}
_ = atomic.AddUint64(t.errCtr, 1)
t.e.Add(EventError, failStr)
return nil
})
}
if len(outMsgs) == 0 {
t.e.Add(EventDelete, "")
}
return outMsgs, res
}
func (t *tracedProcessor) CloseAsync() {
t.wrapped.CloseAsync()
}
func (t *tracedProcessor) WaitForClose(timeout time.Duration) error {
return t.wrapped.WaitForClose(timeout)
}