Skip to content

Commit a07cdfd

Browse files
committed
testing(otel) Testing opentelemetry
1 parent 1474296 commit a07cdfd

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

mongo/document/document.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ import (
77

88
"github.com/sirupsen/logrus"
99

10+
"go.opentelemetry.io/otel"
11+
"go.opentelemetry.io/otel/attribute"
1012
"gopkg.in/mgo.v2"
1113
"gopkg.in/mgo.v2/bson"
1214

1315
"github.com/Scalingo/go-utils/logger"
1416
"github.com/Scalingo/go-utils/mongo"
1517
)
1618

19+
var tracer = otel.Tracer("github.com/Scalingo/go-utils/mongo/document")
20+
1721
type SortField string
1822

1923
type document interface {
@@ -54,6 +58,9 @@ var _ Validable = &Base{}
5458
// Create inserts the document in the database, returns an error if document
5559
// already exists and set CreatedAt timestamp
5660
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()
5764
log := logger.Get(ctx)
5865
doc.ensureID()
5966
doc.ensureCreatedAt()
@@ -73,6 +80,9 @@ func Create(ctx context.Context, collectionName string, doc document) error {
7380
}
7481

7582
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()
7686
log := logger.Get(ctx)
7787
doc.ensureID()
7888
doc.ensureCreatedAt()
@@ -94,10 +104,16 @@ func Save(ctx context.Context, collectionName string, doc document) error {
94104

95105
// Destroy really deletes
96106
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()
97110
return doc.destroy(ctx, collectionName)
98111
}
99112

100113
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()
101117
log := logger.Get(ctx)
102118
c := mongo.Session(log).Clone().DB("").C(collectionName)
103119
defer c.Database.Session.Close()
@@ -112,22 +128,34 @@ func ReallyDestroy(ctx context.Context, collectionName string, doc document) err
112128
// default scope for paranoid documents, it won't look at documents tagged as
113129
// deleted
114130
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()
115134
query := doc.scope(bson.M{"_id": id})
116135
return find(ctx, collectionName, query, doc, sortFields...)
117136
}
118137

119138
// FindUnscoped is similar as Find but does not care of the default scope of
120139
// the document.
121140
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()
122144
query := bson.M{"_id": id}
123145
return find(ctx, collectionName, query, doc, sortFields...)
124146
}
125147

126148
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()
127152
return find(ctx, collectionName, doc.scope(query), doc, sortFields...)
128153
}
129154

130155
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()
131159
return find(ctx, collectionName, query, doc)
132160
}
133161

@@ -144,6 +172,9 @@ func find(ctx context.Context, collectionName string, query bson.M, doc interfac
144172
}
145173

146174
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()
147178
if query == nil {
148179
query = bson.M{}
149180
}
@@ -155,6 +186,9 @@ func WhereQuery(ctx context.Context, collectionName string, query bson.M, sortFi
155186
}
156187

157188
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()
158192
log := logger.Get(ctx)
159193
c := mongo.Session(log).Clone().DB("").C(collectionName)
160194

@@ -170,6 +204,9 @@ func WhereUnscopedQuery(ctx context.Context, collectionName string, query bson.M
170204
}
171205

172206
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()
173210
mongoQuery, session := WhereQuery(ctx, collectionName, query, sortFields...)
174211
defer session.Close()
175212
err := mongoQuery.All(data)
@@ -180,6 +217,9 @@ func Where(ctx context.Context, collectionName string, query bson.M, data interf
180217
}
181218

182219
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()
183223
mongoQuery, session := WhereUnscopedQuery(ctx, collectionName, query, sortFields...)
184224
defer session.Close()
185225
err := mongoQuery.All(data)
@@ -190,6 +230,9 @@ func WhereUnscoped(ctx context.Context, collectionName string, query bson.M, dat
190230
}
191231

192232
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()
193236
if query == nil {
194237
query = bson.M{}
195238
}
@@ -200,6 +243,9 @@ func WhereIter(ctx context.Context, collectionName string, query bson.M, fun fun
200243
}
201244

202245
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()
203249
log := logger.Get(ctx)
204250
c := mongo.Session(log).Clone().DB("").C(collectionName)
205251
defer c.Database.Session.Close()
@@ -226,6 +272,9 @@ func WhereIterUnscoped(ctx context.Context, collectionName string, query bson.M,
226272
}
227273

228274
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()
229278
log := logger.Get(ctx)
230279
c := mongo.Session(log).Clone().DB("").C(collectionName)
231280
defer c.Database.Session.Close()

mongo/document/paranoid.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"time"
66

7+
"go.opentelemetry.io/otel/attribute"
78
"gopkg.in/mgo.v2/bson"
89
)
910

@@ -34,5 +35,8 @@ func (d *Paranoid) destroy(ctx context.Context, collectionName string) error {
3435
}
3536

3637
func Restore(ctx context.Context, collectionName string, doc document) error {
38+
ctx, span := tracer.Start(ctx, "restore")
39+
span.SetAttributes(attribute.String("mongo.collection_name", collectionName))
40+
defer span.End()
3741
return Update(ctx, collectionName, bson.M{"$unset": bson.M{"deleted_at": ""}}, doc)
3842
}

mongo/go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ require (
88
github.com/pkg/errors v0.9.1
99
github.com/sirupsen/logrus v1.9.0
1010
github.com/stretchr/testify v1.8.1
11+
go.opentelemetry.io/otel v1.11.2
1112
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
1213
)
1314

1415
require (
1516
github.com/davecgh/go-spew v1.1.1 // indirect
17+
github.com/go-logr/logr v1.2.3 // indirect
18+
github.com/go-logr/stdr v1.2.2 // indirect
1619
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
1720
github.com/pmezard/go-difflib v1.0.0 // indirect
21+
go.opentelemetry.io/otel/trace v1.11.2 // indirect
1822
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
1923
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
2024
gopkg.in/errgo.v1 v1.0.1 // indirect

mongo/go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
99
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1010
github.com/frankban/quicktest v1.2.2 h1:xfmOhhoH5fGPgbEAlhLpJH9p0z/0Qizio9osmvn9IUY=
1111
github.com/frankban/quicktest v1.2.2/go.mod h1:Qh/WofXFeiAFII1aEBu529AtJo6Zg2VHscnEsbBnJ20=
12+
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
13+
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
14+
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
15+
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
16+
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
1217
github.com/google/go-cmp v0.2.1-0.20190312032427-6f77996f0c42 h1:q3pnF5JFBNRz8sRD+IRj7Y6DMyYGTNqnZ9axTbSfoNI=
1318
github.com/google/go-cmp v0.2.1-0.20190312032427-6f77996f0c42/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
1419
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
@@ -37,6 +42,10 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
3742
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
3843
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
3944
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
45+
go.opentelemetry.io/otel v1.11.2 h1:YBZcQlsVekzFsFbjygXMOXSs6pialIZxcjfO/mBDmR0=
46+
go.opentelemetry.io/otel v1.11.2/go.mod h1:7p4EUV+AqgdlNV9gL97IgUZiVR3yrFXYo53f9BM3tRI=
47+
go.opentelemetry.io/otel/trace v1.11.2 h1:Xf7hWSF2Glv0DE3MH7fBHvtpSBsjcBUe5MYAmZM/+y0=
48+
go.opentelemetry.io/otel/trace v1.11.2/go.mod h1:4N+yC7QEz7TTsG9BSRLNAa63eg5E06ObSbKPmxQ/pKA=
4049
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4150
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
4251
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

0 commit comments

Comments
 (0)