Инструментирование OTEL MongoDB
Приведенный ниже пример не избавит от необходимости выполнять Инициализация OTEL в Golang но все дальнейшие подключения к монге будут покрываться спанами автоматически!
package main
import (
"context"
"log"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo"
"go.opentelemetry.io/otel"
)
var tracer = otel.Tracer("mongo_example")
const URI = "mongodb://127.0.0.1:27017"
func getClient(ctx context.Context) (*mongo.Client, error) {
ctx, span := FollowSpan(ctx, "getClient")
defer span.End()
opts := options.Client()
opts.ApplyURI(URI)
optsAuth := options.Credential{
Username: "root",
Password: "example",
AuthSource: "admin",
}
opts.SetAuth(optsAuth)
opts.Monitor = otelmongo.NewMonitor()
client, err := mongo.Connect(ctx, opts)
if err != nil {
return nil, err
}
return client, nil