forked from benthosdev/benthos
/
select_parts.go
140 lines (117 loc) · 4.7 KB
/
select_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
// Copyright (c) 2017 Ashley Jeffs
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package processor
import (
"github.com/Jeffail/benthos/lib/log"
"github.com/Jeffail/benthos/lib/message"
"github.com/Jeffail/benthos/lib/metrics"
"github.com/Jeffail/benthos/lib/response"
"github.com/Jeffail/benthos/lib/types"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeSelectParts] = TypeSpec{
constructor: NewSelectParts,
description: `
Cherry pick a set of parts from messages by their index. Indexes larger than the
number of parts are simply ignored.
The selected parts are added to the new message in the same order as the
selection array. E.g. with 'parts' set to [ 2, 0, 1 ] and the message parts
[ '0', '1', '2', '3' ], the output will be [ '2', '0', '1' ].
If none of the selected parts exist in the input message (resulting in an empty
output message) the message is dropped entirely.
Part indexes can be negative, and if so the part will be selected from the end
counting backwards starting from -1. E.g. if index = -1 then the selected part
will be the last part of the message, if index = -2 then the part before the
last element with be selected, and so on.`,
}
}
//------------------------------------------------------------------------------
// SelectPartsConfig contains configuration fields for the SelectParts
// processor.
type SelectPartsConfig struct {
Parts []int `json:"parts" yaml:"parts"`
}
// NewSelectPartsConfig returns a SelectPartsConfig with default values.
func NewSelectPartsConfig() SelectPartsConfig {
return SelectPartsConfig{
Parts: []int{0},
}
}
//------------------------------------------------------------------------------
// SelectParts is a processor that selects parts from a message to append to a
// new message.
type SelectParts struct {
conf Config
log log.Modular
stats metrics.Type
mCount metrics.StatCounter
mSkipped metrics.StatCounter
mSelected metrics.StatCounter
mDropped metrics.StatCounter
mSent metrics.StatCounter
mSentParts metrics.StatCounter
}
// NewSelectParts returns a SelectParts processor.
func NewSelectParts(
conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
) (Type, error) {
return &SelectParts{
conf: conf,
log: log.NewModule(".processor.select_parts"),
stats: stats,
mCount: stats.GetCounter("processor.select_parts.count"),
mSkipped: stats.GetCounter("processor.select_parts.skipped"),
mSelected: stats.GetCounter("processor.select_parts.selected"),
mDropped: stats.GetCounter("processor.select_parts.dropped"),
mSent: stats.GetCounter("processor.select_parts.sent"),
mSentParts: stats.GetCounter("processor.select_parts.parts.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 (m *SelectParts) ProcessMessage(msg types.Message) ([]types.Message, types.Response) {
m.mCount.Incr(1)
newMsg := message.New(nil)
lParts := msg.Len()
for _, index := range m.conf.SelectParts.Parts {
if index < 0 {
// Negative indexes count backwards from the end.
index = lParts + index
}
// Check boundary of part index.
if index < 0 || index >= lParts {
m.mSkipped.Incr(1)
} else {
m.mSelected.Incr(1)
newMsg.Append(msg.Get(index).Copy())
}
}
if newMsg.Len() == 0 {
m.mDropped.Incr(1)
return nil, response.NewAck()
}
m.mSent.Incr(1)
m.mSentParts.Incr(int64(newMsg.Len()))
msgs := [1]types.Message{newMsg}
return msgs[:], nil
}
//------------------------------------------------------------------------------