Docs 菜单
Docs 主页
/ / /
Node.js
/ /

集群监控

在此页面上

  • Overview
  • 事件订阅示例
  • 事件描述
  • 示例事件文档
  • serverDescriptionChanged
  • serverHeartbeatStarted
  • serverHeartbeatSucceeded
  • serverHeartbeatFailed
  • serverOpening
  • serverClosed
  • 拓扑结构Opening
  • topologyClosed
  • topologyDescriptionChanged

本指南向您介绍如何监控 MongoDB 实例、副本集或分片集群中的拓扑事件。当您连接到的实例或集群的状态发生变化时,驱动程序会创建拓扑结构事件,也称为服务器发现和监控 (SDAM) 事件。例如,当您建立新连接或集群选出新的主节点时,驱动程序会创建一个事件。

以下部分演示如何记录应用程序中的拓扑更改并探索这些事件中提供的信息。

您可以在应用程序中订阅一个或多个 SDAM 事件,从而使用驱动程序访问这些事件。 以下示例演示了如何连接到副本集并订阅由 MongoDB 部署创建的 SDAM 事件之一:

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>";
client.on(eventName, event => {
console.log(`received ${eventName}: ${JSON.stringify(event, null, 2)}`);
});
async function run() {
try {
// Establish and verify connection
await client.db("admin").command({ ping: 1 });
console.log("Connected successfully");
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);

您可以订阅以下任何 SDAM 事件:

事件名称
说明
serverOpening
在打开与实例的连接时创建。
serverClosed
在与实例的连接关闭时创建。
serverDescriptionChanged
在实例状态更改(例如从节点变为主节点)时创建。
topologyOpening
在尝试连接到实例之前创建。
topologyClosed
在拓扑结构中的所有实例连接关闭后创建。
topologyDescriptionChanged
在拓扑结构发生变化时创建,例如选举新的主节点或mongos代理断开连接。
serverHeartbeatStarted
在向 MongoDB 实例发出hello命令之前创建。
serverHeartbeatSucceeded
hello命令从 MongoDB 实例成功返回时创建。
serverHeartbeatFailed
当向特定 MongoDB 实例发出的hello命令无法返回成功响应时创建。

以下部分显示了每种类型的 SDAM 事件的输出示例。

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字段包含以下可能的值之一:

类型
说明
Unknown
未知实例
Standalone
独立运行的实例
Mongos
Mongo 代理实例
PossiblePrimary
至少一台服务器将其识别为主节点,但尚未得到所有实例的验证。
RSPrimary
主实例
RSSecondary
从节点实例
RSArbiter
仲裁节点实例
RSOther
RSGhost
ServerHeartbeatStartedEvent {
connectionId: 'localhost:27017'
}
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 {
duration: 20,
failure: MongoError('some error'),
connectionId: 'localhost:27017'
}
ServerOpeningEvent {
topologyId: 0,
address: 'localhost:27017'
}
ServerClosedEvent {
topologyId: 0,
address: 'localhost:27017'
}
TopologyOpeningEvent {
topologyId: 0
}
TopologyClosedEvent {
topologyId: 0
}
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字段包含以下可能的值之一:

类型
说明
Single
独立运行的实例
ReplicaSetWithPrimary
具有主节点的副本集
ReplicaSetNoPrimary
没有主节点的副本集
Sharded
分片集群
Unknown
未知拓扑

后退

监控