Skip to content

DOCSP-48169: Monitoring page async example #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions source/includes/monitoring/monitoring.py
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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("<connection URI>", event_listeners=listeners)
# end-monitoring
# end-monitoring-client

# start-monitoring-client-async
listeners = [MyCommandListener(), MyServerListener(), MyPoolListener()]
client = AsyncMongoClient("<connection URI>", event_listeners=listeners)
# end-monitoring-client-async
28 changes: 26 additions & 2 deletions source/monitoring.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------------

Expand Down
Loading