Skip to content

Commit

Permalink
testing(otel) Testing opentelemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
john-scalingo committed Jan 12, 2023
1 parent 1474296 commit a07cdfd
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
49 changes: 49 additions & 0 deletions mongo/document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (

"github.com/sirupsen/logrus"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"

"github.com/Scalingo/go-utils/logger"
"github.com/Scalingo/go-utils/mongo"
)

var tracer = otel.Tracer("github.com/Scalingo/go-utils/mongo/document")

type SortField string

type document interface {
Expand Down Expand Up @@ -54,6 +58,9 @@ var _ Validable = &Base{}
// Create inserts the document in the database, returns an error if document
// already exists and set CreatedAt timestamp
func Create(ctx context.Context, collectionName string, doc document) error {
ctx, span := tracer.Start(ctx, "create")
span.SetAttributes(attribute.String("mongo.collection_name", collectionName))
defer span.End()
log := logger.Get(ctx)
doc.ensureID()
doc.ensureCreatedAt()
Expand All @@ -73,6 +80,9 @@ func Create(ctx context.Context, collectionName string, doc document) error {
}

func Save(ctx context.Context, collectionName string, doc document) error {
ctx, span := tracer.Start(ctx, "save")
span.SetAttributes(attribute.String("mongo.collection_name", collectionName))
defer span.End()
log := logger.Get(ctx)
doc.ensureID()
doc.ensureCreatedAt()
Expand All @@ -94,10 +104,16 @@ func Save(ctx context.Context, collectionName string, doc document) error {

// Destroy really deletes
func Destroy(ctx context.Context, collectionName string, doc destroyable) error {
ctx, span := tracer.Start(ctx, "destroy")
span.SetAttributes(attribute.String("mongo.collection_name", collectionName))
defer span.End()
return doc.destroy(ctx, collectionName)
}

func ReallyDestroy(ctx context.Context, collectionName string, doc document) error {
ctx, span := tracer.Start(ctx, "really_destroy")
span.SetAttributes(attribute.String("mongo.collection_name", collectionName))
defer span.End()
log := logger.Get(ctx)
c := mongo.Session(log).Clone().DB("").C(collectionName)
defer c.Database.Session.Close()
Expand All @@ -112,22 +128,34 @@ func ReallyDestroy(ctx context.Context, collectionName string, doc document) err
// default scope for paranoid documents, it won't look at documents tagged as
// deleted
func Find(ctx context.Context, collectionName string, id bson.ObjectId, doc scopable, sortFields ...SortField) error {
ctx, span := tracer.Start(ctx, "find")
span.SetAttributes(attribute.String("mongo.collection_name", collectionName))
defer span.End()
query := doc.scope(bson.M{"_id": id})
return find(ctx, collectionName, query, doc, sortFields...)
}

// FindUnscoped is similar as Find but does not care of the default scope of
// the document.
func FindUnscoped(ctx context.Context, collectionName string, id bson.ObjectId, doc interface{}, sortFields ...SortField) error {
ctx, span := tracer.Start(ctx, "find_unscoped")
span.SetAttributes(attribute.String("mongo.collection_name", collectionName))
defer span.End()
query := bson.M{"_id": id}
return find(ctx, collectionName, query, doc, sortFields...)
}

func FindOne(ctx context.Context, collectionName string, query bson.M, doc scopable, sortFields ...SortField) error {
ctx, span := tracer.Start(ctx, "find_one")
span.SetAttributes(attribute.String("mongo.collection_name", collectionName))
defer span.End()
return find(ctx, collectionName, doc.scope(query), doc, sortFields...)
}

func FindOneUnscoped(ctx context.Context, collectionName string, query bson.M, doc interface{}) error {
ctx, span := tracer.Start(ctx, "find_unscoped")
span.SetAttributes(attribute.String("mongo.collection_name", collectionName))
defer span.End()
return find(ctx, collectionName, query, doc)
}

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

func WhereQuery(ctx context.Context, collectionName string, query bson.M, sortFields ...SortField) (*mgo.Query, Closer) {
ctx, span := tracer.Start(ctx, "where_query")
span.SetAttributes(attribute.String("mongo.collection_name", collectionName))
defer span.End()
if query == nil {
query = bson.M{}
}
Expand All @@ -155,6 +186,9 @@ func WhereQuery(ctx context.Context, collectionName string, query bson.M, sortFi
}

func WhereUnscopedQuery(ctx context.Context, collectionName string, query bson.M, sortFields ...SortField) (*mgo.Query, Closer) {
ctx, span := tracer.Start(ctx, "where_unscoped_query")
span.SetAttributes(attribute.String("mongo.collection_name", collectionName))
defer span.End()
log := logger.Get(ctx)
c := mongo.Session(log).Clone().DB("").C(collectionName)

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

func Where(ctx context.Context, collectionName string, query bson.M, data interface{}, sortFields ...SortField) error {
ctx, span := tracer.Start(ctx, "where")
span.SetAttributes(attribute.String("mongo.collection_name", collectionName))
defer span.End()
mongoQuery, session := WhereQuery(ctx, collectionName, query, sortFields...)
defer session.Close()
err := mongoQuery.All(data)
Expand All @@ -180,6 +217,9 @@ func Where(ctx context.Context, collectionName string, query bson.M, data interf
}

func WhereUnscoped(ctx context.Context, collectionName string, query bson.M, data interface{}, sortFields ...SortField) error {
ctx, span := tracer.Start(ctx, "where_unscoped")
span.SetAttributes(attribute.String("mongo.collection_name", collectionName))
defer span.End()
mongoQuery, session := WhereUnscopedQuery(ctx, collectionName, query, sortFields...)
defer session.Close()
err := mongoQuery.All(data)
Expand All @@ -190,6 +230,9 @@ func WhereUnscoped(ctx context.Context, collectionName string, query bson.M, dat
}

func WhereIter(ctx context.Context, collectionName string, query bson.M, fun func(*mgo.Iter) error, sortFields ...SortField) error {
ctx, span := tracer.Start(ctx, "where_iter")
span.SetAttributes(attribute.String("mongo.collection_name", collectionName))
defer span.End()
if query == nil {
query = bson.M{}
}
Expand All @@ -200,6 +243,9 @@ func WhereIter(ctx context.Context, collectionName string, query bson.M, fun fun
}

func WhereIterUnscoped(ctx context.Context, collectionName string, query bson.M, fun func(*mgo.Iter) error, sortFields ...SortField) error {
ctx, span := tracer.Start(ctx, "where_iter_unscoped")
span.SetAttributes(attribute.String("mongo.collection_name", collectionName))
defer span.End()
log := logger.Get(ctx)
c := mongo.Session(log).Clone().DB("").C(collectionName)
defer c.Database.Session.Close()
Expand All @@ -226,6 +272,9 @@ func WhereIterUnscoped(ctx context.Context, collectionName string, query bson.M,
}

func Update(ctx context.Context, collectionName string, update bson.M, doc document) error {
ctx, span := tracer.Start(ctx, "update")
span.SetAttributes(attribute.String("mongo.collection_name", collectionName))
defer span.End()
log := logger.Get(ctx)
c := mongo.Session(log).Clone().DB("").C(collectionName)
defer c.Database.Session.Close()
Expand Down
4 changes: 4 additions & 0 deletions mongo/document/paranoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

"go.opentelemetry.io/otel/attribute"
"gopkg.in/mgo.v2/bson"
)

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

func Restore(ctx context.Context, collectionName string, doc document) error {
ctx, span := tracer.Start(ctx, "restore")
span.SetAttributes(attribute.String("mongo.collection_name", collectionName))
defer span.End()
return Update(ctx, collectionName, bson.M{"$unset": bson.M{"deleted_at": ""}}, doc)
}
4 changes: 4 additions & 0 deletions mongo/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ require (
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.1
go.opentelemetry.io/otel v1.11.2
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/trace v1.11.2 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
gopkg.in/errgo.v1 v1.0.1 // indirect
Expand Down
9 changes: 9 additions & 0 deletions mongo/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.2.2 h1:xfmOhhoH5fGPgbEAlhLpJH9p0z/0Qizio9osmvn9IUY=
github.com/frankban/quicktest v1.2.2/go.mod h1:Qh/WofXFeiAFII1aEBu529AtJo6Zg2VHscnEsbBnJ20=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/google/go-cmp v0.2.1-0.20190312032427-6f77996f0c42 h1:q3pnF5JFBNRz8sRD+IRj7Y6DMyYGTNqnZ9axTbSfoNI=
github.com/google/go-cmp v0.2.1-0.20190312032427-6f77996f0c42/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand Down Expand Up @@ -37,6 +42,10 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.opentelemetry.io/otel v1.11.2 h1:YBZcQlsVekzFsFbjygXMOXSs6pialIZxcjfO/mBDmR0=
go.opentelemetry.io/otel v1.11.2/go.mod h1:7p4EUV+AqgdlNV9gL97IgUZiVR3yrFXYo53f9BM3tRI=
go.opentelemetry.io/otel/trace v1.11.2 h1:Xf7hWSF2Glv0DE3MH7fBHvtpSBsjcBUe5MYAmZM/+y0=
go.opentelemetry.io/otel/trace v1.11.2/go.mod h1:4N+yC7QEz7TTsG9BSRLNAa63eg5E06ObSbKPmxQ/pKA=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down

0 comments on commit a07cdfd

Please sign in to comment.