Connection Pool Monitoring连接池监视
On this page本页内容
Overview概述Event Subscription Example事件订阅示例Event Descriptions事件描述Example Event Documents事件文档示例connectionPoolCreated
connectionPoolReady
connectionPoolClosed
connectionCreated
connectionReady
connectionClosed
connectionCheckOutStarted
connectionCheckOutFailed
connectionCheckedOut
connectionCheckedIn
connectionPoolCleared
Overview概述
This guide shows you how to monitor the driver's connection pool. 本指南向您展示如何监控驱动程序的连接池。A connection pool is a set of open TCP connections your driver maintains with a MongoDB instance. 连接池是驱动程序通过MongoDB实例维护的一组开放TCP连接。Connection pools help reduce the number of network handshakes your application needs to perform and can help your application run faster.连接池有助于减少应用程序需要执行的网络握手次数,并有助于应用程序更快地运行。
Read this guide if you need to record connection pool events in your application or want to explore the information provided in these events.如果您需要在应用程序中记录连接池事件,或者想了解这些事件中提供的信息,请阅读本指南。
Event Subscription Example事件订阅示例
You can access one or more connection pool events using the driver by subscribing to them in your application. 您可以通过在应用程序中订阅驱动程序来访问一个或多个连接池事件。The following example demonstrates connecting to a replica set and subscribing to one of the connection pool monitoring events created by the MongoDB deployment:以下示例演示了连接到副本集并订阅MongoDB部署创建的连接池监控事件之一:
const { MongoClient } = require("mongodb");
//Replace the following with your MongoDB deployment's connection string.将以下内容替换为MongoDB部署的连接字符串。
const uri =
"mongodb+srv://<clusterUrl>/?replicaSet=rs&writeConcern=majority";
const client = new MongoClient(uri);
//Replace <event name> with the name of the event you are subscribing to.将<event name>替换为您正在订阅的事件的名称。
const eventName = "<event name>";
client.on(eventName, (event) =>
console.log("\nreceived event:\n", event)
);
async function run() {
try {
//Establish and verify connection建立并验证连接
await client.db("admin").command({ ping: 1 });
console.log("\nConnected successfully!\n");
} finally {
//Ensures that the client will close when you finish/error确保客户端在您完成/出错时关闭
await client.close();
}
}
run().catch(console.dir);
Event Descriptions事件描述
You can subscribe to any of the following connection pool monitoring events:您可以订阅以下任何连接池监视事件:
connectionPoolCreated | |
connectionPoolReady | |
connectionPoolClosed | |
connectionCreated | |
connectionReady | |
connectionClosed | |
connectionCheckOutStarted | |
connectionCheckOutFailed | |
connectionCheckedOut | |
connectionCheckedIn | |
connectionPoolCleared |
Example Event Documents事件文档示例
The following sections show sample output for each type of connection pool monitoring event.以下部分显示了每种类型的连接池监视事件的示例输出。
connectionPoolCreated
ConnectionPoolCreatedEvent {
time: 2023-02-13T15:54:06.944Z,
address: '...',
options: {...}
}
connectionPoolReady
ConnectionPoolReadyEvent {
time: 2023-02-13T15:56:38.440Z,
address: '...'
}
connectionPoolClosed
ConnectionPoolClosedEvent {
time: 2023-02-13T15:56:38.440Z,
address: '...'
}
connectionCreated
ConnectionCreatedEvent {
time: 2023-02-13T15:56:38.291Z,
address: '...',
connectionId: 1
}
connectionReady
ConnectionReadyEvent {
time: 2023-02-13T15:56:38.291Z,
address: '...',
connectionId: 1
}
connectionClosed
ConnectionClosedEvent {
time: 2023-02-13T15:56:38.439Z,
address: '...',
connectionId: 1,
reason: 'poolClosed',
serviceId: undefined
}
connectionCheckOutStarted
ConnectionCheckOutStartedEvent {
time: 2023-02-13T15:56:38.291Z,
address: '...',
}
connectionCheckOutFailed
ConnectionCheckOutFailedEvent {
time: 2023-02-13T15:56:38.291Z,
address: '...',
reason: ...
}
connectionCheckedOut
ConnectionCheckedOutEvent {
time: 2023-02-13T15:54:07.188Z,
address: '...',
connectionId: 1
}
connectionCheckedIn
ConnectionCheckedInEvent {
time: 2023-02-13T15:54:07.189Z,
address: '...',
connectionId: 1
}
connectionPoolCleared
ConnectionPoolClearedEvent {
time: 2023-02-13T15:56:38.439Z,
address: '...',
serviceId: undefined,
interruptInUseConnections: true,
}