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
Overview
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
Event Descriptions
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. |
Event Subscription Example
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
Example Event Documents
The following sections show sample output for each type of connection pool monitoring event.
PoolCreatedEvent
PoolCreatedEvent { address: ..., options: {...} }
PoolReadyEvent
PoolReadyEvent { address: ... }
PoolClearedEvent
PoolClearedEvent { address: ..., service_id: ..., }
PoolClosedEvent
PoolClosedEvent { address: ... }
ConnectionCreatedEvent
ConnectionCreatedEvent { address: ..., connection_id: 1 }
ConnectionReadyEvent
ConnectionReadyEvent { address: ..., connection_id: 1 }
ConnectionClosedEvent
ConnectionClosedEvent { address: ..., connection_id: 1, reason: ..., /* private fields */ }
ConnectionCheckOutStartedEvent
ConnectionCheckOutStartedEvent { address: ..., }
ConnectionCheckoutFailedEvent
ConnectionCheckOutFailedEvent { address: ..., reason: ..., /* private fields */ }
ConnectionCheckedOutEvent
ConnectionCheckedOutEvent { address: ..., connection_id: 1 }
ConnectionCheckedInEvent
ConnectionCheckedInEvent { address: ..., connection_id: 1 }
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.
To learn more about improving the performance of your application, see the guide on Performance Considerations.
API Documentation
To learn more about the methods and types mentioned in this guide, see the following API documentation: