Skip to content

Commit 410e1d6

Browse files
authored
Merge branch 'master' into docsp-46812-type-hints
2 parents c2bf569 + 0741c1b commit 410e1d6

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
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+
# Include 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+
# Include 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+
# Include other event method implementations here
24+
25+
listeners = [MyCommandListener(), MyServerListener(), MyPoolListener()]
26+
client = MongoClient("<connection URI>", event_listeners=listeners)
27+
# end-monitoring

source/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ MongoDB {+driver-short+} Documentation
2323
Security </security>
2424
Data Formats </data-formats>
2525
Logging </logging>
26+
Monitoring </monitoring>
2627
Third-Party Tools </tools>
2728
FAQ </faq>
2829
Troubleshooting </troubleshooting>

source/monitoring.txt

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
.. _pymongo-monitoring:
2+
3+
==========
4+
Monitoring
5+
==========
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: event, subscribe, listener
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 2
18+
:class: singlecol
19+
20+
Overview
21+
--------
22+
23+
In this guide, you can learn how to configure **monitoring** in {+driver-short+}
24+
by using {+driver-short+}'s callback-based interface. Monitoring is the process of
25+
gathering information about your application's performance and resource usage as it runs.
26+
This can help you make informed decisions when designing and debugging your application.
27+
28+
The driver provides information about your application by emitting events. You can
29+
listen for driver events to monitor your application.
30+
31+
.. note:: Event Logging
32+
33+
This page explains how to monitor your application in code. To learn how to record
34+
this information to an external log, see the :ref:`pymongo-logging` guide.
35+
36+
Event Types
37+
-----------
38+
39+
The type of event that the driver emits depends on the operation being performed.
40+
The following table describes the types of events that the driver emits:
41+
42+
.. list-table::
43+
:header-rows: 1
44+
:widths: 30 70
45+
46+
* - Event Type
47+
- Description
48+
* - Command events
49+
- Events related to MongoDB database commands, such as ``find``, ``insert``,
50+
``delete``, and ``count``. To learn how to use {+driver-short+} to run a
51+
database command, see :ref:`<pymongo-run-command>`. For more information about
52+
MongoDB database commands, see :manual:`Database Commands </reference/command/>`
53+
in the {+mdb-server+} manual.
54+
55+
As a security measure, the driver redacts the contents of some
56+
command events. This protects the sensitive information contained in these command
57+
events.
58+
59+
* - Server Discovery and Monitoring (SDAM) events
60+
- Events related to changes in the state of the MongoDB deployment.
61+
62+
* - Connection Pool events
63+
- Events related to the connection pool held by the driver.
64+
65+
For a complete list of events the driver emits, see the
66+
`pymongo.monitoring <{+api-root+}pymongo/monitoring.html>`__ API documentation.
67+
68+
Listening for Events
69+
--------------------
70+
71+
To monitor an event, you must pass an event listener to your application's ``MongoClient``.
72+
The following steps describe how to monitor your application by 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 listener 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+
- `monitoring <{+api-root+}pymongo/monitoring.html>`__
98+
- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__

0 commit comments

Comments
 (0)