forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_parts.go
141 lines (116 loc) · 3.81 KB
/
filter_parts.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
package processor
import (
"fmt"
"time"
"github.com/dafanshu/benthos/v3/internal/docs"
"github.com/dafanshu/benthos/v3/internal/tracing"
"github.com/dafanshu/benthos/v3/lib/condition"
"github.com/dafanshu/benthos/v3/lib/log"
"github.com/dafanshu/benthos/v3/lib/message"
"github.com/dafanshu/benthos/v3/lib/metrics"
"github.com/dafanshu/benthos/v3/lib/response"
"github.com/dafanshu/benthos/v3/lib/types"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeFilterParts] = TypeSpec{
constructor: NewFilterParts,
Status: docs.StatusDeprecated,
Footnotes: `
## Alternatives
All functionality of this processor has been superseded by the
[bloblang](/docs/components/processors/bloblang) processor.`,
config: docs.FieldComponent().HasType(docs.FieldTypeCondition),
}
}
//------------------------------------------------------------------------------
// FilterPartsConfig contains configuration fields for the FilterParts
// processor.
type FilterPartsConfig struct {
condition.Config `json:",inline" yaml:",inline"`
}
// NewFilterPartsConfig returns a FilterPartsConfig with default values.
func NewFilterPartsConfig() FilterPartsConfig {
return FilterPartsConfig{
Config: condition.NewConfig(),
}
}
//------------------------------------------------------------------------------
// MarshalYAML prints the child condition instead of {}.
func (f FilterPartsConfig) MarshalYAML() (interface{}, error) {
return f.Config, nil
}
//------------------------------------------------------------------------------
// FilterParts is a processor that checks each part from a message against a
// condition and removes the part if the condition returns false.
type FilterParts struct {
log log.Modular
stats metrics.Type
condition condition.Type
mCount metrics.StatCounter
mDropped metrics.StatCounter
mSent metrics.StatCounter
mBatchSent metrics.StatCounter
}
// NewFilterParts returns a FilterParts processor.
func NewFilterParts(
conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
) (Type, error) {
cond, err := condition.New(conf.FilterParts.Config, mgr, log, stats)
if err != nil {
return nil, fmt.Errorf(
"failed to construct condition '%v': %v",
conf.FilterParts.Config.Type, err,
)
}
return &FilterParts{
log: log,
stats: stats,
condition: cond,
mCount: stats.GetCounter("count"),
mDropped: stats.GetCounter("dropped"),
mSent: stats.GetCounter("sent"),
mBatchSent: stats.GetCounter("batch.sent"),
}, nil
}
//------------------------------------------------------------------------------
// ProcessMessage applies the processor to a message, either creating >0
// resulting messages or a response to be sent back to the message source.
func (c *FilterParts) ProcessMessage(msg types.Message) ([]types.Message, types.Response) {
c.mCount.Incr(1)
newMsg := message.New(nil)
spans := tracing.CreateChildSpans(TypeFilterParts, msg)
defer func() {
for _, s := range spans {
s.Finish()
}
}()
for i := 0; i < msg.Len(); i++ {
if c.condition.Check(message.Lock(msg, i)) {
newMsg.Append(msg.Get(i).Copy())
spans[i].SetTag("result", true)
} else {
spans[i].LogKV(
"event", "dropped",
"type", "filtered",
)
spans[i].SetTag("result", false)
c.mDropped.Incr(1)
}
}
if newMsg.Len() > 0 {
c.mBatchSent.Incr(1)
c.mSent.Incr(int64(newMsg.Len()))
msgs := [1]types.Message{newMsg}
return msgs[:], nil
}
return nil, response.NewAck()
}
// CloseAsync shuts down the processor and stops processing requests.
func (c *FilterParts) CloseAsync() {
}
// WaitForClose blocks until the processor has closed down.
func (c *FilterParts) WaitForClose(timeout time.Duration) error {
return nil
}
//------------------------------------------------------------------------------