Skip to content

Commit 9dfc72a

Browse files
committed
JM tech review 1
1 parent fce8972 commit 9dfc72a

File tree

2 files changed

+74
-41
lines changed

2 files changed

+74
-41
lines changed

source/includes/monitoring/sdam.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
<?php
22

3-
require __DIR__ . '/vendor/autoload.php';
3+
require __DIR__ . "/vendor/autoload.php";
44

55
// start-mysubscriber
66
class MySubscriber implements MongoDB\Driver\Monitoring\SDAMSubscriber
77
{
88
private $stream;
9+
910
public function __construct($stream)
1011
{
1112
$this->stream = $stream;
1213
}
13-
public function serverOpening(MongoDB\Driver\Monitoring\ServerOpeningEvent $event): void
14-
{
15-
fwrite($this->stream, sprintf(
16-
'Server opening on %s:%s%s',
14+
15+
public function serverOpening(MongoDB\Driver\Monitoring\ServerOpeningEvent $event): void {
16+
fprintf(
17+
$this->stream,
18+
"Server opening on %s:%s\n",
1719
$event->getHost(),
1820
$event->getPort(),
19-
PHP_EOL,
20-
));
21+
);
2122
}
23+
2224
public function serverClosed(MongoDB\Driver\Monitoring\ServerClosedEvent $event): void {}
2325
public function serverChanged(MongoDB\Driver\Monitoring\ServerChangedEvent $event): void {}
2426
public function serverHeartbeatFailed(MongoDB\Driver\Monitoring\ServerHeartbeatFailedEvent $event): void {}
@@ -30,15 +32,18 @@ public function topologyOpening(MongoDB\Driver\Monitoring\TopologyOpeningEvent $
3032
}
3133
// end-mysubscriber
3234

33-
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
35+
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your connection URI');
3436
$client = new MongoDB\Client($uri);
3537

36-
$database = $client->db;
37-
$collection = $database->my_coll;
38+
$collection = $client->db->my_coll;
3839

3940
// start-add-sub
4041
$subscriber = new MySubscriber(STDERR);
4142
$client->addSubscriber($subscriber);
4243
// end-add-sub
4344

44-
$collection->insertOne(['x' => 100]);
45+
$collection->insertOne(["x" => 100]);
46+
47+
// start-remove-sub
48+
$client->removeSubscriber($subscriber);
49+
// end-remove-sub

source/monitoring/cluster-monitoring.txt

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ Cluster Monitoring
2020
Overview
2121
--------
2222

23-
This guide shows you how to use the {+php-library+} to monitor topology
24-
events in a MongoDB instance, replica set, or sharded cluster. The
25-
driver creates topology events, also known as Server Discovery and
26-
Monitoring (SDAM) events, when there are any changes in the state of the
27-
MongoDB instance or cluster that you are connected to.
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
26+
are any changes in the state of the MongoDB instance or cluster that you
27+
are connected to.
2828

29-
You might use information about topology events in your
30-
application to understand cluster changes, assess cluster health, or
31-
perform capacity planning.
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:
3234

3335
Subscribe to Events
3436
-------------------
@@ -55,8 +57,8 @@ server:
5557

5658
As shown in the preceding code, you must implement all of the methods
5759
of the ``SDAMSubscriber`` interface, even for events you are not subscribing to.
58-
You can implement these methods with empty bodies so that the library
59-
does not generate any messages for these events.
60+
The example defines the extra methods as empty so that the
61+
application does not output any messages for those events.
6062

6163
Then, use the ``addSubscriber()`` method to register ``MySubscriber``
6264
with the client, as shown in the following code:
@@ -81,53 +83,79 @@ outputs messages such as the following:
8183
Event Descriptions
8284
------------------
8385

84-
You can subscribe to the following SDAM events by implementing the
85-
corresponding method from the ``SDAMSubscriber`` interface:
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:
8690

8791
.. list-table::
8892
:widths: 35 65
8993
:header-rows: 1
9094

91-
* - Event Name
95+
* - Event Type
9296
- Description
9397

94-
* - ``ServerChangedEvent``
95-
- Created when an instance state changes, such as from secondary to
96-
primary.
98+
* - :php:`ServerChangedEvent <mongodb-driver-monitoring-serverchangedevent.php>`
99+
- Created when the server description changes, such as the server's type
100+
changing from secondary to primary.
97101

98-
* - ``ServerOpeningEvent``
99-
- Created when the server is initialized.
102+
* - :php:`ServerOpeningEvent <mongodb-driver-monitoring-serveropeningevent.php>`
103+
- Created when the server description is instantiated with its
104+
defaults.
100105

101-
* - ``ServerClosedEvent``
106+
* - :php:`ServerClosedEvent <mongodb-driver-monitoring-serverclosedevent.php>`
102107
- Created when the server is closed.
103108

104-
* - ``TopologyChangedEvent``
105-
- Created when the topology changes, such as an election of a new
106-
primary or disconnection of a ``mongos`` proxy.
109+
* - :php:`TopologyChangedEvent <mongodb-driver-monitoring-topologychangedevent.php>`
110+
- Created when the topology description changes, such when there is
111+
an election of a new primary or disconnection of a ``mongos`` proxy.
107112

108-
* - ``TopologyOpeningEvent``
109-
- Created when the topology is initialized.
113+
* - :php:`TopologyOpeningEvent <mongodb-driver-monitoring-topologyopeningevent.php>`
114+
- Created when the topology description is initialized.
110115

111-
* - ``TopologyClosedEvent``
116+
* - :php:`TopologyClosedEvent <mongodb-driver-monitoring-topologyclosedevent.php>`
112117
- Created when the topology is closed.
113118

114-
* - ``ServerHeartbeatStartedEvent``
115-
- Created when the heartbeat is started.
119+
* - :php:`ServerHeartbeatStartedEvent <mongodb-driver-monitoring-serverheartbeatstartedevent.php>`
120+
- Created when the server monitor sends a ``hello`` call to the server.
121+
This action is called a heartbeat.
116122

117-
* - ``ServerHeartbeatSucceededEvent``
123+
* - :php:`ServerHeartbeatSucceededEvent <mongodb-driver-monitoring-serverheartbeatsucceededevent.php>`
118124
- Created when the heartbeat succeeds.
119125

120-
* - ``ServerHeartbeatFailedEvent``
126+
* - :php:`ServerHeartbeatFailedEvent <mongodb-driver-monitoring-serverheartbeatfailedevent.php>`
121127
- Created when the heartbeat fails.
122128

129+
You can find a list of the monitoring subscriber classes and event
130+
methods in the :php:`Monitoring classes and subscriber functions
131+
<mongodb.monitoring.php>` section of the PHP manual.
132+
133+
Remove a Subscriber
134+
-------------------
135+
136+
Later in your application, you might not want to subscribe to
137+
SDAM events. To unregister a subscriber from your client, use the
138+
``MongoDB\Client::removeSubscriber()`` method. If you attempt to remove
139+
a nonexistent subscriber, the method doesn't perform any action.
140+
141+
The following code shows how to remove the subscriber that you
142+
registered in the :ref:`php-subscribe-sdam` section:
143+
144+
.. literalinclude:: /includes/monitoring/sdam.php
145+
:start-after: start-remove-sub
146+
:end-before: end-remove-sub
147+
:language: php
148+
:copyable:
149+
:dedent:
150+
123151
API Documentation
124152
-----------------
125153

126154
To learn more about any of the classes or methods discussed in this guide, see the
127155
following API documentation:
128156

129157
- :phpmethod:`MongoDB\Client::addSubscriber()`
130-
- :phpclass:`MongoDB\Client`
158+
- :phpmethod:`MongoDB\Client::removeSubscriber()`
131159

132160
To learn more about subscriber classes and methods, see the following
133161
pages in the PHP manual:

0 commit comments

Comments
 (0)