集群监控
在此页面上
Overview
本指南向您介绍如何监控 MongoDB 实例、副本集或分片集群中的拓扑事件。当您连接到的实例或集群的状态发生变化时,驱动程序会创建拓扑结构事件,也称为服务器发现和监控 (SDAM) 事件。例如,当您建立新连接或集群选出新的主节点时,驱动程序会创建一个事件。
以下部分演示如何记录应用程序中的拓扑更改并探索这些事件中提供的信息。
事件订阅示例
您可以在应用程序中订阅一个或多个 SDAM 事件,从而使用驱动程序访问这些事件。 以下示例演示了如何连接到副本集并订阅由 MongoDB 部署创建的 SDAM 事件之一:
/* Subscribe to SDAM event */ const { MongoClient } = require("mongodb"); // Replace the following with your MongoDB deployment's connection string const uri = "mongodb+srv://<clusterUrl>/?replicaSet=rs&writeConcern=majority"; const client = new MongoClient(uri); // Replace <event name> with the name of the event you are subscribing to const eventName = "<event name>"; // Subscribe to a specified event and print a message when the event is received client.on(eventName, event => { console.log(`received ${eventName}: ${JSON.stringify(event, null, 2)}`); }); async function run() { try { // Establish and verify connection to the database await client.db("admin").command({ ping: 1 }); console.log("Connected successfully"); } finally { // Close the database connection on completion or error await client.close(); } } run().catch(console.dir);
事件描述
您可以订阅以下任何 SDAM 事件:
事件名称 | 说明 |
---|---|
| 在打开与实例的连接时创建。 |
| 在与实例的连接关闭时创建。 |
| 在实例状态更改(例如从节点变为主节点)时创建。 |
| 在尝试连接到实例之前创建。 |
| 在拓扑结构中的所有实例连接关闭后创建。 |
| 在拓扑结构发生变化时创建,例如选举新的主节点或mongos代理断开连接。 |
| 在向 MongoDB 实例发出 |
| 当 |
| 当向特定 MongoDB 实例发出的 |
示例事件文档
以下部分显示了每种类型的 SDAM 事件的输出示例。
serverDescriptionChanged
ServerDescriptionChangedEvent { topologyId: 0, address: 'localhost:27017', previousDescription: ServerDescription { address: 'localhost:27017', error: null, roundTripTime: 0, lastUpdateTime: 1571251089030, lastWriteDate: null, opTime: null, type: 'Unknown', minWireVersion: 0, maxWireVersion: 0, hosts: [], passives: [], arbiters: [], tags: [] }, newDescription: ServerDescription { address: 'localhost:27017', error: null, roundTripTime: 0, lastUpdateTime: 1571251089051, lastWriteDate: 2019-10-16T18:38:07.000Z, opTime: { ts: Timestamp, t: 18 }, type: 'RSPrimary', minWireVersion: 0, maxWireVersion: 7, maxBsonObjectSize: 16777216, maxMessageSizeBytes: 48000000, maxWriteBatchSize: 100000, me: 'localhost:27017', hosts: [ 'localhost:27017' ], passives: [], arbiters: [], tags: [], setName: 'rs', setVersion: 1, electionId: ObjectID, primary: 'localhost:27017', logicalSessionTimeoutMinutes: 30, '$clusterTime': ClusterTime } }
此事件中ServerDescription
对象的type
字段包含以下可能的值之一:
类型 | 说明 |
---|---|
| 未知实例 |
| 独立运行的实例 |
| Mongo 代理实例 |
| 至少一台服务器将其识别为主节点,但尚未得到所有实例的验证。 |
| 主实例 |
| 从节点实例 |
| 仲裁节点实例 |
| 有关详情,请参阅 RSGhost 和 RSOTHER 规范 |
|
serverHeartbeatStarted
ServerHeartbeatStartedEvent { connectionId: 'localhost:27017' }
serverHeartbeatSucceeded
ServerHeartbeatSucceededEvent { duration: 1.939997, reply:{ hosts: [ 'localhost:27017' ], setName: 'rs', setVersion: 1, isWritablePrimary: true, secondary: false, primary: 'localhost:27017', me: 'localhost:27017', electionId: ObjectID, lastWrite: { opTime: { ts: [Timestamp], t: 18 }, lastWriteDate: 2019-10-16T18:38:17.000Z, majorityOpTime: { ts: [Timestamp], t: 18 }, majorityWriteDate: 2019-10-16T18:38:17.000Z }, maxBsonObjectSize: 16777216, maxMessageSizeBytes: 48000000, maxWriteBatchSize: 100000, localTime: 2019-10-16T18:38:19.589Z, logicalSessionTimeoutMinutes: 30, minWireVersion: 0, maxWireVersion: 7, readOnly: false, ok: 1, operationTime: Timestamp, '$clusterTime': ClusterTime }, connectionId: 'localhost:27017' }
serverHeartbeatFailed
ServerHeartbeatFailed { duration: 20, failure: MongoError('some error'), connectionId: 'localhost:27017' }
serverOpening
ServerOpeningEvent { topologyId: 0, address: 'localhost:27017' }
serverClosed
ServerClosedEvent { topologyId: 0, address: 'localhost:27017' }
拓扑结构Opening
TopologyOpeningEvent { topologyId: 0 }
topologyClosed
TopologyClosedEvent { topologyId: 0 }
topologyDescriptionChanged
TopologyDescriptionChangedEvent { topologyId: 0, previousDescription: TopologyDescription { type: 'ReplicaSetNoPrimary', setName: null, maxSetVersion: null, maxElectionId: null, servers: Map { 'localhost:27017' => ServerDescription }, stale: false, compatible: true, compatibilityError: null, logicalSessionTimeoutMinutes: null, heartbeatFrequencyMS: 10000, localThresholdMS: 15, options: Object, error: undefined, commonWireVersion: null }, newDescription: TopologyDescription { type: 'ReplicaSetWithPrimary', setName: 'rs', maxSetVersion: 1, maxElectionId: null, servers: Map { 'localhost:27017' => ServerDescription }, stale: false, compatible: true, compatibilityError: null, logicalSessionTimeoutMinutes: 30, heartbeatFrequencyMS: 10000, localThresholdMS: 15, options: Object, error: undefined, commonWireVersion: 7 } }
此事件中TopologyDescription
对象的type
字段包含以下可能的值之一:
类型 | 说明 |
---|---|
| 独立运行的实例 |
| 具有主节点的副本集 |
| 没有主节点的副本集 |
| 分片集群 |
| 未知拓扑 |