1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
===============
SDAM Monitoring
===============
The Node.js driver features SDAM Monitoring events,
allowing an application or tool to monitor changes in the drivers
view of a single server, replica set or ``mongos``. This allows an
application to react to changes of topology, such as a secondary
joining or leaving a replica set.
Overview of SDAM events
-----------------------
.. list-table::
:header-rows: 1
* - Event
- Applies To
- Description
* - serverOpening
- Server, Replicaset, Mongos
- Emitted when server connection is established.
* - serverClosed
- Server, Replicaset, Mongos
- Emitted when server connection gets closed.
* - serverDescriptionChanged
- Server, Replicaset, Mongos
- Emitted when server state changes (such as from secondary to primary).
* - topologyOpening
- Server, Replicaset, Mongos
- Emitted before any server connections are performed.
* - topologyClosed
- Server, Replicaset, Mongos
- Emitted when topology connections have all closed.
* - topologyDescriptionChanged
- Replicaset, Mongos
- Emitted when the topology shape changes, such as a new primary being elected or a mongos proxy disconnecting.
* - serverHeartbeatStarted
- Replicaset, Mongos
- Emitted before the ismaster command is issued to a MongoDB server.
* - serverHeartbeatSucceeded
- Replicaset, Mongos
- Emitted after a successful ismaster command was issued to a MongoDB server.
* - serverHeartbeatFailed
- Replicaset, Mongos
- Emitted if a ismaster command failed against a specific MongoDB server.
Simple Code Example
-------------------
The following example demonstrates how to connect to a replica set and monitor all the events that are emitted by the replica set topology.
.. code-block:: js
const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:31000,localhost:31001/?replicaSet=rs';
const client = new MongoClient(url);
client.on('serverDescriptionChanged', event => {
console.log('received serverDescriptionChanged');
console.log(JSON.stringify(event, null, 2));
});
client.on('serverHeartbeatStarted', event => {
console.log('received serverHeartbeatStarted');
console.log(JSON.stringify(event, null, 2));
});
client.on('serverHeartbeatSucceeded', event => {
console.log('received serverHeartbeatSucceeded');
console.log(JSON.stringify(event, null, 2));
});
client.on('serverHeartbeatFailed', event => {
console.log('received serverHeartbeatFailed');
console.log(JSON.stringify(event, null, 2));
});
client.on('serverOpening', event => {
console.log('received serverOpening');
console.log(JSON.stringify(event, null, 2));
});
client.on('serverClosed', event => {
console.log('received serverClosed');
console.log(JSON.stringify(event, null, 2));
});
client.on('topologyOpening', event => {
console.log('received topologyOpening');
console.log(JSON.stringify(event, null, 2));
});
client.on('topologyClosed', event => {
console.log('received topologyClosed');
console.log(JSON.stringify(event, null, 2));
});
client.on('topologyDescriptionChanged', event => {
console.log('received topologyDescriptionChanged');
console.log(JSON.stringify(event, null, 2));
});
async function run() {
try {
await client.connect();
console.log('successfully connected');
} finally {
await client.close();
}
}
run();
Example Documents Returned For Each Event Type
----------------------------------------------
The following examples serve as a guide to the format of the returned documents.
serverDescriptionChanged
^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: js
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
}
}
The ``type`` of the ``ServerDescription`` can be one of the following values:
.. list-table::
:header-rows: 1
* - Type
- Description
* - Unknown
- Unknown server
* - Standalone
- Standalone server
* - Mongos
- Mongos proxy
* - PossiblePrimary
- Not checked yet, but another server thinks this is a primary
* - RSPrimary
- Primary server
* - RSSecondary
- Secondary server
* - RSArbiter
- Arbiter
* - RSOther
- See `the spec <https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#rsghost-and-rsother>`_ for more details
* - RSGhost
- See `the spec <https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#rsghost-and-rsother>`_ for more details
serverHeartbeatStarted
^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: js
ServerHeartbeatStartedEvent {
connectionId: 'localhost:27017'
}
serverHeartbeatSucceeded
^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: js
ServerHeartbeatSucceededEvent {
duration: 1.939997,
reply:{
hosts: [ 'localhost:27017' ],
setName: 'rs',
setVersion: 1,
ismaster: 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
^^^^^^^^^^^^^^^^^^^^^
.. code-block:: js
ServerHeartbeatFailed {
duration: 20,
failure: MongoError('some error'),
connectionId: 'localhost:27017'
}
serverOpening
^^^^^^^^^^^^^
.. code-block:: js
ServerOpeningEvent {
topologyId: 0,
address: 'localhost:27017'
}
serverClosed
^^^^^^^^^^^^
.. code-block:: js
ServerClosedEvent {
topologyId: 0,
address: 'localhost:27017'
}
topologyOpening
^^^^^^^^^^^^^^^
.. code-block:: js
TopologyOpeningEvent {
topologyId: 0
}
topologyClosed
^^^^^^^^^^^^^^
.. code-block:: js
TopologyClosedEvent {
topologyId: 0
}
topologyDescriptionChanged
^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: js
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
}
}
The ``type`` field can be one of the following values:
.. list-table::
:header-rows: 1
* - Type
- Description
* - Single
- A single standalone server
* - ReplicaSetWithPrimary
- Replica set with a primary
* - ReplicaSetNoPrimary
- Replica set with no primary
* - Sharded
- A sharded cluster
* - Unknown
- Unknown topology
|