|
| 1 | ++++ |
| 2 | +date = "2020-01-14T09:03:26-04:00" |
| 3 | +title = "Connection Pool Monitoring" |
| 4 | +[menu.main] |
| 5 | + parent = "Management" |
| 6 | + identifier = "CMAP" |
| 7 | + weight = 100 |
| 8 | + pre = "<i class='fa'></i>" |
| 9 | ++++ |
| 10 | + |
| 11 | +# Connection Pool Monitoring |
| 12 | + |
| 13 | +The Node.js driver `3.5.0` or higher features Connection Pool Monitoring events, allowing an application or |
| 14 | +tool to monitor the internal workings of the driver's connection pool. |
| 15 | + |
| 16 | +**NOTE:** Connection pool monitoring is only available when the "Unified Topology" is enabled |
| 17 | + |
| 18 | +## Overview of CMAP events |
| 19 | + |
| 20 | +| Event | Description | |
| 21 | +| :----------| :------------- | |
| 22 | +| connectionPoolCreated | Emitted when a connection pool is created | |
| 23 | +| connectionPoolClosed | Emitted when a connection pool is closed, prior to server instance destruction | |
| 24 | +| connectionCreated | Emitted when a connection is created, but not necessarily when it is used for an operation | |
| 25 | +| connectionReady | Emitted after a connection has successfully completed a handshake, and is ready to be used for operations| |
| 26 | +| connectionClosed | Emitted when a connection is closed | |
| 27 | +| connectionCheckOutStarted | Emitted when an operation attempts to acquire a connection for execution | |
| 28 | +| connectionCheckOutFailed | Emitted when an operation fails to acquire a connection for execution | |
| 29 | +| connectionCheckedOut | Emitted when an operation successfully acquires a connection for execution | |
| 30 | +| connectionCheckedIn | Emitted when a connection is returned to the pool after operation execution | |
| 31 | +| connectionPoolCleared | Emitted when the connection pool's generation count is increased | |
| 32 | + |
| 33 | +## Simple Code Example |
| 34 | + |
| 35 | +The following example demonstrates connecting to a replica set and printing out all CMAP related events: |
| 36 | + |
| 37 | +```js |
| 38 | +const MongoClient = require('mongodb').MongoClient; |
| 39 | +const url = 'mongodb://localhost:31000,localhost:31001/?replicaSet=rs'; |
| 40 | +const client = new MongoClient(url); |
| 41 | + |
| 42 | +client.on('connectionPoolCreated', event => console.dir(event)); |
| 43 | +client.on('connectionPoolClosed', event => console.dir(event)); |
| 44 | +client.on('connectionCreated', event => console.dir(event)); |
| 45 | +client.on('connectionReady', event => console.dir(event)); |
| 46 | +client.on('connectionClosed', event => console.dir(event)); |
| 47 | +client.on('connectionCheckOutStarted', event => console.dir(event)); |
| 48 | +client.on('connectionCheckOutFailed', event => console.dir(event)); |
| 49 | +client.on('connectionCheckedOut', event => console.dir(event)); |
| 50 | +client.on('connectionCheckedIn', event => console.dir(event)); |
| 51 | +client.on('connectionPoolCleared', event => console.dir(event)); |
| 52 | + |
| 53 | +client.connect((err, client) => { |
| 54 | + if (err) throw err; |
| 55 | +}); |
| 56 | +``` |
| 57 | + |
| 58 | +## Example Events |
| 59 | + |
| 60 | +### connectionPoolCreated |
| 61 | +```js |
| 62 | +ConnectionPoolCreatedEvent { |
| 63 | + time: 2020-01-14T13:46:15.536Z, |
| 64 | + address: 'localhost:31003', |
| 65 | + options: { ... } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### connectionPoolClosed |
| 70 | +```js |
| 71 | +ConnectionPoolClosedEvent { |
| 72 | + time: 2020-01-14T13:54:53.570Z, |
| 73 | + address: '127.0.0.1:34849' |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### connectionCreated |
| 78 | +```js |
| 79 | +ConnectionCreatedEvent { |
| 80 | + time: 2020-01-14T13:54:53.579Z, |
| 81 | + address: '127.0.0.1:34849', |
| 82 | + connectionId: 1 |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### connectionReady |
| 87 | +```js |
| 88 | +ConnectionReadyEvent { |
| 89 | + time: 2020-01-14T13:54:53.579Z, |
| 90 | + address: '127.0.0.1:34849', |
| 91 | + connectionId: 1 |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +### connectionClosed |
| 96 | +```js |
| 97 | +ConnectionClosedEvent { |
| 98 | + time: 2020-01-14T13:54:53.564Z, |
| 99 | + address: '127.0.0.1:34849', |
| 100 | + connectionId: 2, |
| 101 | + reason: ... |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +### connectionCheckOutStarted |
| 106 | +```js |
| 107 | +ConnectionCheckOutStartedEvent { |
| 108 | + time: 2020-01-14T13:49:59.271Z, |
| 109 | + address: 'localhost:31000' |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +### connectionCheckOutFailed |
| 114 | +```js |
| 115 | +ConnectionCheckOutFailedEvent { |
| 116 | + time: 2020-01-14T13:49:59.271Z, |
| 117 | + address: 'localhost:31000' |
| 118 | + reason: ... |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +### connectionCheckedOut |
| 123 | +```js |
| 124 | +ConnectionCheckedOutEvent { |
| 125 | + time: 2020-01-14T13:48:42.541Z, |
| 126 | + address: 'localhost:31000', |
| 127 | + connectionId: 1 |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +### connectionCheckedIn |
| 132 | +```js |
| 133 | +ConnectionCheckedInEvent { |
| 134 | + time: 2020-01-14T13:48:42.543Z, |
| 135 | + address: 'localhost:31000', |
| 136 | + connectionId: 1 |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +### connectionPoolCleared |
| 141 | +```js |
| 142 | +ConnectionPoolClearedEvent { |
| 143 | + time: 2020-01-14T13:58:11.437Z, |
| 144 | + address: '127.0.0.1:45005' |
| 145 | +} |
| 146 | +``` |
0 commit comments