集群监控
概述
本指南展示了如何使用Go驱动程序来监控MongoDB实例、副本集或分片集群中的拓扑事件。当连接的实例或集群状态发生变化时,驱动程序会创建拓扑事件,也称为服务器发现和监控(SDAM)事件。
您可以在应用程序中使用拓扑事件的信息来了解集群更改、评估集群健康或进行容量规划。
订阅事件
您可以通过在应用程序中订阅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事件
事件名称 | 描述 |
---|---|
服务器描述更改事件 | 在实例状态改变时创建(例如从次要状态变为主要状态)。 |
服务器开启事件 | 在服务器初始化时创建。 |
服务器关闭事件 | 在服务器关闭时创建。 |
拓扑描述更改事件 | 在拓扑结构改变时创建,例如新主要节点的选举或 mongos 代理的断开连接。 |
拓扑开启事件 | 在拓扑结构初始化时创建。 |
拓扑关闭事件 | 在拓扑结构关闭时创建。 |
服务器心跳开始事件 | 在心跳开始时创建。 |
服务器心跳成功事件 | 在心跳成功时创建。 |
服务器心跳失败事件 | 在心跳失败时创建。 |
示例事件文档
以下各节展示了每种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 | 负载均衡器实例 |
服务器开启事件
*event.ServerOpeningEvent { "Address": "...", "TopologyID": "..." }
服务器关闭事件
*event.ServerClosedEvent { "Address": "...", "TopologyID": "..." }
拓扑描述更改事件
重要
因为驱动程序在部署拓扑锁定时调用 拓扑描述更改事件
,所以此事件的回调(函数参数)不应尝试在同一客户端进行任何需要服务器选择的操作。
*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
字段的值,请参阅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
字段的值,请参阅 Kind 字段值部分。
服务器心跳失败事件
*event.ServerHeartbeatFailedEvent { "DurationNanos": ..., "Failure": "<error message>" "ConnectionID": "...", "Awaited": true }