nrmongo

package
v3.14.1+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 20, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

README

_integrations/nrmongo GoDoc

Package nrmongo instruments https://github.com/mongodb/mongo-go-driver

import "github.com/newrelic/go-agent/_integrations/nrmongo"

For more information, see godocs.

Documentation

Overview

Package nrmongo instruments https://github.com/mongodb/mongo-go-driver

Use this package to instrument your MongoDB calls without having to manually create DatastoreSegments. To do so, first set the monitor in the connect options using `SetMonitor` (https://godoc.org/go.mongodb.org/mongo-driver/mongo/options#ClientOptions.SetMonitor):

nrMon := nrmongo.NewCommandMonitor(nil)
client, err := mongo.Connect(ctx, options.Client().SetMonitor(nrMon))

Note that it is important that this `nrmongo` monitor is the last monitor set, otherwise it will be overwritten. If needing to use more than one `event.CommandMonitor`, pass the original monitor to the `nrmongo.NewCommandMonitor` function:

origMon := &event.CommandMonitor{
	Started:   origStarted,
	Succeeded: origSucceeded,
	Failed:    origFailed,
}
nrMon := nrmongo.NewCommandMonitor(origMon)
client, err := mongo.Connect(ctx, options.Client().SetMonitor(nrMon))

Then add the current transaction to the context used in any MongoDB call:

ctx = newrelic.NewContext(context.Background(), txn)
resp, err := collection.InsertOne(ctx, bson.M{"name": "pi", "value": 3.14159})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCommandMonitor

func NewCommandMonitor(original *event.CommandMonitor) *event.CommandMonitor

NewCommandMonitor returns a new `*event.CommandMonitor` (https://godoc.org/go.mongodb.org/mongo-driver/event#CommandMonitor). If provided, the original `*event.CommandMonitor` will be called as well. The returned `*event.CommandMonitor` creates `newrelic.DatastoreSegment`s (https://godoc.org/github.com/newrelic/go-agent#DatastoreSegment) for each database call.

// Use `SetMonitor` to register the CommandMonitor.
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017").SetMonitor(nrmongo.NewCommandMonitor(nil)))
if err != nil {
	log.Fatal(err)
}

// Add transaction to the context.  This step is required.
ctx = newrelic.NewContext(ctx, txn)

collection := client.Database("testing").Collection("numbers")
resp, err := collection.InsertOne(ctx, bson.M{"name": "pi", "value": 3.14159})
if err != nil {
	log.Fatal(err)
}

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL