For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Connection Pool Monitoring

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:

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

Event Name
Description

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: