/
mgowrapper.go
316 lines (258 loc) · 8.64 KB
/
mgowrapper.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package mgohttp
import (
"context"
"fmt"
"strings"
opentracing "github.com/opentracing/opentracing-go"
opentracinglog "github.com/opentracing/opentracing-go/log"
mgo "gopkg.in/mgo.v2"
bson "gopkg.in/mgo.v2/bson"
)
type tracedMgoSession struct {
sess *mgo.Session
ctx context.Context
}
func (ts tracedMgoSession) DB(name string) MongoDatabase {
sp := opentracing.SpanFromContext(ts.ctx)
sp.SetTag("db-name", name)
return tracedMgoDatabase{
db: ts.sess.DB(name),
ctx: opentracing.ContextWithSpan(ts.ctx, sp),
}
}
func (ts tracedMgoSession) Ping() error {
sp, _ := opentracing.StartSpanFromContext(ts.ctx, "ping")
defer sp.Finish()
return logAndReturnErr(sp, ts.sess.Ping())
}
type tracedMgoDatabase struct {
db *mgo.Database
ctx context.Context
}
func (t tracedMgoDatabase) C(collection string) MongoCollection {
return tracedMgoCollection{
collectionName: collection,
collection: t.db.C(collection),
ctx: t.ctx,
}
}
func (t tracedMgoDatabase) Run(cmd interface{}, result interface{}) error {
sp, _ := opentracing.StartSpanFromContext(t.ctx, "run")
defer sp.Finish()
sp.LogKV(opentracinglog.String("cmd", fmt.Sprintf("%#v", cmd)))
return logAndReturnErr(sp, t.db.Run(cmd, result))
}
type tracedMgoCollection struct {
collectionName string
collection *mgo.Collection
ctx context.Context
}
func (tc tracedMgoCollection) UpdateId(id bson.ObjectId, update interface{}) error {
return tc.Update(bson.M{"_id": id}, update)
}
func (tc tracedMgoCollection) Update(selector interface{}, update interface{}) error {
sp, _ := opentracing.StartSpanFromContext(tc.ctx, "update")
sp.SetTag("collection", tc.collectionName)
sp.LogFields(bsonToKeys("selector", selector))
sp.LogFields(bsonToKeys("update", update))
defer sp.Finish()
return logAndReturnErr(sp, tc.collection.Update(selector, update))
}
func (tc tracedMgoCollection) UpdateAll(selector interface{}, update interface{}) (info *mgo.ChangeInfo, err error) {
sp, _ := opentracing.StartSpanFromContext(tc.ctx, "update-all")
sp.SetTag("collection", tc.collectionName)
sp.LogFields(bsonToKeys("selector", selector))
sp.LogFields(bsonToKeys("update", update))
defer sp.Finish()
info, err = tc.collection.UpdateAll(selector, update)
return info, logAndReturnErr(sp, err)
}
func (tc tracedMgoCollection) Insert(docs ...interface{}) (err error) {
sp, _ := opentracing.StartSpanFromContext(tc.ctx, "insert")
sp.LogFields(opentracinglog.Int("num-docs", len(docs)))
defer sp.Finish()
return logAndReturnErr(sp, tc.collection.Insert(docs...))
}
func (tc tracedMgoCollection) Upsert(selector interface{}, update interface{}) (info *mgo.ChangeInfo, err error) {
sp, _ := opentracing.StartSpanFromContext(tc.ctx, "upsert")
sp.LogFields(bsonToKeys("selector", selector))
sp.LogFields(bsonToKeys("update", update))
defer sp.Finish()
info, err = tc.collection.Upsert(selector, update)
return info, logAndReturnErr(sp, err)
}
func (tc tracedMgoCollection) FindId(id bson.ObjectId) MongoQuery {
return tc.Find(bson.M{"_id": id})
}
func (tc tracedMgoCollection) Find(selector interface{}) MongoQuery {
sp, ctx := opentracing.StartSpanFromContext(tc.ctx, "find")
sp.SetTag("collection", tc.collectionName)
// NOTE: Find just starts the trace, the finishing call on the MongoQuery must
// finish it.
sp.LogFields(bsonToKeys("selector", selector))
return tracedMongoQuery{
q: tc.collection.Find(selector),
ctx: ctx,
}
}
func (tc tracedMgoCollection) RemoveId(id bson.ObjectId) error {
return tc.Remove(bson.M{"_id": id})
}
func (tc tracedMgoCollection) Remove(selector interface{}) error {
sp, _ := opentracing.StartSpanFromContext(tc.ctx, "remove")
sp.SetTag("collection", tc.collectionName)
sp.LogFields(bsonToKeys("selector", selector))
defer sp.Finish()
return logAndReturnErr(sp, tc.collection.Remove(selector))
}
func (tc tracedMgoCollection) RemoveAll(selector interface{}) (info *mgo.ChangeInfo, err error) {
sp, _ := opentracing.StartSpanFromContext(tc.ctx, "removeall")
sp.SetTag("collection", tc.collectionName)
sp.LogFields(bsonToKeys("selector", selector))
defer sp.Finish()
info, err = tc.collection.RemoveAll(selector)
return info, logAndReturnErr(sp, err)
}
type tracedMongoQuery struct {
q *mgo.Query
ctx context.Context
}
func (q tracedMongoQuery) All(result interface{}) error {
sp := opentracing.SpanFromContext(q.ctx)
defer sp.Finish()
sp.SetTag("access-method", "All")
return logAndReturnErr(sp, q.q.All(result))
}
func (q tracedMongoQuery) One(result interface{}) (err error) {
sp := opentracing.SpanFromContext(q.ctx)
defer sp.Finish()
sp.SetTag("access-method", "One")
return logAndReturnErr(sp, q.q.One(result))
}
func (q tracedMongoQuery) Count() (int, error) {
sp := opentracing.SpanFromContext(q.ctx)
defer sp.Finish()
sp.SetTag("access-method", "Count")
n, err := q.q.Count()
return n, logAndReturnErr(sp, err)
}
func (q tracedMongoQuery) Limit(n int) MongoQuery {
// NOTE: this function just modifies the query, we will rely on
// One/All to terminate the span.
sp := opentracing.SpanFromContext(q.ctx)
sp.LogFields(opentracinglog.Int("query-limit", n))
return tracedMongoQuery{
q: q.q.Limit(n),
ctx: opentracing.ContextWithSpan(q.ctx, sp),
}
}
func (q tracedMongoQuery) Select(selector interface{}) MongoQuery {
// NOTE: this function just modifies the query, we will rely on
// One/All to terminate the span.
sp := opentracing.SpanFromContext(q.ctx)
sp.LogFields(bsonToKeys("select", selector))
return tracedMongoQuery{
q: q.q.Select(selector),
ctx: opentracing.ContextWithSpan(q.ctx, sp),
}
}
func (q tracedMongoQuery) Hint(indexKey ...string) MongoQuery {
// NOTE: this function just modifies the query, we will rely on
// One/All to terminate the span.
sp := opentracing.SpanFromContext(q.ctx)
for i, hint := range indexKey {
sp.LogFields(opentracinglog.String(fmt.Sprintf("hint.%d", i), hint))
}
return tracedMongoQuery{
q: q.q.Hint(indexKey...),
ctx: opentracing.ContextWithSpan(q.ctx, sp),
}
}
func (q tracedMongoQuery) Sort(fields ...string) MongoQuery {
// NOTE: this function just modifies the query, we will rely on
// One/All to terminate the span.
sp := opentracing.SpanFromContext(q.ctx)
sp.SetTag("sort", strings.Join(fields, "|"))
return tracedMongoQuery{
q: q.q.Sort(fields...),
ctx: opentracing.ContextWithSpan(q.ctx, sp),
}
}
func (q tracedMongoQuery) Apply(change mgo.Change, result interface{}) (info *mgo.ChangeInfo, err error) {
sp := opentracing.SpanFromContext(q.ctx)
defer sp.Finish()
sp.SetTag("access-method", "apply")
sp.LogFields(bsonToKeys("update", change.Update))
sp.LogFields(
opentracinglog.Bool("remove", change.Remove),
opentracinglog.Bool("return-new", change.ReturnNew),
opentracinglog.Bool("upsert", change.Upsert),
)
info, err = q.q.Apply(change, result)
return info, logAndReturnErr(sp, err)
}
func (q tracedMongoQuery) Iter() MongoIter {
_, ctx := opentracing.StartSpanFromContext(q.ctx, "iter")
return tracedMongoIter{
i: q.q.Iter(),
ctx: ctx,
}
}
type tracedMongoIter struct {
i *mgo.Iter
ctx context.Context
}
func (t tracedMongoIter) All(result interface{}) error {
sp, _ := opentracing.StartSpanFromContext(t.ctx, "iter-all")
defer sp.Finish()
return logAndReturnErr(sp, t.i.All(result))
}
func (t tracedMongoIter) Close() error {
sp := opentracing.SpanFromContext(t.ctx)
defer sp.Finish()
return logAndReturnErr(sp, t.i.Close())
}
func (t tracedMongoIter) Done() bool {
return t.i.Done()
}
func (t tracedMongoIter) Err() error {
return logAndReturnErr(opentracing.SpanFromContext(t.ctx), t.i.Err())
}
func (t tracedMongoIter) Next(result interface{}) bool {
sp, _ := opentracing.StartSpanFromContext(t.ctx, "iter-next")
defer sp.Finish()
return t.i.Next(result)
}
// logAndReturnErr is a tiny helper for adding the error to a log inline.
func logAndReturnErr(sp opentracing.Span, err error) error {
sp.LogFields(opentracinglog.Error(err))
return err
}
func getKeys(prefix string, q bson.M) []string {
addPrefix := func(s string) string {
if prefix == "" {
return s
}
return prefix + "." + s
}
fields := []string{}
for k, v := range q {
switch val := v.(type) {
case bson.M:
fields = append(fields, getKeys(addPrefix(k), val)...)
default:
fields = append(fields, addPrefix(k))
}
}
return fields
}
// bsonToKeys transforms an arbitrary mgo arg into a set of log fields.
// This is mostly geared towards bson.M, but the Sprintf fallback should handle arrays
// sufficiently for tracing purposes.
func bsonToKeys(name string, query interface{}) opentracinglog.Field {
queryFields := []string{}
if q, ok := query.(bson.M); ok {
queryFields = getKeys("", q)
}
return opentracinglog.String(name, strings.Join(queryFields, "|"))
}