forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_json.go
179 lines (152 loc) · 4.55 KB
/
merge_json.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
package processor
import (
"time"
"github.com/Jeffail/gabs/v2"
"github.com/dafanshu/benthos/v3/internal/docs"
"github.com/dafanshu/benthos/v3/internal/tracing"
"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/types"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeMergeJSON] = TypeSpec{
constructor: NewMergeJSON,
Status: docs.StatusDeprecated,
Footnotes: `
## Alternatives
All functionality of this processor has been superseded by the
[bloblang](/docs/components/processors/bloblang) processor.`,
UsesBatches: true,
FieldSpecs: docs.FieldSpecs{
docs.FieldCommon("retain_parts", "Whether messages that are merged should also have their original contents preserved."),
PartsFieldSpec,
},
}
}
//------------------------------------------------------------------------------
// MergeJSONConfig contains configuration fields for the MergeJSON processor.
type MergeJSONConfig struct {
Parts []int `json:"parts" yaml:"parts"`
RetainParts bool `json:"retain_parts" yaml:"retain_parts"`
}
// NewMergeJSONConfig returns a MergeJSONConfig with default values.
func NewMergeJSONConfig() MergeJSONConfig {
return MergeJSONConfig{
Parts: []int{},
RetainParts: false,
}
}
//------------------------------------------------------------------------------
// MergeJSON is a processor that merges JSON parsed message parts into a single
// value.
type MergeJSON struct {
parts []int
retain bool
log log.Modular
stats metrics.Type
mCount metrics.StatCounter
mErrJSONP metrics.StatCounter
mErrJSONS metrics.StatCounter
mErr metrics.StatCounter
mSent metrics.StatCounter
mBatchSent metrics.StatCounter
}
// NewMergeJSON returns a MergeJSON processor.
func NewMergeJSON(
conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
) (Type, error) {
j := &MergeJSON{
parts: conf.MergeJSON.Parts,
retain: conf.MergeJSON.RetainParts,
log: log,
stats: stats,
mCount: stats.GetCounter("count"),
mErrJSONP: stats.GetCounter("error.json_parse"),
mErrJSONS: stats.GetCounter("error.json_set"),
mErr: stats.GetCounter("error"),
mSent: stats.GetCounter("sent"),
mBatchSent: stats.GetCounter("batch.sent"),
}
return j, 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 *MergeJSON) ProcessMessage(msg types.Message) ([]types.Message, types.Response) {
p.mCount.Incr(1)
spans := tracing.CreateChildSpans(TypeMergeJSON, msg)
defer func() {
for _, s := range spans {
s.Finish()
}
}()
newPart := gabs.New()
mergeFunc := func(index int) {
jsonPart, err := msg.Get(index).JSON()
if err == nil {
jsonPart, err = message.CopyJSON(jsonPart)
}
if err != nil {
p.mErrJSONP.Incr(1)
p.mErr.Incr(1)
p.log.Debugf("Failed to parse part into json: %v\n", err)
return
}
gPart := gabs.Wrap(jsonPart)
newPart.Merge(gPart)
}
var newMsg types.Message
if p.retain {
newMsg = msg.Copy()
} else {
newMsg = message.New(nil)
}
var firstPartCopy types.Part
if len(p.parts) == 0 {
for i := 0; i < msg.Len(); i++ {
mergeFunc(i)
}
firstPartCopy = msg.Get(0).Copy()
} else {
targetParts := make(map[int]struct{}, len(p.parts))
for _, part := range p.parts {
if part < 0 {
part = msg.Len() + part
}
if part < 0 || part >= msg.Len() {
continue
}
targetParts[part] = struct{}{}
}
msg.Iter(func(i int, b types.Part) error {
if _, isTarget := targetParts[i]; isTarget {
mergeFunc(i)
} else if !p.retain {
newMsg.Append(b.Copy())
}
return nil
})
firstPartCopy = msg.Get(p.parts[0]).Copy()
}
i := newMsg.Append(firstPartCopy)
if err := newMsg.Get(i).SetJSON(newPart.Data()); err != nil {
p.mErrJSONS.Incr(1)
p.mErr.Incr(1)
p.log.Debugf("Failed to marshal merged part into json: %v\n", err)
FlagErr(newMsg.Get(i), err)
}
msgs := [1]types.Message{newMsg}
p.mBatchSent.Incr(1)
p.mSent.Incr(int64(newMsg.Len()))
return msgs[:], nil
}
// CloseAsync shuts down the processor and stops processing requests.
func (p *MergeJSON) CloseAsync() {
}
// WaitForClose blocks until the processor has closed down.
func (p *MergeJSON) WaitForClose(timeout time.Duration) error {
return nil
}
//------------------------------------------------------------------------------