连接监控
在本页
概述
本指南展示了如何使用Go驱动程序来监控驱动程序的连接池。连接池是一组开放的传输控制协议(TCP)连接,您的驱动程序与MongoDB实例之间维护的连接。连接池有助于减少应用程序需要创建的新连接数量,这可能会使应用程序运行得更快。
您可以在应用程序中使用连接池事件的信息来优化性能或了解客户端的生命周期。
订阅事件
您可以通过在应用程序中订阅它们来访问连接池事件的详细信息。以下示例演示了如何通过实例化一个PoolMonitor
并连接到部署来订阅PoolEvent
事件
var eventArray []*event.PoolEvent cxnMonitor := &event.PoolMonitor{ Started: func(e *event.PoolEvent) { eventArray = append(eventArray, e) }, } clientOpts := options.Client().ApplyURI(uri).SetPoolMonitor(cxnMonitor) client, err := mongo.Connect(context.TODO(), clientOpts)
事件描述
以下表格描述了驱动程序发出的池事件类型
池事件类型 | 描述 |
---|---|
ConnectionPoolCreated | 在创建连接池时创建。 |
ConnectionPoolReady | 在连接池就绪时创建。 |
ConnectionPoolCleared | 在池中所有连接都关闭时创建。 |
ConnectionPoolClosed | 在关闭连接池但在销毁服务器实例之前创建。 |
ConnectionCreated | 在创建连接时创建,但不一定是用于操作时。 |
ConnectionReady | 在连接完成握手并准备好用于操作后创建。 |
ConnectionClosed | 在关闭连接时创建。 |
ConnectionCheckOutStarted | 在操作尝试获取连接以执行时创建。 |
ConnectionCheckOutFailed | 在操作无法获取连接以执行时创建。 |
ConnectionCheckedOut | 在操作成功获取连接以执行时创建。 |
ConnectionCheckedIn | 在操作执行后,连接被检查回池中时创建。 |
示例事件文档
以下各节显示了每种连接池监控事件的示例输出。
ConnectionPoolCreated
*event.PoolEvent { "type": "ConnectionPoolCreated", "address": "...", "connectionId": 0, "options": { "maxPoolSize": 100, "minPoolSize": 0, "maxIdleTimeMS": 0 }, "reason": "", "serviceId": null, "error": null }
ConnectionPoolReady
*event.PoolEvent { "type": "ConnectionPoolReady", "address": "...", "connectionId": 0, "options": null, "reason": "", "serviceId": null, "error": null }
ConnectionPoolCleared
*event.PoolEvent { "type": "ConnectionPoolCleared", "address": "...", "connectionId": 0, "options": null, "reason": "", "serviceId": null, "error": null }
连接池已关闭
*event.PoolEvent { "type": "ConnectionPoolClosed", "address": "...", "connectionId": 0, "options": null, "reason": "", "serviceId": null, "error": null }
连接已创建
*event.PoolEvent { "type": "ConnectionCreated", "address": "...", "connectionId": 1, "options": null, "reason": "", "serviceId": null, "error": null }
连接就绪
*event.PoolEvent { "type": "ConnectionReady", "address": "...", "connectionId": 1, "options": null, "reason": "", "serviceId": null, "error": null }
连接已关闭
*event.PoolEvent { "type": "ConnectionClosed", "address": "...", "connectionId": 1, "options": null, "reason": "", "serviceId": null, "error": null }
连接检查出库开始
*event.PoolEvent { "type": "ConnectionCheckOutStarted", "address": "...", "connectionId": 0, "options": null, "reason": "", "serviceId": null, "error": null }
连接检查出库失败
*event.PoolEvent { "type": "ConnectionCheckOutFailed", "address": "...", "connectionId": 0, "options": null, "reason": "", "serviceId": null, "error": null }
连接检查出库
*event.PoolEvent { "type": "ConnectionCheckedOut", "address": "...", "connectionId": 1, "options": null, "reason": "", "serviceId": null, "error": null }
连接检查入库
*event.PoolEvent { "type": "ConnectionCheckedIn", "address": "...", "connectionId": 1, "options": null, "reason": "", "serviceId": null, "error": null }
附加信息
要了解更多关于监控MongoDB部署的信息,请参阅如何监控MongoDB 文章。
要了解更多关于连接到MongoDB的信息,请参阅连接指南.
API文档
要了解更多关于本指南中提到的方法和类型,请参阅以下API文档
PoolMonitor 类型
PoolEvent 类型