|  | 
|  | 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