Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs 菜单
Docs 主页
/ / /
Go 驱动程序
/

监控应用程序事件

在本指南中,您可以学习;了解如何在MongoDB Go驱动程序中设立和配置监控。您可以在应用程序中使用Go驾驶员订阅事件监控事件。

监控包括收集运行的程序的活动信息,您可以将这些信息与应用程序性能管理库一起使用。

事件是驾驶员在运行期间发生的任何动作。Go驾驶员包含监听这些事件子集的功能。

通过监控Go驾驶员,您可以了解驱动程序的资源使用情况和性能,并帮助您在设计和调试应用程序时做出明智的决策。

在本指南中,您将了解如何执行这些任务:

  • 监控命令事件

  • 监控服务器发现和监控 (SDAM) 事件

  • 监控连接池事件

本指南介绍如何在代码中使用有关驾驶员活动的信息。要学习;了解如何在驾驶员中记录事件,请参阅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

在命令启动时创建。

CommandSucceededEvent

命令成功时创建。

CommandFailedEvent

命令不成功时创建。

以下部分显示了每种类型的命令监控事件的输出示例。

*event.CommandStartedEvent
{
"Command": "...",
"DatabaseName": "...",
"CommandName": "...",
"RequestID": ...,
"ConnectionID": "...",
"ServerConnectionID": ...,
"ServiceID": "..."
}
*event.CommandSucceededEvent
{
"DurationNanos": 38717583,
"Duration": 38717583,
"CommandName": "insert",
"RequestID": 13,
"ConnectionID": "...",
"ServerConnectionID": ...,
"ServiceID": null,
"Reply": "..."
}
*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 事件:

事件名称
说明

ServerDescriptionChangedEvent

在实例状态更改(例如从节点变为主节点)时创建。

ServerOpeningEvent

在服务器初始化时创建。

ServerClosedEvent

在服务器关闭时创建。

TopologyDescriptionChangedEvent

在拓扑结构发生变化时创建,例如选举新的主节点或断开mongos代理。

TopologyOpeningEvent

在初始化拓扑结构时创建。

TopologyClosedEvent

在拓扑结构闭合时创建。

ServerHeartbeatStartedEvent

在 heartbeat 启动时创建。

ServerHeartbeatSucceededEvent

当心跳成功时创建。

ServerHeartbeatFailedEvent

当 heartbeat 失败时创建。

以下部分显示了每种类型的 SDAM 事件的输出示例。

*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字段表示拓扑结构中单个服务器的类型,可以具有以下值:

说明

0

未知实例

1

独立运行的实例

2

副本集成员

6

主实例

10

从节点实例

18

仲裁节点实例

34

副本集虚影(无法查询的成员)

256

mongos 代理实例

512

负载均衡器实例

*event.ServerOpeningEvent
{
"Address": "...",
"TopologyID": "..."
}
*event.ServerClosedEvent
{
"Address": "...",
"TopologyID": "..."
}

重要

由于驱动程序在部署拓扑结构锁定时调用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 字段的值,请参阅“种类字段值”部分

*event.TopologyOpeningEvent
{
"TopologyID": "..."
}
*event.TopologyClosedEvent
{
"TopologyID": "..."
}
*event.ServerHeartbeatStartedEvent
{
"ConnectionID": "...",
"Awaited": true
}
*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 字段的值,请参阅“种类字段值”部分

*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

在创建连接池时创建。

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 的更多信息,请参阅“连接指南”。

要了解有关执行 MongoDB 操作的更多信息,请参阅CRUD 操作指南。

要进一步了解本指南所提及的方法和类型,请参阅以下 API 文档:

后退

监控和日志记录