github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrmongo/example/main.go (about) 1 // Copyright 2020 New Relic Corporation. All rights reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 4 package main 5 6 import ( 7 "context" 8 "os" 9 "time" 10 11 newrelic "github.com/newrelic/go-agent" 12 "github.com/newrelic/go-agent/_integrations/nrmongo" 13 "go.mongodb.org/mongo-driver/bson" 14 "go.mongodb.org/mongo-driver/mongo" 15 "go.mongodb.org/mongo-driver/mongo/options" 16 ) 17 18 func main() { 19 config := newrelic.NewConfig("Basic Mongo Example", os.Getenv("NEW_RELIC_LICENSE_KEY")) 20 config.Logger = newrelic.NewDebugLogger(os.Stdout) 21 app, err := newrelic.NewApplication(config) 22 if nil != err { 23 panic(err) 24 } 25 app.WaitForConnection(10 * time.Second) 26 27 // If you have another CommandMonitor, you can pass it to NewCommandMonitor and it will get called along 28 // with the NR monitor 29 nrMon := nrmongo.NewCommandMonitor(nil) 30 ctx := context.Background() 31 32 // nrMon must be added after any other monitors are added, as previous options get overwritten. 33 // This example assumes Mongo is running locally on port 27017 34 client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017").SetMonitor(nrMon)) 35 if err != nil { 36 panic(err) 37 } 38 defer client.Disconnect(ctx) 39 40 txn := app.StartTransaction("Mongo txn", nil, nil) 41 // Make sure to add the newrelic.Transaction to the context 42 nrCtx := newrelic.NewContext(context.Background(), txn) 43 collection := client.Database("testing").Collection("numbers") 44 _, err = collection.InsertOne(nrCtx, bson.M{"name": "exampleName", "value": "exampleValue"}) 45 if err != nil { 46 panic(err) 47 } 48 txn.End() 49 app.Shutdown(10 * time.Second) 50 51 }