Overview
在本指南中,您可以学习;了解如何在MongoDB Go驱动程序中设立和配置监控。您可以在应用程序中使用Go驾驶员订阅事件监控事件。
监控包括收集运行的程序的活动信息,您可以将这些信息与应用程序性能管理库一起使用。
事件是驾驶员在运行期间发生的任何动作。Go驾驶员包含监听这些事件子集的功能。
通过监控Go驾驶员,您可以了解驱动程序的资源使用情况和性能,并帮助您在设计和调试应用程序时做出明智的决策。
在本指南中,您将了解如何执行这些任务:
本指南介绍如何在代码中使用有关驾驶员活动的信息。要学习;了解如何在驾驶员中记录事件,请参阅Go驱动程序的 日志记录指南。
监控命令事件
命令事件是与MongoDB 数据库命令相关的事件。您可以通过在应用程序中使用驾驶员订阅一个或多个命令监控事件访问权限这些事件。
要学习;了解有关MongoDB 数据库命令的更多信息,请参阅服务器手册中的数据库命令指南。
订阅事件
您可以通过在应用程序中订阅命令事件来访问有关命令事件的详细信息。 以下示例演示了如何通过实例化CommandMonitor
并连接到部署来订阅 CommandStartedEvent
事件:
var eventArray []*event.CommandStartedEvent cmdMonitor := &event.CommandMonitor{ Started: func(ctx context.Context, e *event.CommandStartedEvent) { eventArray = append(eventArray, e) }, } clientOpts := options.Client().ApplyURI(uri).SetMonitor(cmdMonitor) client, err := mongo.Connect(clientOpts)
事件描述
您可以订阅以下一个或多个命令监控事件:
事件名称 | 说明 |
---|---|
| 在命令启动时创建。 |
| 命令成功时创建。 |
| 命令不成功时创建。 |
示例事件文档
以下部分显示了每种类型的命令监控事件的输出示例。
CommandStartedEvent
*event.CommandStartedEvent { "Command": "...", "DatabaseName": "...", "CommandName": "...", "RequestID": ..., "ConnectionID": "...", "ServerConnectionID": ..., "ServiceID": "..." }
CommandSucceededEvent
*event.CommandSucceededEvent { "DurationNanos": 38717583, "Duration": 38717583, "CommandName": "insert", "RequestID": 13, "ConnectionID": "...", "ServerConnectionID": ..., "ServiceID": null, "Reply": "..." }
CommandFailedEvent
*event.CommandFailedEvent { "DurationNanos": 38717583, "Duration": 38717583, "CommandName": "insert", "RequestID": 13, "ConnectionID": "...", "ServerConnectionID": ..., "ServiceID": null, "Failure": "..." }
监控服务器发现和监控事件
当您连接到的实例或集群的状态发生变化时, Go驾驶员会创建拓扑结构事件(也称为 SDAM 事件)。示例,当您建立新连接或集群选出新的主节点 (primary node in the replica set)节点时,驾驶员会创建一个事件。
以下部分演示如何记录应用程序中的拓扑更改并探索这些事件中提供的信息。
订阅事件
您可以通过在应用程序中订阅 SDAM 事件来访问有关这些事件的详细信息。 以下示例演示了如何通过实例化ServerMonitor
并连接到部署来订阅ServerClosed
事件:
var eventArray []*event.ServerClosedEvent srvMonitor := &event.ServerMonitor{ ServerClosed: func(e *event.ServerClosedEvent) { eventArray = append(eventArray, e) }, } clientOpts := options.Client().ApplyURI(uri).SetServerMonitor(srvMonitor) client, err := mongo.Connect(clientOpts)
事件描述
您可以通过指定ServerMonitor
实例的属性来订阅以下 SDAM 事件:
事件名称 | 说明 |
---|---|
| 在实例状态更改(例如从节点变为主节点)时创建。 |
| 在服务器初始化时创建。 |
| 在服务器关闭时创建。 |
| 在拓扑结构发生变化时创建,例如选举新的主节点或断开 |
| 在初始化拓扑结构时创建。 |
| 在拓扑结构闭合时创建。 |
| 在 heartbeat 启动时创建。 |
| 当心跳成功时创建。 |
| 当 heartbeat 失败时创建。 |
示例事件文档
以下部分显示了每种类型的 SDAM 事件的输出示例。
ServerDescriptionChangedEvent
*event.ServerDescriptionChangedEvent { "Address": "...", "TopologyID": "...", "PreviousDescription": { "Addr": "...", "Arbiters": null, "AverageRTT": 0, "AverageRTTSet": false, "Compression": null, "CanonicalAddr": "...", "ElectionID": "...", "HeartbeatInterval": 0, "HelloOK": false, "Hosts": null, "LastError": null, "LastUpdateTime": "...", "LastWriteTime": "...", "MaxBatchCount": 0, "MaxDocumentSize": 0, "MaxMessageSize": 0, "Members": null, "Passives": null, "Passive": false, "Primary": "...", "ReadOnly": false, "ServiceID": null, "SessionTimeoutMinutes": 0, "SetName": "...", "SetVersion": 0, "Tags": null, "TopologyVersion": null, "Kind": 0, "WireVersion": null }, "NewDescription": { "Addr": "...", "Arbiters": null, "AverageRTT": ..., "AverageRTTSet": true, "Compression": null, "CanonicalAddr": "...", "ElectionID": "...", "HeartbeatInterval": ..., "HelloOK": true, "Hosts": [...], "LastError": null, "LastUpdateTime": "...", "LastWriteTime": "...", "MaxBatchCount": ..., "MaxDocumentSize": ..., "MaxMessageSize": ..., "Members": [...], "Passives": null, "Passive": false, "Primary": "...", "ReadOnly": false, "ServiceID": null, "SessionTimeoutMinutes": 30, "SetName": "...", "SetVersion": 9, "Tags": [...], "TopologyVersion": {...}, "Kind": 10, "WireVersion": {...} } }
Kind
字段值
事件文档中的Kind
字段表示拓扑结构中单个服务器的类型,可以具有以下值:
值 | 说明 |
---|---|
| 未知实例 |
| 独立运行的实例 |
| 副本集成员 |
| 主实例 |
| 从节点实例 |
| 仲裁节点实例 |
| 副本集虚影(无法查询的成员) |
|
|
| 负载均衡器实例 |
ServerOpeningEvent
*event.ServerOpeningEvent { "Address": "...", "TopologyID": "..." }
ServerClosedEvent
*event.ServerClosedEvent { "Address": "...", "TopologyID": "..." }
TopologyDescriptionChangedEvent
重要
由于驱动程序在部署拓扑结构锁定时调用TopologyDescriptionChangedEvent
,因此此事件的回调(函数参数)不得尝试在同一客户端上进行任何需要选择服务器的操作。
*event.TopologyDescriptionChangedEvent { "TopologyID": "...", "PreviousDescription": { "Servers": [ { "Addr": "...", "Arbiters": null, "AverageRTT": 0, "AverageRTTSet": false, "Compression": null, "CanonicalAddr": "...", "ElectionID": "...", "HeartbeatInterval": 0, "HelloOK": false, "Hosts": null, "LastError": null, "LastUpdateTime": "...", "LastWriteTime": "...", "MaxBatchCount": 0, "MaxDocumentSize": 0, "MaxMessageSize": 0, "Members": null, "Passives": null, "Passive": false, "Primary": "...", "ReadOnly": false, "ServiceID": null, "SessionTimeoutMinutes": 0, "SetName": "...", "SetVersion": 0, "Tags": null, "TopologyVersion": null, "Kind": 0, "WireVersion": null }, ... ], "SetName": "...", "Kind": 10, "SessionTimeoutMinutes": 30, "CompatibilityErr": null }, "NewDescription": { "Servers": [...], "SetName": "...", "Kind": 10, "SessionTimeoutMinutes": 30, "CompatibilityErr": null } }
要解释 Kind
字段的值,请参阅“种类字段值”部分。
TopologyOpeningEvent
*event.TopologyOpeningEvent { "TopologyID": "..." }
TopologyClosedEvent
*event.TopologyClosedEvent { "TopologyID": "..." }
ServerHeartbeatStartedEvent
*event.ServerHeartbeatStartedEvent { "ConnectionID": "...", "Awaited": true }
ServerHeartbeatSucceededEvent
*event.ServerHeartbeatSucceededEvent { "DurationNanos": ..., "Reply": { "Addr": "...", "Arbiters": null, "AverageRTT": 0, "AverageRTTSet": false, "Compression": null, "CanonicalAddr": "...", "ElectionID": "...", "HeartbeatInterval": 0, "HelloOK": true, "Hosts": [...], "LastError": null, "LastUpdateTime": "...", "LastWriteTime": "...", "MaxBatchCount": ..., "MaxDocumentSize": ..., "MaxMessageSize": ..., "Members": [...], "Passives": null, "Passive": false, "Primary": "...", "ReadOnly": false, "ServiceID": null, "SessionTimeoutMinutes": 30, "SetName": "...", "SetVersion": 9, "Tags": [...], "TopologyVersion": {...}, "Kind": 6, "WireVersion": {...} }, "ConnectionID": "...", "Awaited": true }
要解释 Kind
字段的值,请参阅“种类字段值”部分。
ServerHeartbeatFailedEvent
*event.ServerHeartbeatFailedEvent { "DurationNanos": ..., "Failure": "<error message>" "ConnectionID": "...", "Awaited": true }
监控连接池事件
连接池是驾驶员与MongoDB实例维护的一设立打开的 TCP 连接。连接池有助于减少应用程序需要执行的网络握手次数,并可帮助应用程序更快地运行。
以下部分演示如何在应用程序中记录连接池事件并探索这些事件中提供的信息。
订阅事件
您可以通过在应用程序中订阅连接池事件来访问有关连接池事件的详细信息。 以下示例演示了如何通过实例化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(clientOpts)
事件描述
下表描述了驱动程序发出的池事件类型:
池事件类型 | 说明 |
---|---|
| 在创建连接池时创建。 |
| 当连接池准备就绪时创建。 |
| 当池中的所有连接都关闭时创建。 |
| 在销毁服务器实例之前关闭连接池时创建。 |
| 在创建连接时创建,但不一定是在用于操作时创建。 |
| 在连接完成握手并准备好用于操作后创建。 |
| 在连接关闭时创建。 |
| 当操作尝试获取用于执行的连接时创建。 |
| 当操作无法获取用于执行的连接时创建。 |
| 当操作成功获取用于执行的连接时创建。 |
| 在执行操作后,当连接被检回连接池时创建。 |
示例事件文档
以下几节显示了每种类型的连接池监控事件的样本输出。
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 }
connectionPoolClosed
*event.PoolEvent { "type": "ConnectionPoolClosed", "address": "...", "connectionId": 0, "options": null, "reason": "", "serviceId": null, "error": null }
connectionCreated
*event.PoolEvent { "type": "ConnectionCreated", "address": "...", "connectionId": 1, "options": null, "reason": "", "serviceId": null, "error": null }
connectionReady
*event.PoolEvent { "type": "ConnectionReady", "address": "...", "connectionId": 1, "options": null, "reason": "", "serviceId": null, "error": null }
connectionClosed
*event.PoolEvent { "type": "ConnectionClosed", "address": "...", "connectionId": 1, "options": null, "reason": "", "serviceId": null, "error": null }
connectionCheckOutStarted
*event.PoolEvent { "type": "ConnectionCheckOutStarted", "address": "...", "connectionId": 0, "options": null, "reason": "", "serviceId": null, "error": null }
connectionCheckOutFailed
*event.PoolEvent { "type": "ConnectionCheckOutFailed", "address": "...", "connectionId": 0, "options": null, "reason": "", "serviceId": null, "error": null }
connectionCheckedOut
*event.PoolEvent { "type": "ConnectionCheckedOut", "address": "...", "connectionId": 1, "options": null, "reason": "", "serviceId": null, "error": null }
connectionCheckedIn
*event.PoolEvent { "type": "ConnectionCheckedIn", "address": "...", "connectionId": 1, "options": null, "reason": "", "serviceId": null, "error": null }
更多信息
要了解有关监控 MongoDB 部署的更多信息,请参阅如何监控 MongoDB一文。
如需了解有关连接 MongoDB 的更多信息,请参阅“连接指南”。
要了解有关执行 MongoDB 操作的更多信息,请参阅CRUD 操作指南。
API 文档
要进一步了解本指南所提及的方法和类型,请参阅以下 API 文档:
事件包