Module Expr Str Import alias Import alias ImportFrom alias alias Assign NameStore Call NameLoad Str TupleLoad Str Assign NameStore Call NameLoad ListLoad ClassDef bases NameLoad body Expr Str FunctionDef arguments args NameParam NameParam defaults body Expr Str Raise NameLoad decorator_list FunctionDef arguments args NameParam NameParam defaults body Expr Str Raise NameLoad decorator_list FunctionDef arguments args NameParam NameParam defaults body Expr Str Raise NameLoad decorator_list decorator_list FunctionDef arguments args NameParam defaults body Expr Str If Call NameLoad NameLoad Str body Return Call NameLoad BinOpMult Call AttributeLoad NameLoad attr Num Return BinOpAdd AttributeLoad NameLoad attr BinOpMult BinOpAdd AttributeLoad NameLoad attr BinOpMult BinOpMult AttributeLoad NameLoad attr Num Num Num decorator_list FunctionDef arguments args NameParam NameParam defaults body Expr Str If UnaryOpNot Call NameLoad NameLoad NameLoad body Raise Call NameLoad BinOpMod Str TupleLoad NameLoad For NameStore NameLoad body If UnaryOpNot Call NameLoad NameLoad NameLoad body Raise Call NameLoad Str Return NameLoad decorator_list FunctionDef arguments args NameParam defaults body Expr Str Expr Call NameLoad Str ListLoad NameLoad Expr Call AttributeLoad AttributeLoad NameLoad attr attr NameLoad decorator_list FunctionDef arguments args defaults body Expr Str If AttributeLoad NameLoad attr body Assign NameStore Call AttributeLoad NameLoad attr TryFinally body TryExcept body Expr Call AttributeLoad NameLoad attr SubscriptLoad NameLoad Index Num SubscriptLoad NameLoad Index Num SubscriptLoad NameLoad Index Num NameLoad AttributeLoad NameLoad attr handlers ExceptHandler type NameLoad body Pass finalbody Delete NameDel decorator_list Assign NameStore Call NameLoad ListLoad Str Str Str Str Str Str Str Str Str ClassDef bases NameLoad body Expr Str Assign NameStore TupleLoad Str Str Str Str FunctionDef arguments args NameParam NameParam NameParam NameParam NameParam defaults body Assign AttributeStore NameLoad attr NameLoad Assign AttributeStore NameLoad attr NameLoad Assign AttributeStore NameLoad attr NameLoad Assign AttributeStore NameLoad attr NameLoad decorator_list FunctionDef arguments args NameParam defaults body Expr Str Return AttributeLoad NameLoad attr decorator_list NameLoad FunctionDef arguments args NameParam defaults body Expr Str Return AttributeLoad NameLoad attr decorator_list NameLoad FunctionDef arguments args NameParam defaults body Expr Str Return AttributeLoad NameLoad attr decorator_list NameLoad FunctionDef arguments args NameParam defaults body Expr Str Return AttributeLoad NameLoad attr decorator_list NameLoad decorator_list ClassDef bases NameLoad body Expr Str Assign NameStore TupleLoad Str Str FunctionDef arguments args NameParam NameParam NameParam defaults vararg body If UnaryOpNot NameLoad body Raise Call NameLoad BinOpMod Str TupleLoad NameLoad Assign NameStore Call NameLoad Call NameLoad NameLoad Expr Call AttributeLoad Call NameLoad NameLoad NameLoad attr NameLoad NameLoad If CompareIn Call AttributeLoad NameLoad attr NameLoad body Assign AttributeStore NameLoad attr Dict orelse Assign AttributeStore NameLoad attr NameLoad Assign AttributeStore NameLoad attr NameLoad decorator_list FunctionDef arguments args NameParam defaults body Expr Str Return AttributeLoad NameLoad attr decorator_list NameLoad FunctionDef arguments args NameParam defaults body Expr Str Return AttributeLoad NameLoad attr decorator_list NameLoad decorator_list ClassDef bases NameLoad body Expr Str Assign NameStore TupleLoad Str Str FunctionDef arguments args NameParam NameParam NameParam NameParam NameParam NameParam NameParam defaults body Expr Call AttributeLoad Call NameLoad NameLoad NameLoad attr NameLoad NameLoad NameLoad NameLoad Assign AttributeStore NameLoad attr Call NameLoad NameLoad If CompareIn Call AttributeLoad NameLoad attr NameLoad body Assign AttributeStore NameLoad attr Dict orelse Assign AttributeStore NameLoad attr NameLoad decorator_list FunctionDef arguments args NameParam defaults body Expr Str Return AttributeLoad NameLoad attr decorator_list NameLoad FunctionDef arguments args NameParam defaults body Expr Str Return AttributeLoad NameLoad attr decorator_list NameLoad decorator_list ClassDef bases NameLoad body Expr Str Assign NameStore TupleLoad Str Str FunctionDef arguments args NameParam NameParam NameParam defaults vararg body Expr Call AttributeLoad Call NameLoad NameLoad NameLoad attr NameLoad Assign AttributeStore NameLoad attr Call NameLoad NameLoad Assign AttributeStore NameLoad attr NameLoad decorator_list FunctionDef arguments args NameParam defaults body Expr Str Return AttributeLoad NameLoad attr decorator_list NameLoad FunctionDef arguments args NameParam defaults body Expr Str Return AttributeLoad NameLoad attr decorator_list NameLoad decorator_list ClassDef bases NameLoad body Expr Str FunctionDef arguments args NameParam NameParam defaults body Assign AttributeStore NameLoad attr SubscriptLoad AttributeLoad NameLoad attr Slice If CompareIsNot NameLoad NameLoad body Expr Call AttributeLoad AttributeLoad NameLoad attr attr NameLoad Assign AttributeStore NameLoad attr Call NameLoad AttributeLoad NameLoad attr decorator_list FunctionDef arguments args NameParam defaults body Expr Str Return AttributeLoad NameLoad attr decorator_list NameLoad FunctionDef arguments args NameParam defaults body Expr Str Return SubscriptLoad AttributeLoad NameLoad attr Slice decorator_list NameLoad FunctionDef arguments args NameParam NameParam NameParam NameParam NameParam NameParam defaults NameLoad body Expr Str If CompareIs NameLoad NameLoad body Assign NameStore NameLoad Assign NameStore Call NameLoad NameLoad NameLoad NameLoad NameLoad NameLoad For NameStore AttributeLoad NameLoad attr body TryExcept body Expr Call AttributeLoad NameLoad attr NameLoad handlers ExceptHandler type NameLoad body Expr Call NameLoad decorator_list FunctionDef arguments args NameParam NameParam NameParam NameParam NameParam NameParam NameParam defaults NameLoad body Expr Str If CompareIs NameLoad NameLoad body Assign NameStore NameLoad Assign NameStore Call NameLoad NameLoad NameLoad NameLoad NameLoad NameLoad NameLoad For NameStore AttributeLoad NameLoad attr body TryExcept body Expr Call AttributeLoad NameLoad attr NameLoad handlers ExceptHandler type NameLoad body Expr Call NameLoad decorator_list FunctionDef arguments args NameParam NameParam NameParam NameParam NameParam NameParam NameParam defaults NameLoad body Expr Str If CompareIs NameLoad NameLoad body Assign NameStore NameLoad Assign NameStore Call NameLoad NameLoad NameLoad NameLoad NameLoad NameLoad NameLoad For NameStore AttributeLoad NameLoad attr body TryExcept body Expr Call AttributeLoad NameLoad attr NameLoad handlers ExceptHandler type NameLoad body Expr Call NameLoad decorator_list decorator_list
N/A N/A Tools to monitor driver events.

Use :func:`register` to register global listeners for specific events.
Currently only command events are published. Listeners must be
a subclass of :class:`CommandListener` and implement
:meth:`~CommandListener.started`, :meth:`~CommandListener.succeeded`, and
:meth:`~CommandListener.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
:class:`~pymongo.mongo_client.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. :meth:`~CommandListener.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.
 N/A sys N/A traceback collections namedtuple Sequence N/A _Listeners N/A namedtuple Listeners N/A command_listeners N/A _LISTENERS N/A _Listeners N/A CommandListener N/A object N/A N/A Abstract base class for command listeners. started N/A N/A self event N/A N/A N/A Abstract method to handle CommandStartedEvent.

        :Parameters:
          - `event`: An instance of :class:`CommandStartedEvent`
         N/A NotImplementedError N/A succeeded N/A N/A self event N/A N/A N/A Abstract method to handle CommandSucceededEvent.

        :Parameters:
          - `event`: An instance of :class:`CommandSucceededEvent`
         N/A NotImplementedError N/A failed N/A N/A self event N/A N/A N/A Abstract method to handle CommandFailedEvent.

        :Parameters:
          - `event`: An instance of :class:`CommandFailedEvent`
         N/A NotImplementedError N/A N/A _to_micros N/A N/A dur N/A N/A N/A Convert duration 'dur' to microseconds. N/A N/A hasattr dur total_seconds N/A N/A N/A int N/A N/A N/A dur total_seconds 1000000.0 N/A N/A N/A dur microseconds N/A N/A N/A dur seconds N/A N/A N/A dur days 24 3600 1000000 N/A _validate_event_listeners N/A N/A option listeners N/A N/A N/A Validate event listeners N/A N/A N/A isinstance listeners Sequence N/A N/A N/A TypeError N/A %s must be a list or tuple N/A option N/A listener listeners N/A N/A N/A N/A isinstance listener CommandListener N/A N/A N/A TypeError Only subclasses of pymongo.monitoring.CommandListener are supported N/A listeners N/A register N/A N/A listener N/A N/A N/A Register a global event listener.

    :Parameters:
      - `listener`: A subclass of :class:`CommandListener`.
     N/A N/A _validate_event_listeners listener N/A listener N/A N/A N/A N/A _LISTENERS command_listeners append listener N/A _handle_exception N/A N/A N/A N/A N/A Print exceptions raised by subscribers to stderr. N/A N/A sys stderr N/A N/A einfo N/A N/A sys exc_info N/A N/A N/A N/A N/A N/A N/A traceback print_exception N/A einfo N/A 0 N/A einfo N/A 1 N/A einfo N/A 2 None N/A sys stderr N/A N/A N/A IOError N/A N/A N/A N/A einfo N/A N/A _SENSITIVE_COMMANDS N/A set N/A authenticate saslstart saslcontinue getnonce createuser updateuser copydbgetnonce copydbsaslstart copydb _CommandEvent N/A object N/A N/A Base class for command events. N/A __slots__ N/A __cmd_name __rqst_id __conn_id __op_id __init__ N/A N/A self command_name request_id connection_id operation_id N/A N/A N/A N/A self __cmd_name command_name N/A N/A self __rqst_id request_id N/A N/A self __conn_id connection_id N/A N/A self __op_id operation_id N/A command_name N/A N/A self N/A N/A N/A The command name. N/A N/A self __cmd_name N/A property request_id N/A N/A self N/A N/A N/A The request id for this operation. N/A N/A self __rqst_id N/A property connection_id N/A N/A self N/A N/A N/A The address (host, port) of the server this command was sent to. N/A N/A self __conn_id N/A property operation_id N/A N/A self N/A N/A N/A An id for this series of events or None. N/A N/A self __op_id N/A property N/A CommandStartedEvent N/A _CommandEvent N/A N/A 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.
     N/A __slots__ N/A __cmd __db __init__ N/A N/A self command database_name N/A args N/A N/A N/A command N/A N/A N/A ValueError N/A %r is not a valid command N/A command N/A command_name N/A next N/A iter command N/A N/A N/A N/A super CommandStartedEvent self __init__ command_name args N/A N/A N/A N/A command_name lower _SENSITIVE_COMMANDS N/A N/A N/A self __cmd N/A N/A N/A N/A self __cmd command N/A N/A self __db database_name N/A command N/A N/A self N/A N/A N/A The command document. N/A N/A self __cmd N/A property database_name N/A N/A self N/A N/A N/A The name of the database this command was run against. N/A N/A self __db N/A property N/A CommandSucceededEvent N/A _CommandEvent N/A N/A 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.
     N/A __slots__ N/A __duration_micros __reply __init__ N/A N/A self duration reply command_name request_id connection_id operation_id N/A N/A N/A N/A N/A N/A super CommandSucceededEvent self __init__ command_name request_id connection_id operation_id N/A N/A self __duration_micros N/A _to_micros duration N/A N/A N/A N/A command_name lower _SENSITIVE_COMMANDS N/A N/A N/A self __reply N/A N/A N/A N/A self __reply reply N/A duration_micros N/A N/A self N/A N/A N/A The duration of this operation in microseconds. N/A N/A self __duration_micros N/A property reply N/A N/A self N/A N/A N/A The server failure document for this operation. N/A N/A self __reply N/A property N/A CommandFailedEvent N/A _CommandEvent N/A N/A 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.
     N/A __slots__ N/A __duration_micros __failure __init__ N/A N/A self duration failure N/A args N/A N/A N/A N/A N/A super CommandFailedEvent self __init__ args N/A N/A self __duration_micros N/A _to_micros duration N/A N/A self __failure failure N/A duration_micros N/A N/A self N/A N/A N/A The duration of this operation in microseconds. N/A N/A self __duration_micros N/A property failure N/A N/A self N/A N/A N/A The server failure document for this operation. N/A N/A self __failure N/A property N/A _EventListeners N/A object N/A N/A Configure event listeners for a client instance.

    Any event listeners registered globally are included by default.

    :Parameters:
      - `listeners`: A list of event listeners.
     __init__ N/A N/A self listeners N/A N/A N/A N/A self __command_listeners N/A N/A _LISTENERS command_listeners N/A N/A N/A listeners None N/A N/A N/A N/A N/A self __command_listeners extend listeners N/A N/A self __enabled_for_commands N/A bool N/A self __command_listeners N/A enabled_for_commands N/A N/A self N/A N/A N/A Are any CommandListener instances registered? N/A N/A self __enabled_for_commands N/A property event_listeners N/A N/A self N/A N/A N/A List of registered event listeners. N/A N/A N/A self __command_listeners N/A N/A property publish_command_start N/A N/A self command database_name request_id connection_id op_id N/A None N/A N/A Publish a CommandStartedEvent to all command listeners.

        :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.
          - `op_id`: The (optional) operation id for this operation.
         N/A N/A op_id None N/A N/A op_id request_id N/A event N/A CommandStartedEvent command database_name request_id connection_id op_id N/A subscriber N/A self __command_listeners N/A N/A N/A N/A N/A N/A subscriber started event N/A N/A N/A Exception N/A N/A N/A _handle_exception N/A publish_command_success N/A N/A self duration reply command_name request_id connection_id op_id N/A None N/A N/A Publish a CommandSucceededEvent to all command listeners.

        :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.
          - `op_id`: The (optional) operation id for this operation.
         N/A N/A op_id None N/A N/A op_id request_id N/A event N/A CommandSucceededEvent duration reply command_name request_id connection_id op_id N/A subscriber N/A self __command_listeners N/A N/A N/A N/A N/A N/A subscriber succeeded event N/A N/A N/A Exception N/A N/A N/A _handle_exception N/A publish_command_failure N/A N/A self duration failure command_name request_id connection_id op_id N/A None N/A N/A Publish a CommandFailedEvent to all command listeners.

        :Parameters:
          - `duration`: The command duration as a datetime.timedelta.
          - `failure`: The server reply document or failure description
            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.
          - `op_id`: The (optional) operation id for this operation.
         N/A N/A op_id None N/A N/A op_id request_id N/A event N/A CommandFailedEvent duration failure command_name request_id connection_id op_id N/A subscriber N/A self __command_listeners N/A N/A N/A N/A N/A N/A subscriber failed event N/A N/A N/A Exception N/A N/A N/A _handle_exception N/A N/A