/
insert_part.go
147 lines (123 loc) · 4.19 KB
/
insert_part.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
package processor
import (
"fmt"
"time"
"github.com/Jeffail/benthos/v3/internal/bloblang"
"github.com/Jeffail/benthos/v3/internal/bloblang/field"
"github.com/Jeffail/benthos/v3/internal/docs"
"github.com/Jeffail/benthos/v3/lib/log"
"github.com/Jeffail/benthos/v3/lib/message"
"github.com/Jeffail/benthos/v3/lib/metrics"
"github.com/Jeffail/benthos/v3/lib/types"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeInsertPart] = TypeSpec{
constructor: NewInsertPart,
Categories: []Category{
CategoryComposition,
},
Summary: `
Insert a new message into a batch at an index. If the specified index is greater
than the length of the existing batch it will be appended to the end.`,
Description: `
The index can be negative, and if so the message will be inserted from the end
counting backwards starting from -1. E.g. if index = -1 then the new message
will become the last of the batch, if index = -2 then the new message will be
inserted before the last message, and so on. If the negative index is greater
than the length of the existing batch it will be inserted at the beginning.
The new message will have metadata copied from the first pre-existing message of
the batch.
This processor will interpolate functions within the 'content' field, you can
find a list of functions [here](/docs/configuration/interpolation#bloblang-queries).`,
FieldSpecs: docs.FieldSpecs{
docs.FieldCommon("index", "The index within the batch to insert the message at."),
docs.FieldCommon("content", "The content of the message being inserted.").IsInterpolated(),
},
}
}
//------------------------------------------------------------------------------
// InsertPartConfig contains configuration fields for the InsertPart processor.
type InsertPartConfig struct {
Index int `json:"index" yaml:"index"`
Content string `json:"content" yaml:"content"`
}
// NewInsertPartConfig returns a InsertPartConfig with default values.
func NewInsertPartConfig() InsertPartConfig {
return InsertPartConfig{
Index: -1,
Content: "",
}
}
//------------------------------------------------------------------------------
// InsertPart is a processor that inserts a new message part at a specific
// index.
type InsertPart struct {
part *field.Expression
conf Config
log log.Modular
stats metrics.Type
mCount metrics.StatCounter
mSent metrics.StatCounter
mBatchSent metrics.StatCounter
}
// NewInsertPart returns a InsertPart processor.
func NewInsertPart(
conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
) (Type, error) {
part, err := bloblang.NewField(conf.InsertPart.Content)
if err != nil {
return nil, fmt.Errorf("failed to parse content expression: %v", err)
}
return &InsertPart{
part: part,
conf: conf,
log: log,
stats: stats,
mCount: stats.GetCounter("count"),
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 (p *InsertPart) ProcessMessage(msg types.Message) ([]types.Message, types.Response) {
p.mCount.Incr(1)
newPartBytes := p.part.Bytes(0, msg)
index := p.conf.InsertPart.Index
msgLen := msg.Len()
if index < 0 {
index = msgLen + index + 1
if index < 0 {
index = 0
}
} else if index > msgLen {
index = msgLen
}
newMsg := message.New(nil)
newPart := msg.Get(0).Copy()
newPart.Set(newPartBytes)
msg.Iter(func(i int, p types.Part) error {
if i == index {
newMsg.Append(newPart)
}
newMsg.Append(p.Copy())
return nil
})
if index == msg.Len() {
newMsg.Append(newPart)
}
p.mBatchSent.Incr(1)
p.mSent.Incr(int64(newMsg.Len()))
msgs := [1]types.Message{newMsg}
return msgs[:], nil
}
// CloseAsync shuts down the processor and stops processing requests.
func (p *InsertPart) CloseAsync() {
}
// WaitForClose blocks until the processor has closed down.
func (p *InsertPart) WaitForClose(timeout time.Duration) error {
return nil
}
//------------------------------------------------------------------------------