From a6be12a2c9ed8341bcf3e680db70536b602d0c4f Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Mon, 17 Mar 2025 09:44:17 -0400 Subject: [PATCH 1/2] DOCSP-48169: Monitoring page async example --- source/includes/monitoring/monitoring.py | 13 ++++++++--- source/monitoring.txt | 28 ++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 5 deletions(-) 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..8b79b13b 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 +You can then pass these listeners to the ``MongoClient`` constructor to monitor your +application. 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 ----------------- From dcf817c8c6535f2d9e56e75670dc20fd6e256b22 Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Mon, 17 Mar 2025 09:50:47 -0400 Subject: [PATCH 2/2] Tweak --- source/monitoring.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/monitoring.txt b/source/monitoring.txt index 8b79b13b..281652f0 100644 --- a/source/monitoring.txt +++ b/source/monitoring.txt @@ -90,8 +90,8 @@ connection pool events: :end-before: end-monitoring-listeners :copyable: true -You can then pass these listeners to the ``MongoClient`` constructor to monitor your -application. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the +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::