Docs Menu
Docs Home
/ / /
Rust Driver
/ /

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

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.

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 mongos proxy disconnects.
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 hello command to an instance.
Created when the hello command returns successfully from a MongoDB instance.
Created when a hello command to a MongoDB instance does not return a successful response.

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

The following sections show sample output for each type of SDAM event.

ServerDescriptionChangedEvent {
address: ...,
topology_id: ...,
previous_description: ...,
new_description: ...,
}
ServerOpeningEvent {
address: ...,
topology_id: ...,
}
ServerClosedEvent {
address: ...,
topology_id: ...,
}
TopologyDescriptionChangedEvent {
topology_id: ...,
previous_description: ...,
new_description: ...,
}
TopologyOpeningEvent {
topology_id: ...,
}
TopologyClosedEvent {
topology_id: ...,
}
ServerHeartbeatStartedEvent {
server_address: ...,
awaited: false,
driver_connection_id: 12,
server_connection_id: ...,
}
ServerHeartbeatSucceededEvent {
duration: ...,
reply: ...,
server_address: ...,
awaited: false,
driver_connection_id: 12,
server_connection_id: ...,
}
ServerHeartbeatFailedEvent {
duration: ...,
failure: ...,
server_address: ...,
awaited: false,
driver_connection_id: 12,
server_connection_id: ...,
}

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.

To learn more about the methods and types mentioned in this guide, see the following API documentation:

Back

Monitoring