-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_example.go
54 lines (46 loc) · 1.32 KB
/
mongo_example.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
package main
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"io"
"log"
"net/http"
"os"
"github.com/pinpoint-apm/pinpoint-go-agent"
"github.com/pinpoint-apm/pinpoint-go-agent/plugin/http"
"github.com/pinpoint-apm/pinpoint-go-agent/plugin/mongodriver"
)
func mongodb(w http.ResponseWriter, r *http.Request) {
opts := options.Client()
opts.ApplyURI("mongodb://localhost:27017")
opts.Monitor = ppmongo.NewMonitor()
ctx := context.Background()
client, err := mongo.Connect(ctx, opts)
if err != nil {
panic(err)
}
defer client.Disconnect(ctx)
collection := client.Database("testdb").Collection("example")
_, err = collection.InsertOne(r.Context(), bson.M{"foo": "bar", "apm": "pinpoint"})
if err != nil {
panic(err)
}
io.WriteString(w, "insert success")
}
func main() {
opts := []pinpoint.ConfigOption{
pinpoint.WithAppName("GoMongoTest"),
pinpoint.WithAgentId("GoMongoTestAgent"),
pinpoint.WithConfigFile(os.Getenv("HOME") + "/tmp/pinpoint-config.yaml"),
}
cfg, _ := pinpoint.NewConfig(opts...)
agent, err := pinpoint.NewAgent(cfg)
if err != nil {
log.Fatalf("pinpoint agent start fail: %v", err)
}
defer agent.Shutdown()
http.HandleFunc("/mongo", pphttp.WrapHandlerFunc(mongodb))
http.ListenAndServe(":9000", nil)
}