文档菜单
文档首页
/ / /
Go 驱动
/ /

连接监控

在本页

  • 概述
  • 订阅事件
  • 事件描述
  • 示例事件文档
  • ConnectionPoolCreated
  • ConnectionPoolReady
  • ConnectionPoolCleared
  • ConnectionPoolClosed
  • ConnectionCreated
  • ConnectionReady
  • ConnectionClosed
  • ConnectionCheckOutStarted
  • ConnectionCheckOutFailed
  • ConnectionCheckedOut
  • ConnectionCheckedIn
  • 更多信息
  • API 文档

本指南展示了如何使用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
在操作执行后,连接被检查回池中时创建。

以下各节显示了每种连接池监控事件的示例输出。

*event.PoolEvent
{
"type": "ConnectionPoolCreated",
"address": "...",
"connectionId": 0,
"options": {
"maxPoolSize": 100,
"minPoolSize": 0,
"maxIdleTimeMS": 0
},
"reason": "",
"serviceId": null,
"error": null
}
*event.PoolEvent
{
"type": "ConnectionPoolReady",
"address": "...",
"connectionId": 0,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}
*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文档

返回

命令监控