Docs Menu
Docs Home
/ / /
Rust Driver
/ /

Connection Pool Monitoring

On this page

  • Overview
  • Event Descriptions
  • Event Subscription Example
  • Example Event Documents
  • PoolCreatedEvent
  • PoolReadyEvent
  • PoolClearedEvent
  • PoolClosedEvent
  • ConnectionCreatedEvent
  • ConnectionReadyEvent
  • ConnectionClosedEvent
  • ConnectionCheckOutStartedEvent
  • ConnectionCheckoutFailedEvent
  • ConnectionCheckedOutEvent
  • ConnectionCheckedInEvent
  • Additional Information
  • API Documentation

This guide shows you how to use the Rust driver to monitor the driver's connection pool. A connection pool is a set of open Transmission Control Protocol (TCP) connections that your driver maintains with a MongoDB instance. Connection pools help reduce the number of new connections your application needs to create, which might make your application run faster.

You can use information about changes to your connection pool in your application, or you can monitor the connection pool to learn more about how the driver uses connections.

This guide includes the following sections:

  • Event Descriptions describes the connection pool events that the driver can generate

  • Event Subscription Example provides sample code that shows how to subscribe to a connection pool event

  • Example Event Documents provides samples of each connection pool event

  • Additional Information provides links to resources and API documentation for types and methods mentioned in this guide

You can subscribe to one or more of the following connection pool monitoring events:

Event Name
Created when a connection pool is created.
Created when a connection pool is ready.
Created when a connection pool is cleared.
Created when a connection pool is closed, before the destruction of the server instance.
Created when a connection is created, but not necessarily when it is used for an operation.
Created after a connection successfully completes a handshake and is ready to be used for operations.
Created when a connection is closed.
Created when an operation attempts to acquire a connection for execution.
Created when an operation cannot acquire a connection for execution.
Created when an operation successfully acquires a connection for execution.
Created when a connection is checked back into the pool after an operation is executed.

You can access one or more connection pool events by subscribing to them in your application. The following example connects to a MongoDB deployment and subscribes to the ConnectionCreatedEvent event type:

struct ConnectionCreatedHandler;
impl CmapEventHandler for ConnectionCreatedHandler {
fn handle_connection_created_event(&self, event: ConnectionCreatedEvent) {
eprintln!("Connection created: {:?}", event);
let handler: Arc<dyn CmapEventHandler> = Arc::new(ConnectionCreatedHandler);
client_options.cmap_event_handler = Some(handler);
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 connection pool monitoring event.

PoolCreatedEvent {
address: ...,
options: {...}
PoolReadyEvent {
address: ...
PoolClearedEvent {
address: ...,
service_id: ...,
PoolClosedEvent {
address: ...
ConnectionCreatedEvent {
address: ...,
connection_id: 1
ConnectionReadyEvent {
address: ...,
connection_id: 1
ConnectionClosedEvent {
address: ...,
connection_id: 1,
reason: ...,
/* private fields */
ConnectionCheckOutStartedEvent {
address: ...,
ConnectionCheckOutFailedEvent {
address: ...,
reason: ...,
/* private fields */
ConnectionCheckedOutEvent {
address: ...,
connection_id: 1
ConnectionCheckedInEvent {
address: ...,
connection_id: 1

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 improving the performance of your application, see the guide on Performance Considerations.

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

← Command Monitoring