|
| 1 | +.. _rust-cluster-monitoring: |
| 2 | + |
| 3 | +================== |
| 4 | +Cluster Monitoring |
| 5 | +================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecols |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +This guide shows you how to use the {+driver-short+} to monitor topology |
| 17 | +events in a MongoDB instance, replica set, or sharded cluster. The |
| 18 | +driver creates topology events, also known as Server Discovery and |
| 19 | +Monitoring (SDAM) events, when there are any changes in the state of the |
| 20 | +instance or cluster that you are connected to. |
| 21 | + |
| 22 | +You can use information about topology changes in your |
| 23 | +application, or you can monitor cluster changes to learn more about |
| 24 | +how they affect your application. |
| 25 | + |
| 26 | +This guide includes the following sections: |
| 27 | + |
| 28 | +- :ref:`Event Descriptions <rust-cluster-monitoring-descriptions>` describes |
| 29 | + the SDAM events that the driver can generate |
| 30 | + |
| 31 | +- :ref:`Event Subscription Example <rust-cluster-monitoring-example>` |
| 32 | + provides sample code that shows how to subscribe to an SDAM event |
| 33 | + |
| 34 | +- :ref:`Example Event Documents <rust-cluster-monitoring-documents>` |
| 35 | + provides samples of each SDAM event |
| 36 | + |
| 37 | +- :ref:`Additional Information <rust-cluster-addtl-info>` |
| 38 | + provides links to resources and API documentation for types |
| 39 | + and methods mentioned in this guide |
| 40 | + |
| 41 | +.. _rust-cluster-monitoring-descriptions: |
| 42 | + |
| 43 | +Event Descriptions |
| 44 | +------------------ |
| 45 | + |
| 46 | +You can subscribe to one or more of the following SDAM events: |
| 47 | + |
| 48 | +.. list-table:: |
| 49 | + :widths: 33 67 |
| 50 | + :header-rows: 1 |
| 51 | + |
| 52 | + * - Event Name |
| 53 | + - Description |
| 54 | + |
| 55 | + * - `ServerDescriptionChangedEvent <{+api+}/event/sdam/struct.ServerDescriptionChangedEvent.html>`__ |
| 56 | + - Created when an instance state changes, such as when a replica set |
| 57 | + member changes from a secondary to a primary. |
| 58 | + |
| 59 | + * - `ServerOpeningEvent <{+api+}/event/sdam/struct.ServerOpeningEvent.html>`__ |
| 60 | + - Created when a connection to an instance, such as a replica set member, opens. |
| 61 | + |
| 62 | + * - `ServerClosedEvent <{+api+}/event/sdam/struct.ServerClosedEvent.html>`__ |
| 63 | + - Created when a connection to an instance, such as a replica set member, closes. |
| 64 | + |
| 65 | + * - `TopologyDescriptionChangedEvent <{+api+}/event/sdam/struct.TopologyDescriptionChangedEvent.html>`__ |
| 66 | + - Created when the topology description changes, such as when there |
| 67 | + is an election of a new primary or when a ``mongos`` proxy disconnects. |
| 68 | + |
| 69 | + * - `TopologyOpeningEvent <{+api+}/event/sdam/struct.TopologyOpeningEvent.html>`__ |
| 70 | + - Created before the driver attempts to connect to an instance. |
| 71 | + |
| 72 | + * - `TopologyClosedEvent <{+api+}/event/sdam/struct.TopologyClosedEvent.html>`__ |
| 73 | + - Created after all instance connections in the topology close. |
| 74 | + |
| 75 | + * - `ServerHeartbeatStartedEvent <{+api+}/event/sdam/struct.ServerHeartbeatStartedEvent.html>`__ |
| 76 | + - Created before the driver issues a ``hello`` command to an instance. |
| 77 | + |
| 78 | + * - `ServerHeartbeatSucceededEvent <{+api+}/event/sdam/struct.ServerHeartbeatSucceededEvent.html>`__ |
| 79 | + - Created when the ``hello`` command returns successfully from a |
| 80 | + MongoDB instance. |
| 81 | + |
| 82 | + * - `ServerHeartbeatFailedEvent <{+api+}/event/sdam/struct.ServerHeartbeatFailedEvent.html>`__ |
| 83 | + - Created when a ``hello`` command to a MongoDB instance does not |
| 84 | + return a successful response. |
| 85 | + |
| 86 | +.. _rust-cluster-monitoring-example: |
| 87 | + |
| 88 | +Event Subscription Example |
| 89 | +-------------------------- |
| 90 | + |
| 91 | +You can access one or more SDAM events by |
| 92 | +subscribing to them in your application. The following example connects |
| 93 | +to a MongoDB deployment and subscribes to the ``ServerOpeningEvent`` event type: |
| 94 | + |
| 95 | +.. literalinclude:: /includes/fundamentals/code-snippets/monitoring/sdam-subscribe.rs |
| 96 | + :language: rust |
| 97 | + :dedent: |
| 98 | + :start-after: begin-sdam |
| 99 | + :end-before: end-sdam |
| 100 | + |
| 101 | +.. _rust-cluster-monitoring-documents: |
| 102 | + |
| 103 | +Example Event Documents |
| 104 | +----------------------- |
| 105 | + |
| 106 | +The following sections show sample output for each type of SDAM event. |
| 107 | + |
| 108 | +ServerDescriptionChangedEvent |
| 109 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 110 | + |
| 111 | +.. code-block:: none |
| 112 | + :copyable: false |
| 113 | + |
| 114 | + ServerDescriptionChangedEvent { |
| 115 | + address: ..., |
| 116 | + topology_id: ..., |
| 117 | + previous_description: ..., |
| 118 | + new_description: ..., |
| 119 | + } |
| 120 | + |
| 121 | +ServerOpeningEvent |
| 122 | +~~~~~~~~~~~~~~~~~~ |
| 123 | + |
| 124 | +.. code-block:: none |
| 125 | + :copyable: false |
| 126 | + |
| 127 | + ServerOpeningEvent { |
| 128 | + address: ..., |
| 129 | + topology_id: ..., |
| 130 | + } |
| 131 | + |
| 132 | +ServerClosedEvent |
| 133 | +~~~~~~~~~~~~~~~~~ |
| 134 | + |
| 135 | +.. code-block:: none |
| 136 | + :copyable: false |
| 137 | + |
| 138 | + ServerClosedEvent { |
| 139 | + address: ..., |
| 140 | + topology_id: ..., |
| 141 | + } |
| 142 | + |
| 143 | +TopologyDescriptionChangedEvent |
| 144 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 145 | + |
| 146 | +.. code-block:: none |
| 147 | + :copyable: false |
| 148 | + |
| 149 | + TopologyDescriptionChangedEvent { |
| 150 | + topology_id: ..., |
| 151 | + previous_description: ..., |
| 152 | + new_description: ..., |
| 153 | + } |
| 154 | + |
| 155 | +TopologyOpeningEvent |
| 156 | +~~~~~~~~~~~~~~~~~~~~ |
| 157 | + |
| 158 | +.. code-block:: none |
| 159 | + :copyable: false |
| 160 | + |
| 161 | + TopologyOpeningEvent { |
| 162 | + topology_id: ..., |
| 163 | + } |
| 164 | + |
| 165 | +TopologyClosedEvent |
| 166 | +~~~~~~~~~~~~~~~~~~~ |
| 167 | + |
| 168 | +.. code-block:: none |
| 169 | + :copyable: false |
| 170 | + |
| 171 | + TopologyClosedEvent { |
| 172 | + topology_id: ..., |
| 173 | + } |
| 174 | + |
| 175 | +ServerHeartbeatStartedEvent |
| 176 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 177 | + |
| 178 | +.. code-block:: none |
| 179 | + :copyable: false |
| 180 | + |
| 181 | + ServerHeartbeatStartedEvent { |
| 182 | + server_address: ..., |
| 183 | + awaited: false, |
| 184 | + driver_connection_id: 12, |
| 185 | + server_connection_id: ..., |
| 186 | + } |
| 187 | + |
| 188 | +ServerHeartbeatSucceededEvent |
| 189 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 190 | + |
| 191 | +.. code-block:: none |
| 192 | + :copyable: false |
| 193 | + |
| 194 | + ServerHeartbeatSucceededEvent { |
| 195 | + duration: ..., |
| 196 | + reply: ..., |
| 197 | + server_address: ..., |
| 198 | + awaited: false, |
| 199 | + driver_connection_id: 12, |
| 200 | + server_connection_id: ..., |
| 201 | + } |
| 202 | + |
| 203 | +ServerHeartbeatFailedEvent |
| 204 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 205 | + |
| 206 | +.. code-block:: none |
| 207 | + :copyable: false |
| 208 | + |
| 209 | + ServerHeartbeatFailedEvent { |
| 210 | + duration: ..., |
| 211 | + failure: ..., |
| 212 | + server_address: ..., |
| 213 | + awaited: false, |
| 214 | + driver_connection_id: 12, |
| 215 | + server_connection_id: ..., |
| 216 | + } |
| 217 | + |
| 218 | +.. _rust-cluster-addtl-info: |
| 219 | + |
| 220 | +Additional Information |
| 221 | +---------------------- |
| 222 | + |
| 223 | +To learn more about monitoring a MongoDB deployment, see the :website:`How |
| 224 | +to Monitor MongoDB |
| 225 | +</basics/how-to-monitor-mongodb-and-what-metrics-to-monitor>` article. |
| 226 | + |
| 227 | +To learn more about connecting to MongoDB, see the |
| 228 | +:ref:`rust-connect-to-mongodb`. |
| 229 | + |
| 230 | +API Documentation |
| 231 | +~~~~~~~~~~~~~~~~~ |
| 232 | + |
| 233 | +To learn more about the methods and types mentioned in this |
| 234 | +guide, see the following API documentation: |
| 235 | + |
| 236 | +- `SdamEventHandler <{+api+}/event/sdam/trait.SdamEventHandler.html>`__ |
| 237 | +- `ClientOptions <{+api+}/options/struct.ClientOptions.html>`__ |
| 238 | +- `Client::with_options() <{+api+}/struct.Client.html#method.with_options>`__ |
0 commit comments