Command Monitoring
On this page
Overview
This guide shows you how to use the Rust driver to monitor the outcome of commands that the driver sends to your MongoDB deployment.
You can use information about command events in your application, or you can monitor commands to learn more about how the driver executes them.
This guide includes the following sections:
Event Descriptions describes the command events that the driver can generate
Event Subscription Example provides sample code that shows how to subscribe to a command event
Example Event Documents provides samples of each command event
Additional Information provides links to resources and API documentation for types and methods mentioned in this guide
Event Descriptions
You can subscribe to one or more of the following command monitoring events:
Event Name | Description |
---|---|
Created when a command starts. | |
Created when a command succeeds. | |
Created when a command does not succeed. |
Event Subscription Example
You can access one or more command events by
subscribing to them in your application. The following example connects
to a MongoDB deployment and subscribes to the CommandStartedEvent
event type:
struct CommandStartHandler; impl CommandEventHandler for CommandStartHandler { fn handle_command_started_event(&self, event: CommandStartedEvent) { eprintln!("Command started: {:?}", event); } } let handler: Arc<dyn CommandEventHandler> = Arc::new(CommandStartHandler); client_options.command_event_handler = Some(handler); let client = Client::with_options(client_options)?; // ... perform actions with the client to generate events
Example Event Documents
The following sections show sample output for each type of command monitoring event.
CommandStartedEvent
CommandStartedEvent { request_id: 12, db: "testdb", command_name: "find", connection: ..., command: ..., service_id: ... }
CommandSucceededEvent
CommandSucceededEvent { duration: ..., reply: ..., command_name: "find", request_id: 12, connection: ..., service_id: ..., }
CommandFailedEvent
CommandFailedEvent { duration: ..., command_name: "find", failure: ..., request_id: 12, connection: ..., service_id: ..., }
Additional Information
To learn more about monitoring a MongoDB deployment, see the How to Monitor MongoDB article.
To learn more about performing MongoDB operations, see the CRUD Operations guides.
API Documentation
To learn more about the methods and types mentioned in this guide, see the following API documentation: