@@ -7,13 +7,17 @@ import (
7
7
8
8
"github.com/sirupsen/logrus"
9
9
10
+ "go.opentelemetry.io/otel"
11
+ "go.opentelemetry.io/otel/attribute"
10
12
"gopkg.in/mgo.v2"
11
13
"gopkg.in/mgo.v2/bson"
12
14
13
15
"github.com/Scalingo/go-utils/logger"
14
16
"github.com/Scalingo/go-utils/mongo"
15
17
)
16
18
19
+ var tracer = otel .Tracer ("github.com/Scalingo/go-utils/mongo/document" )
20
+
17
21
type SortField string
18
22
19
23
type document interface {
@@ -54,6 +58,9 @@ var _ Validable = &Base{}
54
58
// Create inserts the document in the database, returns an error if document
55
59
// already exists and set CreatedAt timestamp
56
60
func Create (ctx context.Context , collectionName string , doc document ) error {
61
+ ctx , span := tracer .Start (ctx , "create" )
62
+ span .SetAttributes (attribute .String ("mongo.collection_name" , collectionName ))
63
+ defer span .End ()
57
64
log := logger .Get (ctx )
58
65
doc .ensureID ()
59
66
doc .ensureCreatedAt ()
@@ -73,6 +80,9 @@ func Create(ctx context.Context, collectionName string, doc document) error {
73
80
}
74
81
75
82
func Save (ctx context.Context , collectionName string , doc document ) error {
83
+ ctx , span := tracer .Start (ctx , "save" )
84
+ span .SetAttributes (attribute .String ("mongo.collection_name" , collectionName ))
85
+ defer span .End ()
76
86
log := logger .Get (ctx )
77
87
doc .ensureID ()
78
88
doc .ensureCreatedAt ()
@@ -94,10 +104,16 @@ func Save(ctx context.Context, collectionName string, doc document) error {
94
104
95
105
// Destroy really deletes
96
106
func Destroy (ctx context.Context , collectionName string , doc destroyable ) error {
107
+ ctx , span := tracer .Start (ctx , "destroy" )
108
+ span .SetAttributes (attribute .String ("mongo.collection_name" , collectionName ))
109
+ defer span .End ()
97
110
return doc .destroy (ctx , collectionName )
98
111
}
99
112
100
113
func ReallyDestroy (ctx context.Context , collectionName string , doc document ) error {
114
+ ctx , span := tracer .Start (ctx , "really_destroy" )
115
+ span .SetAttributes (attribute .String ("mongo.collection_name" , collectionName ))
116
+ defer span .End ()
101
117
log := logger .Get (ctx )
102
118
c := mongo .Session (log ).Clone ().DB ("" ).C (collectionName )
103
119
defer c .Database .Session .Close ()
@@ -112,22 +128,34 @@ func ReallyDestroy(ctx context.Context, collectionName string, doc document) err
112
128
// default scope for paranoid documents, it won't look at documents tagged as
113
129
// deleted
114
130
func Find (ctx context.Context , collectionName string , id bson.ObjectId , doc scopable , sortFields ... SortField ) error {
131
+ ctx , span := tracer .Start (ctx , "find" )
132
+ span .SetAttributes (attribute .String ("mongo.collection_name" , collectionName ))
133
+ defer span .End ()
115
134
query := doc .scope (bson.M {"_id" : id })
116
135
return find (ctx , collectionName , query , doc , sortFields ... )
117
136
}
118
137
119
138
// FindUnscoped is similar as Find but does not care of the default scope of
120
139
// the document.
121
140
func FindUnscoped (ctx context.Context , collectionName string , id bson.ObjectId , doc interface {}, sortFields ... SortField ) error {
141
+ ctx , span := tracer .Start (ctx , "find_unscoped" )
142
+ span .SetAttributes (attribute .String ("mongo.collection_name" , collectionName ))
143
+ defer span .End ()
122
144
query := bson.M {"_id" : id }
123
145
return find (ctx , collectionName , query , doc , sortFields ... )
124
146
}
125
147
126
148
func FindOne (ctx context.Context , collectionName string , query bson.M , doc scopable , sortFields ... SortField ) error {
149
+ ctx , span := tracer .Start (ctx , "find_one" )
150
+ span .SetAttributes (attribute .String ("mongo.collection_name" , collectionName ))
151
+ defer span .End ()
127
152
return find (ctx , collectionName , doc .scope (query ), doc , sortFields ... )
128
153
}
129
154
130
155
func FindOneUnscoped (ctx context.Context , collectionName string , query bson.M , doc interface {}) error {
156
+ ctx , span := tracer .Start (ctx , "find_unscoped" )
157
+ span .SetAttributes (attribute .String ("mongo.collection_name" , collectionName ))
158
+ defer span .End ()
131
159
return find (ctx , collectionName , query , doc )
132
160
}
133
161
@@ -144,6 +172,9 @@ func find(ctx context.Context, collectionName string, query bson.M, doc interfac
144
172
}
145
173
146
174
func WhereQuery (ctx context.Context , collectionName string , query bson.M , sortFields ... SortField ) (* mgo.Query , Closer ) {
175
+ ctx , span := tracer .Start (ctx , "where_query" )
176
+ span .SetAttributes (attribute .String ("mongo.collection_name" , collectionName ))
177
+ defer span .End ()
147
178
if query == nil {
148
179
query = bson.M {}
149
180
}
@@ -155,6 +186,9 @@ func WhereQuery(ctx context.Context, collectionName string, query bson.M, sortFi
155
186
}
156
187
157
188
func WhereUnscopedQuery (ctx context.Context , collectionName string , query bson.M , sortFields ... SortField ) (* mgo.Query , Closer ) {
189
+ ctx , span := tracer .Start (ctx , "where_unscoped_query" )
190
+ span .SetAttributes (attribute .String ("mongo.collection_name" , collectionName ))
191
+ defer span .End ()
158
192
log := logger .Get (ctx )
159
193
c := mongo .Session (log ).Clone ().DB ("" ).C (collectionName )
160
194
@@ -170,6 +204,9 @@ func WhereUnscopedQuery(ctx context.Context, collectionName string, query bson.M
170
204
}
171
205
172
206
func Where (ctx context.Context , collectionName string , query bson.M , data interface {}, sortFields ... SortField ) error {
207
+ ctx , span := tracer .Start (ctx , "where" )
208
+ span .SetAttributes (attribute .String ("mongo.collection_name" , collectionName ))
209
+ defer span .End ()
173
210
mongoQuery , session := WhereQuery (ctx , collectionName , query , sortFields ... )
174
211
defer session .Close ()
175
212
err := mongoQuery .All (data )
@@ -180,6 +217,9 @@ func Where(ctx context.Context, collectionName string, query bson.M, data interf
180
217
}
181
218
182
219
func WhereUnscoped (ctx context.Context , collectionName string , query bson.M , data interface {}, sortFields ... SortField ) error {
220
+ ctx , span := tracer .Start (ctx , "where_unscoped" )
221
+ span .SetAttributes (attribute .String ("mongo.collection_name" , collectionName ))
222
+ defer span .End ()
183
223
mongoQuery , session := WhereUnscopedQuery (ctx , collectionName , query , sortFields ... )
184
224
defer session .Close ()
185
225
err := mongoQuery .All (data )
@@ -190,6 +230,9 @@ func WhereUnscoped(ctx context.Context, collectionName string, query bson.M, dat
190
230
}
191
231
192
232
func WhereIter (ctx context.Context , collectionName string , query bson.M , fun func (* mgo.Iter ) error , sortFields ... SortField ) error {
233
+ ctx , span := tracer .Start (ctx , "where_iter" )
234
+ span .SetAttributes (attribute .String ("mongo.collection_name" , collectionName ))
235
+ defer span .End ()
193
236
if query == nil {
194
237
query = bson.M {}
195
238
}
@@ -200,6 +243,9 @@ func WhereIter(ctx context.Context, collectionName string, query bson.M, fun fun
200
243
}
201
244
202
245
func WhereIterUnscoped (ctx context.Context , collectionName string , query bson.M , fun func (* mgo.Iter ) error , sortFields ... SortField ) error {
246
+ ctx , span := tracer .Start (ctx , "where_iter_unscoped" )
247
+ span .SetAttributes (attribute .String ("mongo.collection_name" , collectionName ))
248
+ defer span .End ()
203
249
log := logger .Get (ctx )
204
250
c := mongo .Session (log ).Clone ().DB ("" ).C (collectionName )
205
251
defer c .Database .Session .Close ()
@@ -226,6 +272,9 @@ func WhereIterUnscoped(ctx context.Context, collectionName string, query bson.M,
226
272
}
227
273
228
274
func Update (ctx context.Context , collectionName string , update bson.M , doc document ) error {
275
+ ctx , span := tracer .Start (ctx , "update" )
276
+ span .SetAttributes (attribute .String ("mongo.collection_name" , collectionName ))
277
+ defer span .End ()
229
278
log := logger .Get (ctx )
230
279
c := mongo .Session (log ).Clone ().DB ("" ).C (collectionName )
231
280
defer c .Database .Session .Close ()
0 commit comments