Skip to content

Commit df3d005

Browse files
committed
DOCSP-38042: CMAP Tracking Example (#891)
(cherry picked from commit 1859354)
1 parent c8b3627 commit df3d005

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function connectionPoolStatus(client) {
2+
let checkedOut = 0;
3+
4+
function onCheckout() {
5+
checkedOut++;
6+
}
7+
8+
function onCheckin() {
9+
checkedOut--;
10+
}
11+
12+
function onClose() {
13+
client.removeListener('connectionCheckedOut', onCheckout);
14+
client.removeListener('connectionCheckedIn', onCheckin);
15+
16+
checkedOut = NaN;
17+
}
18+
19+
// Decreases count of connections checked out of the pool when connectionCheckedIn event is triggered
20+
client.on('connectionCheckedIn', onCheckin);
21+
22+
// Increases count of connections checked out of the pool when connectionCheckedOut event is triggered
23+
client.on('connectionCheckedOut', onCheckout);
24+
25+
// Cleans up event listeners when client is closed
26+
client.on('close', onClose);
27+
28+
return {
29+
count: () => checkedOut,
30+
cleanUp: onClose
31+
};
32+
}

source/fundamentals/monitoring/connection-monitoring.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ network handshakes your application needs to perform and can help your
2727
application run faster.
2828

2929
The following sections demonstrate how to record connection pool events in your
30-
application or want to explore the information provided in these events.
30+
application and explore the information provided in these events.
3131

32-
Event Subscription Example
33-
--------------------------
32+
Event Subscription Examples
33+
---------------------------
3434

3535
You can access one or more connection pool events using the driver by
3636
subscribing to them in your application. The following example demonstrates
@@ -40,6 +40,13 @@ pool monitoring events created by the MongoDB deployment:
4040
.. literalinclude:: /code-snippets/monitoring/cpm-subscribe.js
4141
:language: javascript
4242

43+
Connection pool monitoring events can aid you in debugging and understanding
44+
the behavior of your application's connection pool. The following example uses connection
45+
pool monitoring events to return a count of checked-out connections in the pool:
46+
47+
.. literalinclude:: /code-snippets/monitoring/cpm-status.js
48+
:language: javascript
49+
4350
Event Descriptions
4451
------------------
4552

0 commit comments

Comments
 (0)