|
| 1 | +.. _c-cluster-monitoring: |
| 2 | + |
| 3 | +================== |
| 4 | +Cluster Monitoring |
| 5 | +================== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: code example, server, topology |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecols |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +This guide shows you how to use the {+driver-short+} to monitor server |
| 24 | +discovery and monitoring (SDAM) events in a MongoDB instance, replica |
| 25 | +set, or sharded cluster. These events occur when there are any changes |
| 26 | +in the state of the MongoDB instance or cluster that you are connected |
| 27 | +to. |
| 28 | + |
| 29 | +You might use information about SDAM events in your application to |
| 30 | +understand cluster changes, assess cluster health, or perform capacity |
| 31 | +planning. |
| 32 | + |
| 33 | +.. _c-subscribe-sdam: |
| 34 | + |
| 35 | +Subscribe to Events |
| 36 | +------------------- |
| 37 | + |
| 38 | +You can access details about SDAM events by subscribing to them |
| 39 | +in your application. To subscribe to an event, define an Application |
| 40 | +Performance Monitoring (APM) callback function to handle each event |
| 41 | +type you want to subscribe to. Pass a ``mongoc_apm_callbacks_t`` object |
| 42 | +to the ``mongoc_client_set_apm_callbacks()`` function to register the |
| 43 | +list of APM callbacks with a client. |
| 44 | + |
| 45 | +This code monitors server opening events by performing the following |
| 46 | +actions: |
| 47 | + |
| 48 | +- Defines a ``server_opening()`` APM callback function |
| 49 | +- Creates a ``mongoc_apm_callbacks_t`` object to store callbacks |
| 50 | +- Calls the ``mongoc_apm_set_server_opening_cb()`` function, which |
| 51 | + stores a pointer to the provided APM callback function in the |
| 52 | + ``mongoc_apm_callbacks_t`` object |
| 53 | +- Calls the ``mongoc_client_set_apm_callbacks()`` function, which registers |
| 54 | + the callback in the ``mongoc_apm_callbacks_t`` object with the client |
| 55 | + |
| 56 | +.. literalinclude:: /includes/monitoring/sdam.c |
| 57 | + :language: c |
| 58 | + :copyable: |
| 59 | + :linenos: |
| 60 | + :emphasize-lines: 10, 30-32 |
| 61 | + |
| 62 | +When you perform a database operation, the driver establishes a new connection to |
| 63 | +the server and your subscriber records the server opening event. The code outputs |
| 64 | +messages that resemble the following: |
| 65 | + |
| 66 | +.. code-block:: none |
| 67 | + :copyable: false |
| 68 | + |
| 69 | + Server opening: <host>:<port number> |
| 70 | + |
| 71 | +Event Descriptions |
| 72 | +------------------ |
| 73 | + |
| 74 | +You can subscribe to SDAM events by defining the corresponding |
| 75 | +APM callback function. The following table provides the name of |
| 76 | +each SDAM event, links to the type's API documentation, and describes |
| 77 | +when the event is published: |
| 78 | + |
| 79 | +.. list-table:: |
| 80 | + :widths: 35 65 |
| 81 | + :header-rows: 1 |
| 82 | + |
| 83 | + * - Event Type |
| 84 | + - Description |
| 85 | + |
| 86 | + * - `mongoc_apm_server_changed_t <{+api-libmongoc+}/mongoc_apm_server_changed_t.html>`__ |
| 87 | + - Created when the server description changes, such as the server's |
| 88 | + type changing from secondary to primary. |
| 89 | + |
| 90 | + * - `mongoc_apm_server_opening_t <{+api-libmongoc+}/mongoc_apm_server_opening_t.html>`__ |
| 91 | + - Created when a new server is added to the topology. For an example application that |
| 92 | + subscribes to this SDAM event, see :ref:`c-subscribe-sdam` on this page. |
| 93 | + |
| 94 | + * - `mongoc_apm_server_closed_t <{+api-libmongoc+}/mongoc_apm_server_closed_t.html>`__ |
| 95 | + - Created when an existing server is removed from the topology. |
| 96 | + |
| 97 | + * - `mongoc_apm_topology_changed_t <{+api-libmongoc+}/mongoc_apm_topology_changed_t.html>`__ |
| 98 | + - Created when the topology description changes, such as when there |
| 99 | + is an election of a new primary. |
| 100 | + |
| 101 | + * - `mongoc_apm_topology_opening_t <{+api-libmongoc+}/mongoc_apm_topology_opening_t.html>`__ |
| 102 | + - Created when the driver first connects to the cluster. |
| 103 | + |
| 104 | + * - `mongoc_apm_topology_closed_t <{+api-libmongoc+}/mongoc_apm_topology_closed_t.html>`__ |
| 105 | + - Created when the driver disconnects from the cluster. |
| 106 | + |
| 107 | + * - `mongoc_apm_server_heartbeat_started_t <{+api-libmongoc+}/mongoc_apm_server_heartbeat_started_t.html>`__ |
| 108 | + - Created when the server monitor sends a ``hello`` command to the server. |
| 109 | + This action is called a heartbeat. |
| 110 | + |
| 111 | + * - `mongoc_apm_server_heartbeat_succeeded_t <{+api-libmongoc+}/mongoc_apm_server_heartbeat_succeeded_t.html>`__ |
| 112 | + - Created when the heartbeat succeeds. |
| 113 | + |
| 114 | + * - `mongoc_apm_server_heartbeat_failed_t <{+api-libmongoc+}/mongoc_apm_server_heartbeat_failed_t.html>`__ |
| 115 | + - Created when the heartbeat fails. |
| 116 | + |
| 117 | +You can find information about each monitoring subscriber type and event |
| 118 | +method in the `Application Performance Monitoring <{+api-libmongoc+}/application-performance-monitoring.html>`__ |
| 119 | +section of the API documentation. |
| 120 | + |
| 121 | +API Documentation |
| 122 | +----------------- |
| 123 | + |
| 124 | +To learn more about the functions discussed in this guide, see the |
| 125 | +following API documentation: |
| 126 | + |
| 127 | +- `mongoc_apm_set_server_opening_cb() <{+api-libmongoc+}/mongoc_apm_set_server_opening_cb.html>`__ |
| 128 | +- `mongoc_client_set_apm_callbacks() <{+api-libmongoc+}/mongoc_client_set_apm_callbacks.html>`__ |
0 commit comments