Cluster Monitoring
On this page
- Overview
- Event Descriptions
- Event Monitoring Example
- Example Event Documents
- ServerDescriptionChangedEvent
- ServerOpeningEvent
- ServerClosedEvent
- TopologyDescriptionChangedEvent
- TopologyOpeningEvent
- TopologyClosedEvent
- ServerHeartbeatStartedEvent
- ServerHeartbeatSucceededEvent
- ServerHeartbeatFailedEvent
- Additional Information
- API Documentation
Overview
This guide shows you how to use the Rust driver to monitor topology events in a MongoDB instance, replica set, or sharded cluster. The driver creates topology events, also known as Server Discovery and Monitoring (SDAM) events, when there are any changes in the state of the instance or cluster that you are connected to.
You can use information about topology changes in your application, or you can monitor cluster changes to learn more about how they affect your application.
This guide includes the following sections:
Event Descriptions describes the SDAM events that the driver can generate.
Event Subscription Example provides sample code that shows how to subscribe to SDAM events.
Example Event Documents provides samples of each SDAM 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 SDAM events:
Event Name | Description |
---|---|
Created when an instance state changes, such as when a replica set member changes from a secondary to a primary. | |
Created when a connection to an instance, such as a replica set member, opens. | |
Created when a connection to an instance, such as a replica set member, closes. | |
Created when the topology description changes, such as when there
is an election of a new primary or when a | |
Created before the driver attempts to connect to an instance. | |
Created after all instance connections in the topology close. | |
Created before the driver issues a | |
Created when the | |
Created when a |
Event Monitoring Example
You can monitor SDAM events by assigning an EventHandler
instance as the value of the
sdam_event_handler
client option. To construct an EventHandler
, which processes all
SDAM events, use the callback()
or async_callback()
method.
The following example connects to a MongoDB deployment, instructs the client to monitor SDAM events, and prints each event:
let mut client_options = ClientOptions::parse("<connection string>").await?; client_options.sdam_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 SDAM event.
ServerDescriptionChangedEvent
ServerDescriptionChangedEvent { address: ..., topology_id: ..., previous_description: ..., new_description: ..., }
ServerOpeningEvent
ServerOpeningEvent { address: ..., topology_id: ..., }
ServerClosedEvent
ServerClosedEvent { address: ..., topology_id: ..., }
TopologyDescriptionChangedEvent
TopologyDescriptionChangedEvent { topology_id: ..., previous_description: ..., new_description: ..., }
TopologyOpeningEvent
TopologyOpeningEvent { topology_id: ..., }
TopologyClosedEvent
TopologyClosedEvent { topology_id: ..., }
ServerHeartbeatStartedEvent
ServerHeartbeatStartedEvent { server_address: ..., awaited: false, driver_connection_id: 12, server_connection_id: ..., }
ServerHeartbeatSucceededEvent
ServerHeartbeatSucceededEvent { duration: ..., reply: ..., server_address: ..., awaited: false, driver_connection_id: 12, server_connection_id: ..., }
ServerHeartbeatFailedEvent
ServerHeartbeatFailedEvent { duration: ..., failure: ..., server_address: ..., awaited: false, driver_connection_id: 12, server_connection_id: ..., }
Additional Information
To learn more about monitoring a MongoDB deployment, see the How to Monitor MongoDB article.
To learn more about connecting to MongoDB, see the Connection Guide.
API Documentation
To learn more about the methods and types mentioned in this guide, see the following API documentation: