Documentation ¶
Overview ¶
Example ¶
The following example demonstrates how to instrument a MongoDB client with instana using github.com/mier85/go-sensor/instrumentation/instamongo wrapper module.
// (c) Copyright IBM Corp. 2021 // (c) Copyright Instana Inc. 2021 package main import ( "context" "fmt" "log" "time" instana "github.com/mier85/go-sensor" "github.com/mier85/go-sensor/instrumentation/instamongo" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo/options" ) const localhostMongo = "mongodb://localhost:27017" // The following example demonstrates how to instrument a MongoDB client with instana using // github.com/mier85/go-sensor/instrumentation/instamongo wrapper module. func main() { // Initialize Instana sensor sensor := instana.NewSensor("mongo-client") ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() // Use instamongo.Connect() to establish connection to MongoDB and instrument the client client, err := instamongo.Connect(ctx, sensor, options.Client().ApplyURI(localhostMongo)) if err != nil { log.Fatalf("failed to connect to %s: %s", localhostMongo, err) } // Use instrumented client as usual dbs, err := client.ListDatabases(context.Background(), bson.D{}) if err != nil { log.Fatalf("failed to list databases: %s", err) } fmt.Println("found", len(dbs.Databases), "database(s)") for _, db := range dbs.Databases { fmt.Println("* ", db.Name) } }
Output:
Index ¶
- Constants
- func Connect(ctx context.Context, sensor *instana.Sensor, opts ...*options.ClientOptions) (*mongo.Client, error)
- func NewClient(sensor *instana.Sensor, opts ...*options.ClientOptions) (*mongo.Client, error)
- func NewCommandMonitor(sensor *instana.Sensor) *event.CommandMonitor
- func WrapCommandMonitor(mon *event.CommandMonitor, sensor *instana.Sensor) *event.CommandMonitor
Examples ¶
Constants ¶
const Version = "v1.0.0"
Version is the instrumentation module semantic version
Variables ¶
This section is empty.
Functions ¶
func Connect ¶
func Connect(ctx context.Context, sensor *instana.Sensor, opts ...*options.ClientOptions) (*mongo.Client, error)
Connect creates and instruments a new mongo.Client
This is a wrapper method for mongo.Connect(), see https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#Connect for details on the original method.
Example ¶
To instrument a mongo.Client created with mongo.Connect() replace mongo.Connect() with instamongo.Connect() and pass an instana.Sensor instance to use
package main import ( "context" "log" instana "github.com/mier85/go-sensor" "github.com/mier85/go-sensor/instrumentation/instamongo" "go.mongodb.org/mongo-driver/mongo/options" ) func main() { // Initialize Instana sensor sensor := instana.NewSensor("mongo-client") // Replace mongo.Connect() with instamongo.Connect and pass the sensor instance client, err := instamongo.Connect(context.Background(), sensor, options.Client().ApplyURI("mongodb://localhost:27017")) if err != nil { log.Fatal(err) } defer client.Disconnect(context.Background()) // Query MongoDB as usual using the instrumented client instance // ... }
Output:
func NewClient ¶
NewClient returns a new instrumented mongo.Client instance
This is a wrapper method for mongo.NewClient(), see https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#NewClient for details on the original method.
Example ¶
To instrument a mongo.Client created with mongo.NewClient() replace mongo.NewClient() with instamongo.NewClient() and pass an instana.Sensor instance to use
package main import ( "context" "log" instana "github.com/mier85/go-sensor" "github.com/mier85/go-sensor/instrumentation/instamongo" "go.mongodb.org/mongo-driver/mongo/options" ) func main() { // Initialize Instana sensor sensor := instana.NewSensor("mongo-client") // Replace mongo.Connect() with instamongo.Connect and pass the sensor instance client, err := instamongo.NewClient(sensor, options.Client().ApplyURI("mongodb://localhost:27017")) if err != nil { log.Fatal(err) } // Use instrumented client as usual client.Connect(context.Background()) }
Output:
func NewCommandMonitor ¶
func NewCommandMonitor(sensor *instana.Sensor) *event.CommandMonitor
NewCommandMonitor creates a new event.CommandMonitor that instruments a mongo.Client with Instana.
func WrapCommandMonitor ¶
func WrapCommandMonitor(mon *event.CommandMonitor, sensor *instana.Sensor) *event.CommandMonitor
WrapCommandMonitor wraps an existing event.CommandMonitor to instrument a mongo.Client with Instana
Types ¶
This section is empty.