From 78c38993d3d647be77d8b0d2b53ca1f9f0e533a0 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 4 Aug 2024 10:09:05 +0300 Subject: [PATCH] added monitor only for command 'find' --- mongo/connection.go | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/mongo/connection.go b/mongo/connection.go index 1bcf40a..49aad4e 100644 --- a/mongo/connection.go +++ b/mongo/connection.go @@ -3,6 +3,7 @@ package mongo import ( "context" "fmt" + "go.mongodb.org/mongo-driver/event" "log" "net" "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), } - //cmdMonitor := &event.CommandMonitor{ - // Started: func(_ context.Context, evt *event.CommandStartedEvent) { - // log.Println(evt.Command) - // }, - // Succeeded: func(_ context.Context, evt *event.CommandSucceededEvent) { - // log.Println(evt.Reply) - // }, - // Failed: func(_ context.Context, evt *event.CommandFailedEvent) { - // log.Println(evt.Failure) - // }, - //} + cmdMonitor := &event.CommandMonitor{ + Started: func(_ context.Context, evt *event.CommandStartedEvent) { + if evt.CommandName == "find" { + log.Println(evt.Command) + } + }, + Succeeded: func(_ context.Context, evt *event.CommandSucceededEvent) { + if evt.CommandName == "find" { + log.Println(evt.Reply) + } + }, + Failed: func(_ context.Context, evt *event.CommandFailedEvent) { + if evt.CommandName == "find" { + log.Println(evt.Failure) + } + }, + } connectionOptions := options.Client(). ApplyURI(mongoURI.String()). @@ -46,7 +53,7 @@ func Connect(ctx context.Context, deps *ConnectDeps) (*mongo.Database, error) { AuthSource: deps.Configuration.Auth, Username: deps.Configuration.User, Password: deps.Configuration.Password, - }) + }).SetMonitor(cmdMonitor) ticker := time.NewTicker(1 * time.Second) timeoutExceeded := time.After(deps.Timeout)