diff --git a/source/includes/monitoring/monitoring.py b/source/includes/monitoring/monitoring.py index 4b8ff2fa..38f36785 100644 --- a/source/includes/monitoring/monitoring.py +++ b/source/includes/monitoring/monitoring.py @@ -1,9 +1,9 @@ -from pymongo import MongoClient +from pymongo import MongoClient, AsyncMongoClient from pymongo.monitoring import CommandListener, CommandSucceededEvent, ServerListener, \ ConnectionPoolListener, ServerHeartbeatStartedEvent, \ ConnectionCreatedEvent -# start-monitoring +# start-monitoring-listeners class MyCommandListener(CommandListener): def succeeded(self, event: CommandSucceededEvent): print(f"Command {event.command_name} succeeded") @@ -21,7 +21,14 @@ def connection_created(self, event: ConnectionCreatedEvent): print(f"Connection {event.connection_id} created") # Include other event method implementations here +# end-monitoring-listeners +# start-monitoring-client listeners = [MyCommandListener(), MyServerListener(), MyPoolListener()] client = MongoClient("", event_listeners=listeners) -# end-monitoring \ No newline at end of file +# end-monitoring-client + +# start-monitoring-client-async +listeners = [MyCommandListener(), MyServerListener(), MyPoolListener()] +client = AsyncMongoClient("", event_listeners=listeners) +# end-monitoring-client-async \ No newline at end of file diff --git a/source/monitoring.txt b/source/monitoring.txt index dd9e2c96..281652f0 100644 --- a/source/monitoring.txt +++ b/source/monitoring.txt @@ -86,10 +86,34 @@ connection pool events: .. literalinclude:: /includes/monitoring/monitoring.py :language: python - :start-after: start-monitoring - :end-before: end-monitoring + :start-after: start-monitoring-listeners + :end-before: end-monitoring-listeners :copyable: true +The following code passes instances of the preceding listeners to the ``MongoClient`` +constructor. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the +corresponding code: + +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/monitoring/monitoring.py + :language: python + :start-after: start-monitoring-client + :end-before: end-monitoring-client + :copyable: true + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/monitoring/monitoring.py + :language: python + :start-after: start-monitoring-client-async + :end-before: end-monitoring-client-async + :copyable: true + API Documentation -----------------