Skip to content

Commit 4eef60d

Browse files
authored
RUBY-1415 Add SDAM monitoring example to ruby driver tutorial (#1066)
1 parent 861b57b commit 4eef60d

File tree

1 file changed

+181
-2
lines changed

1 file changed

+181
-2
lines changed

source/tutorials/ruby-driver-monitoring.txt

Lines changed: 181 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,187 @@ all clients or on a per-client basis:
8585

8686
Sample output:
8787

88+
.. code-block:: none
89+
8890
D, [2018-09-23T13:47:31.258020 #4692] DEBUG -- : COMMAND | 127.0.0.1:27027 | test.ismaster | STARTED | {"ismaster"=>1, "$readPreference"=>{"mode"=>"primary"}, "lsid"=>{"id"=><BSON::Binary:0x47111693353080 type=uuid data=0x730341e880dc40a2...>}}
8991
D, [2018-09-23T13:47:31.259145 #4692] DEBUG -- : COMMAND | 127.0.0.1:27027 | test.ismaster | SUCCEEDED | 0.000791175s
9092

9193

94+
Server Discovery And Monitoring
95+
-------------------------------
96+
97+
The Ruby driver implements `Server Discovery And Monitoring (SDAM) specification
98+
<https://github.com/mongodb/specifications/tree/master/source/server-discovery-and-monitoring>`_.
99+
and makes the following events available to the application:
100+
101+
- Topology opening
102+
- Server opening
103+
- Server description changed
104+
- Topology changed
105+
- Server closed
106+
- Topology closed
107+
- Heartbeat events (covered below in a separate section)
108+
109+
For all events other than the heartbeat events, the ``succeeded`` method
110+
will be called on each event subscriber with the event as the sole argument.
111+
Available data for events varies, therefore to log the events a separate
112+
class is needed for each event type. A simple SDAM logging subscriber
113+
can look like the following:
114+
115+
.. code-block:: ruby
116+
117+
class SDAMLogSubscriber
118+
include Mongo::Loggable
119+
120+
def succeeded(event)
121+
log_debug(format_event(event))
122+
end
123+
124+
private
125+
126+
def logger
127+
Mongo::Logger.logger
128+
end
129+
130+
def format_message(message)
131+
format("SDAM | %s".freeze, message)
132+
end
133+
end
134+
135+
class TopologyOpeningLogSubscriber < SDAMLogSubscriber
136+
private
137+
138+
def format_event(event)
139+
"Topology type '#{event.topology.display_name}' initializing."
140+
end
141+
end
142+
143+
class ServerOpeningLogSubscriber < SDAMLogSubscriber
144+
private
145+
146+
def format_event(event)
147+
"Server #{event.address} initializing."
148+
end
149+
end
150+
151+
class ServerDescriptionChangedLogSubscriber < SDAMLogSubscriber
152+
private
153+
154+
def format_event(event)
155+
"Server description for #{event.address} changed from " +
156+
"'#{event.previous_description.server_type}' to '#{event.new_description.server_type}'."
157+
end
158+
end
159+
160+
class TopologyChangedLogSubscriber < SDAMLogSubscriber
161+
private
162+
163+
def format_event(event)
164+
if event.previous_topology != event.new_topology
165+
"Topology type '#{event.previous_topology.display_name}' changed to " +
166+
"type '#{event.new_topology.display_name}'."
167+
else
168+
"There was a change in the members of the '#{event.new_topology.display_name}' " +
169+
"topology."
170+
end
171+
end
172+
end
173+
174+
class ServerClosedLogSubscriber < SDAMLogSubscriber
175+
private
176+
177+
def format_event(event)
178+
"Server #{event.address} connection closed."
179+
end
180+
end
181+
182+
class TopologyClosedLogSubscriber < SDAMLogSubscriber
183+
private
184+
185+
def format_event(event)
186+
"Topology type '#{event.topology.display_name}' closed."
187+
end
188+
end
189+
190+
To subscribe to SDAM events globally:
191+
192+
.. code-block:: ruby
193+
194+
topology_opening_subscriber = TopologyOpeningLogSubscriber.new
195+
server_opening_subscriber = ServerOpeningLogSubscriber.new
196+
server_description_changed_subscriber = ServerDescriptionChangedLogSubscriber.new
197+
topology_changed_subscriber = TopologyChangedLogSubscriber.new
198+
server_closed_subscriber = ServerClosedLogSubscriber.new
199+
topology_closed_subscriber = TopologyClosedLogSubscriber.new
200+
201+
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::TOPOLOGY_OPENING,
202+
topology_opening_subscriber)
203+
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::SERVER_OPENING,
204+
server_opening_subscriber)
205+
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::SERVER_DESCRIPTION_CHANGED,
206+
server_description_changed_subscriber)
207+
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::TOPOLOGY_CHANGED,
208+
topology_changed_subscriber)
209+
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::SERVER_CLOSED,
210+
server_closed_subscriber)
211+
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::TOPOLOGY_CLOSED,
212+
topology_closed_subscriber)
213+
214+
Subscribing to SDAM events for a single client is a little more involved
215+
since the events may be published during the client's construction:
216+
217+
.. code-block:: ruby
218+
219+
topology_opening_subscriber = TopologyOpeningLogSubscriber.new
220+
server_opening_subscriber = ServerOpeningLogSubscriber.new
221+
server_description_changed_subscriber = ServerDescriptionChangedLogSubscriber.new
222+
topology_changed_subscriber = TopologyChangedLogSubscriber.new
223+
server_closed_subscriber = ServerClosedLogSubscriber.new
224+
topology_closed_subscriber = TopologyClosedLogSubscriber.new
225+
226+
sdam_proc = Proc.new do |client|
227+
client.subscribe(Mongo::Monitoring::TOPOLOGY_OPENING,
228+
topology_opening_subscriber)
229+
client.subscribe(Mongo::Monitoring::SERVER_OPENING,
230+
server_opening_subscriber)
231+
client.subscribe(Mongo::Monitoring::SERVER_DESCRIPTION_CHANGED,
232+
server_description_changed_subscriber)
233+
client.subscribe(Mongo::Monitoring::TOPOLOGY_CHANGED,
234+
topology_changed_subscriber)
235+
client.subscribe(Mongo::Monitoring::SERVER_CLOSED,
236+
server_closed_subscriber)
237+
client.subscribe(Mongo::Monitoring::TOPOLOGY_CLOSED,
238+
topology_closed_subscriber)
239+
end
240+
241+
client = Mongo::Client.new(['127.0.0.1:27017'], database: 'test',
242+
sdam_proc: sdam_proc)
243+
244+
Sample output:
245+
246+
.. code-block:: none
247+
248+
D, [2018-10-09T13:58:03.489461 #22079] DEBUG -- : SDAM | Topology type 'Unknown' initializing.
249+
D, [2018-10-09T13:58:03.489699 #22079] DEBUG -- : SDAM | Server 127.0.0.1:27100 initializing.
250+
D, [2018-10-09T13:58:03.491384 #22079] DEBUG -- : SDAM | Server description for 127.0.0.1:27100 changed from 'unknown' to 'unknown'.
251+
D, [2018-10-09T13:58:03.491642 #22079] DEBUG -- : SDAM | Server localhost:27100 initializing.
252+
D, [2018-10-09T13:58:03.493199 #22079] DEBUG -- : SDAM | Server description for localhost:27100 changed from 'unknown' to 'primary'.
253+
D, [2018-10-09T13:58:03.493473 #22079] DEBUG -- : SDAM | Server localhost:27101 initializing.
254+
D, [2018-10-09T13:58:03.494874 #22079] DEBUG -- : SDAM | Server description for localhost:27101 changed from 'unknown' to 'secondary'.
255+
D, [2018-10-09T13:58:03.495139 #22079] DEBUG -- : SDAM | Server localhost:27102 initializing.
256+
D, [2018-10-09T13:58:03.496504 #22079] DEBUG -- : SDAM | Server description for localhost:27102 changed from 'unknown' to 'secondary'.
257+
D, [2018-10-09T13:58:03.496777 #22079] DEBUG -- : SDAM | Topology type 'Unknown' changed to type 'ReplicaSetNoPrimary'.
258+
D, [2018-10-09T13:58:03.497306 #22079] DEBUG -- : SDAM | Server 127.0.0.1:27100 connection closed.
259+
D, [2018-10-09T13:58:03.497606 #22079] DEBUG -- : SDAM | Topology type 'ReplicaSetNoPrimary' changed to type 'ReplicaSetWithPrimary'.
260+
261+
# client.close
262+
263+
D, [2018-10-09T13:58:05.342057 #22079] DEBUG -- : SDAM | Server localhost:27100 connection closed.
264+
D, [2018-10-09T13:58:05.342299 #22079] DEBUG -- : SDAM | Server localhost:27101 connection closed.
265+
D, [2018-10-09T13:58:05.342565 #22079] DEBUG -- : SDAM | Server localhost:27102 connection closed.
266+
D, [2018-10-09T13:58:05.342693 #22079] DEBUG -- : SDAM | Topology type 'ReplicaSetWithPrimary' closed.
267+
268+
92269
Server Heartbeats
93270
-----------------
94271

@@ -149,8 +326,10 @@ events globally or for a specific client:
149326

150327
Sample output:
151328

152-
D, [2018-09-23T13:44:10.707018 #1739] DEBUG -- : HEARTBEAT | 127.0.0.1:27027 | STARTED
153-
D, [2018-09-23T13:44:10.707778 #1739] DEBUG -- : HEARTBEAT | 127.0.0.1:27027 | SUCCEEDED | 0.000772381s
329+
.. code-block:: none
330+
331+
D, [2018-09-23T13:44:10.707018 #1739] DEBUG -- : HEARTBEAT | 127.0.0.1:27027 | STARTED
332+
D, [2018-09-23T13:44:10.707778 #1739] DEBUG -- : HEARTBEAT | 127.0.0.1:27027 | SUCCEEDED | 0.000772381s
154333

155334

156335
Disabling Monitoring

0 commit comments

Comments
 (0)