对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

集群监控

本指南向您展示如何使用 Go 驱动程序监控 MongoDB 实例、副本集或分片集群中的拓扑事件。当您连接到的实例或集群的状态发生任何变化时,驱动程序会创建拓扑结构事件,也称为服务器发现和监控 (SDAM) 事件。

您可以使用有关应用程序中的拓扑结构事件的信息来了解集群更改、评估集群运行状况或执行容量规划。

提示

日志记录

您还可以在应用程序中配置日志记录,以了解有关驱动程序事件的更多信息。 要了解更多信息,请参阅日志记录。

您可以通过在应用程序中订阅 SDAM 事件来访问有关这些事件的详细信息。 以下示例演示了如何通过实例化ServerMonitor并连接到部署来订阅ServerClosed事件:

var eventArray []*event.ServerClosedEventsrvMonitor := &event.ServerMonitor{ ServerClosed: func(e *event.ServerClosedEvent) { eventArray = append(eventArray, e) },}clientOpts := options.Client().ApplyURI(uri).SetServerMonitor(srvMonitor)client, err := mongo.Connect(context.TODO(), 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}