added monitor only for command 'find'

This commit is contained in:
Pavel 2024-08-04 10:09:05 +03:00
parent 502bc1d1c4
commit 78c38993d3

@ -3,6 +3,7 @@ package mongo
import ( import (
"context" "context"
"fmt" "fmt"
"go.mongodb.org/mongo-driver/event"
"log" "log"
"net" "net"
"net/url" "net/url"
@ -27,17 +28,23 @@ func Connect(ctx context.Context, deps *ConnectDeps) (*mongo.Database, error) {
Host: net.JoinHostPort(deps.Configuration.Host, deps.Configuration.Port), Host: net.JoinHostPort(deps.Configuration.Host, deps.Configuration.Port),
} }
//cmdMonitor := &event.CommandMonitor{ cmdMonitor := &event.CommandMonitor{
// Started: func(_ context.Context, evt *event.CommandStartedEvent) { Started: func(_ context.Context, evt *event.CommandStartedEvent) {
// log.Println(evt.Command) if evt.CommandName == "find" {
// }, log.Println(evt.Command)
// Succeeded: func(_ context.Context, evt *event.CommandSucceededEvent) { }
// log.Println(evt.Reply) },
// }, Succeeded: func(_ context.Context, evt *event.CommandSucceededEvent) {
// Failed: func(_ context.Context, evt *event.CommandFailedEvent) { if evt.CommandName == "find" {
// log.Println(evt.Failure) log.Println(evt.Reply)
// }, }
//} },
Failed: func(_ context.Context, evt *event.CommandFailedEvent) {
if evt.CommandName == "find" {
log.Println(evt.Failure)
}
},
}
connectionOptions := options.Client(). connectionOptions := options.Client().
ApplyURI(mongoURI.String()). ApplyURI(mongoURI.String()).
@ -46,7 +53,7 @@ func Connect(ctx context.Context, deps *ConnectDeps) (*mongo.Database, error) {
AuthSource: deps.Configuration.Auth, AuthSource: deps.Configuration.Auth,
Username: deps.Configuration.User, Username: deps.Configuration.User,
Password: deps.Configuration.Password, Password: deps.Configuration.Password,
}) }).SetMonitor(cmdMonitor)
ticker := time.NewTicker(1 * time.Second) ticker := time.NewTicker(1 * time.Second)
timeoutExceeded := time.After(deps.Timeout) timeoutExceeded := time.After(deps.Timeout)