monitoring – Tools for monitoring driver events.

Tools to monitor driver events.

Use subscribe() to register subscribers for specific events. Only events of type COMMAND are currently supported. Subscribers must be a subclass of Subscriber and implement started(), succeeded(), and failed().

For example, a simple logging subscriber might be implemented like this:

import logging

from pymongo import monitoring

class LoggingSubscriber(monitoring.Subscriber):

    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.subscribe(LoggingSubscriber(), monitoring.COMMAND)
pymongo.monitoring.COMMAND

The event type of user commands.

pymongo.monitoring.subscribe(subscriber, events=COMMAND)

Register a subscriber for events.

This version of PyMongo only publishes events of type COMMAND.

Parameters:
  • subscriber: A subclass of abstract class Subscriber.
  • events: Optional integer to set event subscriptions
pymongo.monitoring.get_subscribers(event=COMMAND)

Get the list of subscribers for event.

Parameters:
  • event: Return subscribers for this event type.
class pymongo.monitoring.Subscriber

Abstract base class for all subscribers.

failed(event)

Abstract method to handle CommandFailedEvent.

Parameters:
started(event)

Abstract method to handle CommandStartedEvent.

Parameters:
succeeded(event)

Abstract method to handle CommandSucceededEvent.

Parameters:
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, *args)

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.