Documentation
¶
Overview ¶
Example ¶
The following example demonstrates how to instrument a MongoDB client with instana using github.com/instana/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/instana/go-sensor"
"github.com/instana/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/instana/go-sensor/instrumentation/instamongo wrapper module.
func main() {
// Initialize Instana collector
c := instana.InitCollector(&instana.Options{
Service: "mongo-client",
})
defer instana.ShutdownCollector()
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, c, 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.TracerLogger, ...) (*mongo.Client, error)
- func NewClient(sensor instana.TracerLogger, opts ...*options.ClientOptions) (*mongo.Client, error)
- func NewCommandMonitor(sensor instana.TracerLogger) *event.CommandMonitor
- func WrapCommandMonitor(mon *event.CommandMonitor, sensor instana.TracerLogger) *event.CommandMonitor
Examples ¶
Constants ¶
const Version = "1.52.0"
Version is the instrumentation module semantic version
Variables ¶
This section is empty.
Functions ¶
func Connect ¶
func Connect(ctx context.Context, sensor instana.TracerLogger, 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/instana/go-sensor"
"github.com/instana/go-sensor/instrumentation/instamongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
// Initialize Instana collector
c := instana.InitCollector(&instana.Options{
Service: "mongo-client",
})
defer instana.ShutdownCollector()
// Replace mongo.Connect() with instamongo.Connect and pass the sensor instance
client, err := instamongo.Connect(context.Background(), c, 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 ¶
func NewClient(sensor instana.TracerLogger, opts ...*options.ClientOptions) (*mongo.Client, error)
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/instana/go-sensor"
"github.com/instana/go-sensor/instrumentation/instamongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
// Initialize Instana collector
c := instana.InitCollector(&instana.Options{
Service: "mongo-client",
})
defer instana.ShutdownCollector()
// Replace mongo.Connect() with instamongo.Connect and pass the sensor instance
client, err := instamongo.NewClient(c, 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.TracerLogger) *event.CommandMonitor
NewCommandMonitor creates a new event.CommandMonitor that instruments a mongo.Client with Instana.
func WrapCommandMonitor ¶
func WrapCommandMonitor(mon *event.CommandMonitor, sensor instana.TracerLogger) *event.CommandMonitor
WrapCommandMonitor wraps an existing event.CommandMonitor to instrument a mongo.Client with Instana
Types ¶
This section is empty.