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 command events.
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 monitor the following command events:
Event Name | Description |
---|---|
Created when a command starts. | |
Created when a command succeeds. | |
Created when a command does not succeed. |
Event Monitoring Example
You can monitor command events by assigning an EventHandler
instance as the value of the
command_event_handler
client option. To construct an EventHandler
, which processes all
command events, use the callback()
or async_callback()
method.
The following example connects to a MongoDB deployment, instructs the client to monitor command events, and prints each event:
let mut client_options = ClientOptions::parse("<connection string>").await?; client_options.command_event_handler = Some(EventHandler::callback(|ev| println!("{:?}", ev))); 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: