Cluster Monitoring
On this page
- Overview
- Event Descriptions
- Event Subscription 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 an SDAM event
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 subscribe to one or more of 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 Subscription Example
You can access one or more SDAM events by
subscribing to them in your application. The following example connects
to a MongoDB deployment and subscribes to the ServerOpeningEvent
event type:
struct ServerOpenHandler; impl SdamEventHandler for ServerOpenHandler { fn handle_server_opening_event(&self, event: ServerOpeningEvent) { eprintln!("Server opening: {:?}", event); } } let handler: Arc<dyn SdamEventHandler> = Arc::new(ServerOpenHandler); client_options.sdam_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 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: