-
Notifications
You must be signed in to change notification settings - Fork 20
DOCSP-46689: Monitoring #166
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
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from pymongo import MongoClient | ||
from pymongo.monitoring import CommandListener, CommandSucceededEvent, ServerListener, \ | ||
ConnectionPoolListener, ServerHeartbeatStartedEvent, \ | ||
ConnectionCreatedEvent | ||
|
||
# start-monitoring | ||
class MyCommandListener(CommandListener): | ||
def succeeded(self, event: CommandSucceededEvent): | ||
print(f"Command {event.command_name} succeeded") | ||
|
||
# Other event method implementations here | ||
|
||
class MyServerListener(ServerListener): | ||
def heartbeat_started(self, event: ServerHeartbeatStartedEvent): | ||
print(f"Heartbeat started on server with id: {event.connection_id}") | ||
|
||
# Other event method implementations here | ||
|
||
class MyPoolListener(ConnectionPoolListener): | ||
def connection_created(self, event: ConnectionCreatedEvent): | ||
print(f"Connection {event.connection_id} created") | ||
|
||
# Other event method implementations here | ||
|
||
listeners = [MyCommandListener(), MyServerListener(), MyPoolListener()] | ||
client = MongoClient("<connection URI>", event_listeners=listeners) | ||
# end-monitoring |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
.. _pymongo-monitoring: | ||
|
||
========== | ||
Monitoring | ||
========== | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: event, subscribe, listener | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to configure **monitoring** in {+driver-short+}. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would specifically call out that our monitoring API provides a callback-based interface for users to implement themselves. This currently reads to me that we provide a built-in way to monitor performance and resource usage, instead of an interface for users to do that using what we provide. |
||
Monitoring is the process of gathering information about your | ||
application's performance and resource usage as it runs. | ||
This can help you make informed decisions when designing and debugging your application. | ||
|
||
The driver provides information about your application by emitting events. You can | ||
listen for driver events to monitor your application. | ||
|
||
.. note:: Event Logging | ||
|
||
This page explains how to monitor your application in code. To learn how to record | ||
this information to an external log, see :ref:`Logging. <pymongo-logging>` | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Event Types | ||
----------- | ||
|
||
The type of event that the driver emits depends on the operation being performed. | ||
The following table describes the types of events that the driver emits: | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:widths: 30 70 | ||
|
||
* - Event Type | ||
- Description | ||
* - Command Events | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- Events related to MongoDB database commands, such as ``find``, ``insert``, | ||
``delete``, and ``count``. To learn how to use the {+driver-short+} to run a | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
database command, see :ref:`<pymongo-run-command>`. For more information about | ||
MongoDB database commands, see :manual:`Database Commands </reference/command/>` | ||
in the {+mdb-server+} manual. | ||
|
||
As a security measure, the driver redacts the contents of some | ||
command events. This protects the sensitive information contained in these command | ||
events. | ||
|
||
* - Server Discovery and Monitoring (SDAM) Events | ||
- Events related to changes in the state of the MongoDB deployment. | ||
|
||
* - Connection Pool Events | ||
- Events related to the connection pool held by the driver. | ||
|
||
For a complete list of events the driver emits, see the | ||
`pymongo.monitoring <{+api-root+}pymongo/monitoring.html>`__ API documentation. | ||
|
||
Listening for Events | ||
-------------------- | ||
|
||
To monitor an event, you must pass an event listener to your application's ``MongoClient``. | ||
The following steps describe how to monitor your application using an event listener: | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
1. Create a class that inherits from one of the event listener base classes | ||
provided by {+driver-short+}. The base class you choose depends on the type of event | ||
you want to monitor. For example, to monitor command events, create a class | ||
that inherits from ``CommandListener``. | ||
#. Implement the methods of the base class that correpond to the events you want to monitor. | ||
#. Pass an instance of your listener class to the ``MongoClient`` constructor. | ||
|
||
The following code implements a ``CommandListener`` to listen for command events, a | ||
``ServerListener`` to listen for SDAM events, and a ``ConnectionPoolListener`` to listen for | ||
connection pool events: | ||
|
||
.. literalinclude:: /includes/monitoring/monitoring.py | ||
:language: python | ||
:start-after: start-monitoring | ||
:end-before: end-monitoring | ||
:copyable: true | ||
|
||
API Documentation | ||
----------------- | ||
|
||
To learn more about the methods and classes used to monitor events in the driver, see the | ||
following API documentation: | ||
|
||
- `pymongo.monitoring <{+api-root+}pymongo/monitoring.html>`__ | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__ |
Uh oh!
There was an error while loading. Please reload this page.