Skip to content

Instantly share code, notes, and snippets.

@Deeptiman
Created August 24, 2021 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Deeptiman/96320d9dae0d8a46a77f34e0f817b656 to your computer and use it in GitHub Desktop.
Save Deeptiman/96320d9dae0d8a46a77f34e0f817b656 to your computer and use it in GitHub Desktop.
command-monitor
cmdMonitor := &event.CommandMonitor{
Started: func(_ context.Context, evt *event.CommandStartedEvent) {
// Command Started event triggered when a mongo query transaction prepares the command lifecycle for
// certain DatabaseName
CommandStartedEvent {
Command bson.Raw // base64 hash value of the mongo query transaction raw
DatabaseName string // mongodb database name
CommandName string // mongodb query name [ping,insert,update,find]
RequestID int64 // mongo query command unique ID
ConnectionID string // connection string for mongo instance
}
},
Succeeded: func(_ context.Context, evt *event.CommandSucceededEvent) {
// Command Succeeded event triggered when a mongo query transaction gets successful
CommandSucceededEvent {
CommandFinishedEvent // CommandFinishedEvent will contain the basic cmd event details
Reply bson.Raw // response base64 hash for a successfully executed cmd
}
},
Failed: func(_ context.Context, evt *event.CommandFailedEvent) {
// Command Failed event triggered when a mongo query transaction failed
CommandFailedEvent {
CommandFinishedEvent // CommandFinishedEvent will contain the basic cmd event details
Failure string // failure error message for the mongo-query
}
},
}
client, err := mongo.NewClient(options.Client().
ApplyURI("mongodb://<username>:<password>@localhost:27017").SetMonitor(cmdMonitor))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment