forked from newrelic/go-agent
/
main.go
51 lines (43 loc) · 1.55 KB
/
main.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
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"os"
"time"
newrelic "github.com/newrelic/go-agent"
"github.com/newrelic/go-agent/_integrations/nrmongo"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
config := newrelic.NewConfig("Basic Mongo Example", os.Getenv("NEW_RELIC_LICENSE_KEY"))
config.Logger = newrelic.NewDebugLogger(os.Stdout)
app, err := newrelic.NewApplication(config)
if nil != err {
panic(err)
}
app.WaitForConnection(10 * time.Second)
// If you have another CommandMonitor, you can pass it to NewCommandMonitor and it will get called along
// with the NR monitor
nrMon := nrmongo.NewCommandMonitor(nil)
ctx := context.Background()
// nrMon must be added after any other monitors are added, as previous options get overwritten.
// This example assumes Mongo is running locally on port 27017
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017").SetMonitor(nrMon))
if err != nil {
panic(err)
}
defer client.Disconnect(ctx)
txn := app.StartTransaction("Mongo txn", nil, nil)
// Make sure to add the newrelic.Transaction to the context
nrCtx := newrelic.NewContext(context.Background(), txn)
collection := client.Database("testing").Collection("numbers")
_, err = collection.InsertOne(nrCtx, bson.M{"name": "exampleName", "value": "exampleValue"})
if err != nil {
panic(err)
}
txn.End()
app.Shutdown(10 * time.Second)
}