文档菜单
文档首页
/ / /
Go 驱动
/ /

集群监控

本页内容

  • 概述
  • 订阅事件
  • 事件说明
  • 示例事件文档
  • 服务器描述更改事件
  • 服务器开启事件
  • 服务器关闭事件
  • 拓扑描述更改事件
  • 拓扑开启事件
  • 拓扑关闭事件
  • 服务器心跳开始事件
  • 服务器心跳成功事件
  • 服务器心跳失败事件
  • API文档

本指南展示了如何使用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事件的示例输出。

*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": "..."
}

重要

因为驱动程序在部署拓扑锁定时调用 拓扑描述更改事件,所以此事件的回调(函数参数)不应尝试在同一客户端进行任何需要服务器选择的操作。

*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
}

返回

监控