集群监控
在此页面上
Overview
本指南向您展示如何使用 Go 驱动程序监控 MongoDB 实例、副本集或分片集群中的拓扑事件。当您连接到的实例或集群的状态发生任何变化时,驱动程序会创建拓扑结构事件,也称为服务器发现和监控 (SDAM) 事件。
您可以使用有关应用程序中的拓扑结构事件的信息来了解集群更改、评估集群运行状况或执行容量规划。
订阅事件
您可以通过在应用程序中订阅 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(context.TODO(), clientOpts)
事件描述
您可以通过指定ServerMonitor
实例的属性来订阅以下 SDAM 事件:
事件名称 | 说明 |
---|---|
ServerDescriptionChangedEvent | 在实例状态更改(例如从节点变为主节点)时创建。 |
ServerOpeningEvent | 在服务器初始化时创建。 |
ServerClosedEvent | 在服务器关闭时创建。 |
TopologyDescriptionChangedEvent | 在拓扑结构发生变化时创建,例如选举新的主节点或断开 mongos 代理。 |
TopologyOpeningEvent | 在初始化拓扑结构时创建。 |
TopologyClosedEvent | 在拓扑结构闭合时创建。 |
ServerHeartbeatStartedEvent | 在 heartbeat 启动时创建。 |
ServerHeartbeatSucceededEvent | 当心跳成功时创建。 |
ServerHeartbeatFailedEvent | 当 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
字段表示拓扑结构中单个服务器的类型,可以具有以下值:
值 | 说明 |
---|---|
0 | 未知实例 |
1 | 独立运行的实例 |
2 | 副本集成员 |
6 | 主实例 |
10 | 从节点实例 |
18 | 仲裁节点实例 |
34 | 副本集虚影(无法查询的成员) |
256 | mongos 代理实例 |
512 | 负载均衡器实例 |
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 }