-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb_stream_benthos.go
207 lines (160 loc) · 4.52 KB
/
mongodb_stream_benthos.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package mongodb_stream_benthos
import (
"context"
"encoding/json"
"github.com/benthosdev/benthos/v4/public/service"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var mongoStreamConfigSpec = service.NewConfigSpec().
Summary("Creates an input that generates mongo stream").
Field(service.NewStringField("uri")).
Field(service.NewStringField("database")).
Field(service.NewStringField("collection")).
Field(service.NewBoolField("stream_snapshot"))
type mongoStreamInput struct {
uri string
database string
collection string
client *mongo.Client
stream chan map[string]interface{}
streamSnapshot bool
}
func newMongoStreamInput(conf *service.ParsedConfig) (service.Input, error) {
var (
uri string
database string
collection string
streamSnapshot bool
)
uri, err := conf.FieldString("uri")
if err != nil {
return nil, err
}
database, err = conf.FieldString("database")
if err != nil {
return nil, err
}
collection, err = conf.FieldString("collection")
if err != nil {
return nil, err
}
streamSnapshot, err = conf.FieldBool("stream_snapshot")
if err != nil {
return nil, err
}
return service.AutoRetryNacks(&mongoStreamInput{
uri: uri,
database: database,
collection: collection,
streamSnapshot: streamSnapshot,
stream: make(chan map[string]interface{}),
}), nil
}
func init() {
err := service.RegisterInput(
"mongodb_stream",
mongoStreamConfigSpec,
func(conf *service.ParsedConfig, mgr *service.Resources) (service.Input, error) {
return newMongoStreamInput(conf)
},
)
if err != nil {
panic(err)
}
}
func (m *mongoStreamInput) Connect(ctx context.Context) error {
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(m.uri))
m.client = client
if err != nil {
panic(err)
}
database := client.Database(m.database)
collection := database.Collection(m.collection)
if m.streamSnapshot {
go m.takeSnapshot(collection)
} else {
stream, err := collection.Watch(context.TODO(), mongo.Pipeline{}, options.ChangeStream().SetFullDocument(options.UpdateLookup))
if err != nil {
panic(err)
}
go m.iterateChangeStream(stream)
}
return nil
}
func (m *mongoStreamInput) takeSnapshot(c *mongo.Collection) {
cursor, err := c.Find(context.TODO(), bson.D{})
if err != nil {
panic(err)
}
m.iterateCursor(cursor)
stream, err := c.Watch(context.TODO(), mongo.Pipeline{}, options.ChangeStream().SetFullDocument(options.UpdateLookup))
if err != nil {
panic(err)
}
m.iterateChangeStream(stream)
}
func (m *mongoStreamInput) iterateCursor(cursor *mongo.Cursor) {
defer cursor.Close(context.TODO())
for cursor.Next(context.TODO()) {
var data bson.M
if err := cursor.Decode(&data); err != nil {
panic(err)
}
m.processMessage(data)
}
if err := cursor.Err(); err != nil {
panic(err)
}
}
func (m *mongoStreamInput) iterateChangeStream(stream *mongo.ChangeStream) {
defer stream.Close(context.TODO())
for stream.Next(context.TODO()) {
var data bson.M
if err := stream.Decode(&data); err != nil {
panic(err)
}
m.processMessage(data)
}
}
func (m *mongoStreamInput) processMessage(data map[string]interface{}) {
event := make(map[string]interface{})
if operation, ok := data["operationType"]; ok {
switch operation {
case "delete":
event["action"] = operation
event["database"] = m.database
event["collection"] = m.collection
event["data"] = data["documentKey"]
default:
event["action"] = operation
event["database"] = m.database
event["collection"] = m.collection
event["data"] = data["fullDocument"]
}
} else {
event["action"] = "insert"
event["database"] = m.database
event["collection"] = m.collection
event["data"] = data
}
m.stream <- event
}
func (m *mongoStreamInput) Close(ctx context.Context) error {
if m.client != nil {
return m.client.Disconnect(context.TODO())
}
return nil
}
func (m *mongoStreamInput) Read(ctx context.Context) (*service.Message, service.AckFunc, error) {
snapshotMessage := <-m.stream
messageBodyEncoded, _ := json.Marshal(snapshotMessage["data"])
createdMessage := service.NewMessage(messageBodyEncoded)
createdMessage.MetaSet("table", snapshotMessage["collection"].(string))
createdMessage.MetaSet("schema", snapshotMessage["database"].(string))
createdMessage.MetaSet("event", snapshotMessage["action"].(string))
return createdMessage, func(ctx context.Context, err error) error {
return nil
}, nil
}