Skip to content

Commit 3cfcda9

Browse files
committed
DOCSP-46689: Monitoring
1 parent 24a7bf9 commit 3cfcda9

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from pymongo import MongoClient
2+
from pymongo.monitoring import CommandListener, CommandSucceededEvent, ServerListener, \
3+
ConnectionPoolListener, ServerHeartbeatStartedEvent, \
4+
ConnectionCreatedEvent
5+
6+
# start-monitoring
7+
class MyCommandListener(CommandListener):
8+
def succeeded(self, event: CommandSucceededEvent):
9+
print(f"Command {event.command_name} succeeded")
10+
11+
# Other event method implementations here
12+
13+
class MyServerListener(ServerListener):
14+
def heartbeat_started(self, event: ServerHeartbeatStartedEvent):
15+
print(f"Heartbeat started on server with id: {event.connection_id}")
16+
17+
# Other event method implementations here
18+
19+
class MyPoolListener(ConnectionPoolListener):
20+
def connection_created(self, event: ConnectionCreatedEvent):
21+
print(f"Connection {event.connection_id} created")
22+
23+
# Other event method implementations here
24+
25+
listeners = [MyCommandListener(), MyServerListener(), MyPoolListener()]
26+
client = MongoClient("<connection URI>", event_listeners=listeners)
27+
# end-monitoring

source/monitoring.txt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,31 @@ For a copmlete list of events the driver emits, see the
6868
Listening for Events
6969
--------------------
7070

71-
To monitor an event, you must
71+
To monitor an event, you must pass an event listener to your application's ``MongoClient``.
72+
The following steps describe how to create monitor your application using an event listener:
73+
74+
1. Create a class that inherits from one of the event listener base classes
75+
provided by {+driver-short+}. The base class you choose depends on the type of event
76+
you want to monitor. For example, to monitor command events, create a class
77+
that inherits from ``CommandListener``.
78+
#. Implement the methods of the base class that correpond to the events you want to monitor.
79+
#. Pass an instance of your class to the ``MongoClient`` constructor.
80+
81+
The following code implements a ``CommandListener`` to listen for command events, a
82+
``ServerListener`` to listen for SDAM events, and a ``ConnectionPoolListener`` to listen for
83+
connection pool events:
84+
85+
.. literalinclude:: /includes/monitoring/monitoring.py
86+
:language: python
87+
:start-after: start-monitoring
88+
:end-before: end-monitoring
89+
:copyable: true
90+
91+
API Documentation
92+
-----------------
93+
94+
To learn more about the methods and classes used to monitor events in the driver, see the
95+
following API documentation:
96+
97+
- `pymongo.monitoring <{+api-root+}pymongo/monitoring.html>`__
98+
- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__

0 commit comments

Comments
 (0)