monitoring
– Tools for monitoring driver events.¶
Tools to monitor driver events.
Use register()
to register global listeners for specific events.
Currently only command events are published. Listeners must be
a subclass of CommandListener
and implement
started()
, succeeded()
, and
failed()
.
For example, a simple command logger might be implemented like this:
import logging
from pymongo import monitoring
class CommandLogger(monitoring.CommandListener):
def started(self, event):
logging.info("Command {0.command_name} with request id "
"{0.request_id} started on server "
"{0.connection_id}".format(event))
def succeeded(self, event):
logging.info("Command {0.command_name} with request id "
"{0.request_id} on server {0.connection_id} "
"succeeded in {0.duration_micros} "
"microseconds".format(event))
def failed(self, event):
logging.info("Command {0.command_name} with request id "
"{0.request_id} on server {0.connection_id} "
"failed in {0.duration_micros} "
"microseconds".format(event))
monitoring.register(CommandLogger())
Event listeners can also be registered per instance of
MongoClient
:
client = MongoClient(event_listeners=[CommandLogger()])
Note that previously registered global listeners are automatically included when configuring per client event listeners. Registering a new global listener will not add that listener to existing client instances.
Note
Events are delivered synchronously. Application threads block
waiting for event handlers (e.g. started()
) to
return. Care must be taken to ensure that your event handlers are efficient
enough to not adversely affect overall application performance.
Warning
The command documents published through this API are not copies. If you intend to modify them in any way you must copy them in your event handler first.
-
pymongo.monitoring.
register
(listener)¶ Register a global event listener.
Parameters: - listener: A subclass of
CommandListener
.
- listener: A subclass of
-
class
pymongo.monitoring.
CommandListener
¶ Abstract base class for command listeners.
-
failed
(event)¶ Abstract method to handle CommandFailedEvent.
Parameters: - event: An instance of
CommandFailedEvent
- event: An instance of
-
started
(event)¶ Abstract method to handle CommandStartedEvent.
Parameters: - event: An instance of
CommandStartedEvent
- event: An instance of
-
succeeded
(event)¶ Abstract method to handle CommandSucceededEvent.
Parameters: - event: An instance of
CommandSucceededEvent
- event: An instance of
-
-
class
pymongo.monitoring.
CommandStartedEvent
(command, database_name, *args)¶ Event published when a command starts.
Parameters: - command: The command document.
- database_name: The name of the database this command was run against.
- request_id: The request id for this operation.
- connection_id: The address (host, port) of the server this command was sent to.
- operation_id: An optional identifier for a series of related events.
-
command
¶ The command document.
-
command_name
¶ The command name.
-
connection_id
¶ The address (host, port) of the server this command was sent to.
-
database_name
¶ The name of the database this command was run against.
-
operation_id
¶ An id for this series of events or None.
-
request_id
¶ The request id for this operation.
-
class
pymongo.monitoring.
CommandSucceededEvent
(duration, reply, command_name, request_id, connection_id, operation_id)¶ Event published when a command succeeds.
Parameters: - duration: The command duration as a datetime.timedelta.
- reply: The server reply document.
- command_name: The command name.
- request_id: The request id for this operation.
- connection_id: The address (host, port) of the server this command was sent to.
- operation_id: An optional identifier for a series of related events.
-
command_name
¶ The command name.
-
connection_id
¶ The address (host, port) of the server this command was sent to.
-
duration_micros
¶ The duration of this operation in microseconds.
-
operation_id
¶ An id for this series of events or None.
-
reply
¶ The server failure document for this operation.
-
request_id
¶ The request id for this operation.
-
class
pymongo.monitoring.
CommandFailedEvent
(duration, failure, *args)¶ Event published when a command fails.
Parameters: - duration: The command duration as a datetime.timedelta.
- failure: The server reply document.
- command_name: The command name.
- request_id: The request id for this operation.
- connection_id: The address (host, port) of the server this command was sent to.
- operation_id: An optional identifier for a series of related events.
-
command_name
¶ The command name.
-
connection_id
¶ The address (host, port) of the server this command was sent to.
-
duration_micros
¶ The duration of this operation in microseconds.
-
failure
¶ The server failure document for this operation.
-
operation_id
¶ An id for this series of events or None.
-
request_id
¶ The request id for this operation.