|
| 1 | +.. _php-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 {+php-library+} 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 | +.. _php-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, create a class that |
| 40 | +implements the ``MongoDB\Driver\Monitoring\SDAMSubscriber`` interface, |
| 41 | +then use the ``MongoDB\Client::addSubscriber()`` method to register the |
| 42 | +event subscriber with your ``MongoDB\Client`` instance. |
| 43 | + |
| 44 | +The following code creates the ``MySubscriber`` class, which implements |
| 45 | +the ``SDAMSubscriber`` interface. The class is defined with a method to |
| 46 | +output a message when a ``ServerOpeningEvent`` is generated by the |
| 47 | +server: |
| 48 | + |
| 49 | +.. literalinclude:: /includes/monitoring/sdam.php |
| 50 | + :start-after: start-mysubscriber |
| 51 | + :end-before: end-mysubscriber |
| 52 | + :language: php |
| 53 | + :copyable: |
| 54 | + :dedent: |
| 55 | + |
| 56 | +.. note:: |
| 57 | + |
| 58 | + As shown in the preceding code, you must implement all the methods |
| 59 | + of the ``SDAMSubscriber`` interface, even for events you are not subscribing to. |
| 60 | + The example defines the extra methods as empty so that the |
| 61 | + application does not output any messages for those events. |
| 62 | + |
| 63 | +Then, use the ``addSubscriber()`` method to register ``MySubscriber`` |
| 64 | +with the client, as shown in the following code: |
| 65 | + |
| 66 | +.. literalinclude:: /includes/monitoring/sdam.php |
| 67 | + :start-after: start-add-sub |
| 68 | + :end-before: end-add-sub |
| 69 | + :language: php |
| 70 | + :copyable: |
| 71 | + :dedent: |
| 72 | + |
| 73 | +When you run the application, your subscriber records the SDAM event and |
| 74 | +outputs messages such as the following: |
| 75 | + |
| 76 | +.. code-block:: none |
| 77 | + :copyable: false |
| 78 | + |
| 79 | + Server opening on ac-rmuag0v-shard-00-00.gh0qg50.mongodb.net:27017 |
| 80 | + Server opening on ac-rmuag0v-shard-00-01.gh0qg50.mongodb.net:27017 |
| 81 | + Server opening on ac-rmuag0v-shard-00-02.gh0qg50.mongodb.net:27017 |
| 82 | + |
| 83 | +Event Descriptions |
| 84 | +------------------ |
| 85 | + |
| 86 | +You can subscribe to SDAM events by implementing the corresponding |
| 87 | +method from the ``SDAMSubscriber`` interface. The following table |
| 88 | +provides the name of each SDAM event, linked to the class's API |
| 89 | +documentation, and a description of when the event is published: |
| 90 | + |
| 91 | +.. list-table:: |
| 92 | + :widths: 35 65 |
| 93 | + :header-rows: 1 |
| 94 | + |
| 95 | + * - Event Type |
| 96 | + - Description |
| 97 | + |
| 98 | + * - :php:`ServerChangedEvent <mongodb-driver-monitoring-serverchangedevent>` |
| 99 | + - Created when the server description changes, such as the server's |
| 100 | + type changing from secondary to primary. |
| 101 | + |
| 102 | + * - :php:`ServerOpeningEvent <mongodb-driver-monitoring-serveropeningevent>` |
| 103 | + - Created when a server connection is established. |
| 104 | + |
| 105 | + * - :php:`ServerClosedEvent <mongodb-driver-monitoring-serverclosedevent>` |
| 106 | + - Created when a server connection is closed. |
| 107 | + |
| 108 | + * - :php:`TopologyChangedEvent <mongodb-driver-monitoring-topologychangedevent>` |
| 109 | + - Created when the topology description changes, such as when there |
| 110 | + is an election of a new primary. |
| 111 | + |
| 112 | + * - :php:`TopologyOpeningEvent <mongodb-driver-monitoring-topologyopeningevent>` |
| 113 | + - Created when the driver first connects to the cluster. |
| 114 | + |
| 115 | + * - :php:`TopologyClosedEvent <mongodb-driver-monitoring-topologyclosedevent>` |
| 116 | + - Created when the driver disconnects from the cluster. |
| 117 | + |
| 118 | + * - :php:`ServerHeartbeatStartedEvent <mongodb-driver-monitoring-serverheartbeatstartedevent>` |
| 119 | + - Created when the server monitor sends a ``hello`` command to the server. |
| 120 | + This action is called a heartbeat. |
| 121 | + |
| 122 | + * - :php:`ServerHeartbeatSucceededEvent <mongodb-driver-monitoring-serverheartbeatsucceededevent>` |
| 123 | + - Created when the heartbeat succeeds. |
| 124 | + |
| 125 | + * - :php:`ServerHeartbeatFailedEvent <mongodb-driver-monitoring-serverheartbeatfailedevent>` |
| 126 | + - Created when the heartbeat fails. |
| 127 | + |
| 128 | +You can find a list of the monitoring subscriber classes and event |
| 129 | +methods in the :php:`Monitoring classes and subscriber functions |
| 130 | +<mongodb.monitoring>` section of the PHP manual. |
| 131 | + |
| 132 | +Remove a Subscriber |
| 133 | +------------------- |
| 134 | + |
| 135 | +Later in your application, you might not want to subscribe to |
| 136 | +SDAM events. To unregister a subscriber from your client, use the |
| 137 | +``MongoDB\Client::removeSubscriber()`` method. If you attempt to remove |
| 138 | +a nonexistent subscriber, the method doesn't perform any action. |
| 139 | + |
| 140 | +The following code shows how to remove the subscriber that you |
| 141 | +registered in the :ref:`php-subscribe-sdam` section: |
| 142 | + |
| 143 | +.. literalinclude:: /includes/monitoring/sdam.php |
| 144 | + :start-after: start-remove-sub |
| 145 | + :end-before: end-remove-sub |
| 146 | + :language: php |
| 147 | + :copyable: |
| 148 | + :dedent: |
| 149 | + |
| 150 | +API Documentation |
| 151 | +----------------- |
| 152 | + |
| 153 | +To learn more about any of the classes or methods discussed in this guide, see the |
| 154 | +following API documentation: |
| 155 | + |
| 156 | +- :phpmethod:`MongoDB\Client::addSubscriber()` |
| 157 | +- :phpmethod:`MongoDB\Client::removeSubscriber()` |
| 158 | + |
| 159 | +To learn more about subscriber classes and methods, see the following |
| 160 | +pages in the PHP manual: |
| 161 | + |
| 162 | +- :php:`MongoDB\Driver\Monitoring\SDAMSubscriber <mongodb-driver-monitoring-sdamsubscriber>` |
| 163 | +- :php:`MongoDB\Driver\Monitoring\ServerOpeningEvent <mongodb-driver-monitoring-serveropeningevent>` |
0 commit comments