forked from ChrisLGardner/go-discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
94 lines (72 loc) · 2.45 KB
/
storage.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
package main
import (
"context"
"github.com/honeycombio/beeline-go"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Connect to the specified mongo instance using the context for timeout
func connectDb(ctx context.Context, uri string) (*mongo.Client, error) {
ctx, span := beeline.StartSpan(ctx, "mongo.connect")
defer span.Send()
span.AddField("mongo.server", uri)
clientOptions := options.Client().ApplyURI(uri).SetDirect(true)
c, err := mongo.NewClient(clientOptions)
if err != nil {
span.AddField("mongo.client.error", err)
return nil, err
}
err = c.Connect(ctx)
if err != nil {
span.AddField("mongo.connect.error", err)
return nil, err
}
err = c.Ping(ctx, nil)
if err != nil {
span.AddField("mongo.ping.error", err)
return nil, err
}
return c, nil
}
func runQuery(ctx context.Context, mc *mongo.Client, query interface{}) ([]bson.M, error) {
ctx, span := beeline.StartSpan(ctx, "mongo.runQuery")
defer span.Send()
collection := mc.Database("reminders").Collection("reminders")
span.AddField("mongo.runQuery.collection", collection.Name())
span.AddField("mongo.runQuery.database", collection.Database().Name())
span.AddField("mongo.runQuery.query", query)
cursor, err := collection.Find(ctx, query)
if err != nil {
span.AddField("mongo.runQuery.error", err)
return nil, err
}
var results []bson.M
if err = cursor.All(ctx, &results); err != nil {
span.AddField("mongo.runQuery.error", err)
return nil, err
}
span.AddField("mongo.runQuery.results.Count", len(results))
span.AddField("mongo.runQuery.results.raw", results)
return results, nil
}
func writeDbObject(ctx context.Context, mc *mongo.Client, obj interface{}) error {
ctx, span := beeline.StartSpan(ctx, "mongo.writeObject")
defer span.Send()
data, err := bson.Marshal(obj)
if err != nil {
span.AddField("mongo.writeObject.error", err)
return err
}
collection := mc.Database("reminders").Collection("reminders")
span.AddField("mongo.writeObject.collection", collection.Name())
span.AddField("mongo.writeObject.database", collection.Database().Name())
span.AddField("mongo.writeObject.object", data)
res, err := collection.InsertOne(ctx, data)
if err != nil {
span.AddField("mongo.writeObject.error", err)
return err
}
span.AddField("mongo.writeObject.id", res.InsertedID)
return nil
}