forked from benthosdev/benthos
/
merge_json.go
177 lines (153 loc) · 5.38 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
// Copyright (c) 2018 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/types"
"github.com/Jeffail/gabs"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeMergeJSON] = TypeSpec{
constructor: NewMergeJSON,
description: `
Parses selected message parts as JSON documents, attempts to merge them into one
single JSON document and then writes it to a new message part at the end of the
message. Merged parts are removed unless ` + "`retain_parts`" + ` is set to
true. The new merged message part will contain the metadata of the first part to
be merged.`,
}
}
//------------------------------------------------------------------------------
// 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
mSucc metrics.StatCounter
mSent metrics.StatCounter
mSentParts 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.NewModule(".processor.merge_json"),
stats: stats,
mCount: stats.GetCounter("processor.merge_json.count"),
mErrJSONP: stats.GetCounter("processor.merge_json.error.json_parse"),
mErrJSONS: stats.GetCounter("processor.merge_json.error.json_set"),
mSucc: stats.GetCounter("processor.merge_json.success"),
mSent: stats.GetCounter("processor.merge_json.sent"),
mSentParts: stats.GetCounter("processor.merge_json.parts.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)
newPart := gabs.New()
mergeFunc := func(index int) {
jsonPart, err := msg.Get(index).JSON()
if err != nil {
p.mErrJSONP.Incr(1)
p.log.Debugf("Failed to parse part into json: %v\n", err)
return
}
var gPart *gabs.Container
if gPart, err = gabs.Consume(jsonPart); err != nil {
p.mErrJSONP.Incr(1)
p.log.Debugf("Failed to parse part into json: %v\n", err)
return
}
newPart.Merge(gPart)
}
var newMsg types.Message
if p.retain {
newMsg = msg.Copy()
} else {
newMsg = message.New(nil)
}
var firstMetadata types.Metadata
if len(p.parts) == 0 {
for i := 0; i < msg.Len(); i++ {
mergeFunc(i)
}
firstMetadata = msg.Get(0).Metadata().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
})
firstMetadata = msg.Get(p.parts[0]).Metadata().Copy()
}
i := newMsg.Append(message.NewPart(nil))
if err := newMsg.Get(i).SetJSON(newPart.Data()); err != nil {
p.mErrJSONS.Incr(1)
p.log.Debugf("Failed to marshal merged part into json: %v\n", err)
} else {
p.mSucc.Incr(1)
}
newMsg.Get(i).SetMetadata(firstMetadata)
msgs := [1]types.Message{newMsg}
p.mSent.Incr(1)
p.mSentParts.Incr(int64(newMsg.Len()))
return msgs[:], nil
}
//------------------------------------------------------------------------------