monitoring – Tools for monitoring driver events. (original) (raw)

Tools to monitor driver events.

Added in version 3.1.

Attention

Starting in PyMongo 3.11, the monitoring classes outlined below are included in the PyMongo distribution under theevent_loggers submodule.

See also

This module is compatible with both the synchronous and asynchronous PyMongo APIs.

Use register() to register global listeners for specific events. Listeners must inherit from one of the abstract classes below and implement the correct functions for that class.

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())

Server discovery and monitoring events are also available. For example:

class ServerLogger(monitoring.ServerListener):

def opened(self, event):
    logging.info("Server {0.server_address} added to topology "
                 "{0.topology_id}".format(event))

def description_changed(self, event):
    previous_server_type = event.previous_description.server_type
    new_server_type = event.new_description.server_type
    if new_server_type != previous_server_type:
        # server_type_name was added in PyMongo 3.4
        logging.info(
            "Server {0.server_address} changed type from "
            "{0.previous_description.server_type_name} to "
            "{0.new_description.server_type_name}".format(event))

def closed(self, event):
    logging.warning("Server {0.server_address} removed from topology "
                    "{0.topology_id}".format(event))

class HeartbeatLogger(monitoring.ServerHeartbeatListener):

def started(self, event):
    logging.info("Heartbeat sent to server "
                 "{0.connection_id}".format(event))

def succeeded(self, event):
    # The reply.document attribute was added in PyMongo 3.4.
    logging.info("Heartbeat to server {0.connection_id} "
                 "succeeded with reply "
                 "{0.reply.document}".format(event))

def failed(self, event):
    logging.warning("Heartbeat to server {0.connection_id} "
                    "failed with error {0.reply}".format(event))

class TopologyLogger(monitoring.TopologyListener):

def opened(self, event):
    logging.info("Topology with id {0.topology_id} "
                 "opened".format(event))

def description_changed(self, event):
    logging.info("Topology description updated for "
                 "topology id {0.topology_id}".format(event))
    previous_topology_type = event.previous_description.topology_type
    new_topology_type = event.new_description.topology_type
    if new_topology_type != previous_topology_type:
        # topology_type_name was added in PyMongo 3.4
        logging.info(
            "Topology {0.topology_id} changed type from "
            "{0.previous_description.topology_type_name} to "
            "{0.new_description.topology_type_name}".format(event))
    # The has_writable_server and has_readable_server methods
    # were added in PyMongo 3.4.
    if not event.new_description.has_writable_server():
        logging.warning("No writable servers available.")
    if not event.new_description.has_readable_server():
        logging.warning("No readable servers available.")

def closed(self, event):
    logging.info("Topology with id {0.topology_id} "
                 "closed".format(event))

Connection monitoring and pooling events are also available. For example:

class ConnectionPoolLogger(ConnectionPoolListener):

def pool_created(self, event):
    logging.info("[pool {0.address}] pool created".format(event))

def pool_ready(self, event):
    logging.info("[pool {0.address}] pool is ready".format(event))

def pool_cleared(self, event):
    logging.info("[pool {0.address}] pool cleared".format(event))

def pool_closed(self, event):
    logging.info("[pool {0.address}] pool closed".format(event))

def connection_created(self, event):
    logging.info("[pool {0.address}][connection #{0.connection_id}] "
                 "connection created".format(event))

def connection_ready(self, event):
    logging.info("[pool {0.address}][connection #{0.connection_id}] "
                 "connection setup succeeded".format(event))

def connection_closed(self, event):
    logging.info("[pool {0.address}][connection #{0.connection_id}] "
                 "connection closed, reason: "
                 "{0.reason}".format(event))

def connection_check_out_started(self, event):
    logging.info("[pool {0.address}] connection check out "
                 "started".format(event))

def connection_check_out_failed(self, event):
    logging.info("[pool {0.address}] connection check out "
                 "failed, reason: {0.reason}".format(event))

def connection_checked_out(self, event):
    logging.info("[pool {0.address}][connection #{0.connection_id}] "
                 "connection checked out of pool".format(event))

def connection_checked_in(self, event):
    logging.info("[pool {0.address}][connection #{0.connection_id}] "
                 "connection checked into pool".format(event))

Event listeners can also be registered per instance ofMongoClient:

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 (_EventListener) – A subclasses of CommandListener,ServerHeartbeatListener, ServerListener,TopologyListener, or ConnectionPoolListener.

Return type:

None

class pymongo.monitoring.CommandListener

Abstract base class for command listeners.

Handles CommandStartedEvent, CommandSucceededEvent, and CommandFailedEvent.

failed(event)

Abstract method to handle a CommandFailedEvent.

Parameters:

event (CommandFailedEvent) – An instance of CommandFailedEvent.

Return type:

None

started(event)

Abstract method to handle a CommandStartedEvent.

Parameters:

event (CommandStartedEvent) – An instance of CommandStartedEvent.

Return type:

None

succeeded(event)

Abstract method to handle a CommandSucceededEvent.

Parameters:

event (CommandSucceededEvent) – An instance of CommandSucceededEvent.

Return type:

None

class pymongo.monitoring.ServerListener

Abstract base class for server listeners. Handles ServerOpeningEvent, ServerDescriptionChangedEvent, andServerClosedEvent.

Added in version 3.3.

closed(event)

Abstract method to handle a ServerClosedEvent.

Parameters:

event (ServerClosedEvent) – An instance of ServerClosedEvent.

Return type:

None

description_changed(event)

Abstract method to handle a ServerDescriptionChangedEvent.

Parameters:

event (ServerDescriptionChangedEvent) – An instance of ServerDescriptionChangedEvent.

Return type:

None

opened(event)

Abstract method to handle a ServerOpeningEvent.

Parameters:

event (ServerOpeningEvent) – An instance of ServerOpeningEvent.

Return type:

None

class pymongo.monitoring.ServerHeartbeatListener

Abstract base class for server heartbeat listeners.

Handles ServerHeartbeatStartedEvent, ServerHeartbeatSucceededEvent, and ServerHeartbeatFailedEvent.

Added in version 3.3.

failed(event)

Abstract method to handle a ServerHeartbeatFailedEvent.

Parameters:

event (ServerHeartbeatFailedEvent) – An instance of ServerHeartbeatFailedEvent.

Return type:

None

started(event)

Abstract method to handle a ServerHeartbeatStartedEvent.

Parameters:

event (ServerHeartbeatStartedEvent) – An instance of ServerHeartbeatStartedEvent.

Return type:

None

succeeded(event)

Abstract method to handle a ServerHeartbeatSucceededEvent.

Parameters:

event (ServerHeartbeatSucceededEvent) – An instance of ServerHeartbeatSucceededEvent.

Return type:

None

class pymongo.monitoring.TopologyListener

Abstract base class for topology monitoring listeners. Handles TopologyOpenedEvent, TopologyDescriptionChangedEvent, andTopologyClosedEvent.

Added in version 3.3.

closed(event)

Abstract method to handle a TopologyClosedEvent.

Parameters:

event (TopologyClosedEvent) – An instance of TopologyClosedEvent.

Return type:

None

description_changed(event)

Abstract method to handle a TopologyDescriptionChangedEvent.

Parameters:

event (TopologyDescriptionChangedEvent) – An instance of TopologyDescriptionChangedEvent.

Return type:

None

opened(event)

Abstract method to handle a TopologyOpenedEvent.

Parameters:

event (TopologyOpenedEvent) – An instance of TopologyOpenedEvent.

Return type:

None

class pymongo.monitoring.ConnectionPoolListener

Abstract base class for connection pool listeners.

Handles all of the connection pool events defined in the Connection Monitoring and Pooling Specification:PoolCreatedEvent, PoolClearedEvent,PoolClosedEvent, ConnectionCreatedEvent,ConnectionReadyEvent, ConnectionClosedEvent,ConnectionCheckOutStartedEvent,ConnectionCheckOutFailedEvent,ConnectionCheckedOutEvent, and ConnectionCheckedInEvent.

Added in version 3.9.

connection_check_out_failed(event)

Abstract method to handle a ConnectionCheckOutFailedEvent.

Emitted when the driver’s attempt to check out a connection fails.

Parameters:

event (ConnectionCheckOutFailedEvent) – An instance of ConnectionCheckOutFailedEvent.

Return type:

None

connection_check_out_started(event)

Abstract method to handle a ConnectionCheckOutStartedEvent.

Emitted when the driver starts attempting to check out a connection.

Parameters:

event (ConnectionCheckOutStartedEvent) – An instance of ConnectionCheckOutStartedEvent.

Return type:

None

connection_checked_in(event)

Abstract method to handle a ConnectionCheckedInEvent.

Emitted when the driver checks in a connection back to the connection Pool.

Parameters:

event (ConnectionCheckedInEvent) – An instance of ConnectionCheckedInEvent.

Return type:

None

connection_checked_out(event)

Abstract method to handle a ConnectionCheckedOutEvent.

Emitted when the driver successfully checks out a connection.

Parameters:

event (ConnectionCheckedOutEvent) – An instance of ConnectionCheckedOutEvent.

Return type:

None

connection_closed(event)

Abstract method to handle a ConnectionClosedEvent.

Emitted when a connection Pool closes a connection.

Parameters:

event (ConnectionClosedEvent) – An instance of ConnectionClosedEvent.

Return type:

None

connection_created(event)

Abstract method to handle a ConnectionCreatedEvent.

Emitted when a connection Pool creates a Connection object.

Parameters:

event (ConnectionCreatedEvent) – An instance of ConnectionCreatedEvent.

Return type:

None

connection_ready(event)

Abstract method to handle a ConnectionReadyEvent.

Emitted when a connection has finished its setup, and is now ready to use.

Parameters:

event (ConnectionReadyEvent) – An instance of ConnectionReadyEvent.

Return type:

None

pool_cleared(event)

Abstract method to handle a PoolClearedEvent.

Emitted when a connection Pool is cleared.

Parameters:

event (PoolClearedEvent) – An instance of PoolClearedEvent.

Return type:

None

pool_closed(event)

Abstract method to handle a PoolClosedEvent.

Emitted when a connection Pool is closed.

Parameters:

event (PoolClosedEvent) – An instance of PoolClosedEvent.

Return type:

None

pool_created(event)

Abstract method to handle a PoolCreatedEvent.

Emitted when a connection Pool is created.

Parameters:

event (PoolCreatedEvent) – An instance of PoolCreatedEvent.

Return type:

None

pool_ready(event)

Abstract method to handle a PoolReadyEvent.

Emitted when a connection Pool is marked ready.

Parameters:

event (PoolReadyEvent) – An instance of PoolReadyEvent.

Return type:

None

Added in version 4.0.

class pymongo.monitoring.CommandStartedEvent(command, database_name, request_id, connection_id, operation_id, service_id=None, server_connection_id=None)

Event published when a command starts.

Parameters:

property command_: _DocumentOut_

The command document.

property command_name_: str_

The command name.

property connection_id_: Tuple[str, int | None]_

The address (host, port) of the server this command was sent to.

property database_name_: str_

The name of the database this command was run against.

property operation_id_: int | None_

An id for this series of events or None.

property request_id_: int_

The request id for this operation.

property server_connection_id_: int | None_

The server-side connection id for the connection this command was sent on, or None.

Added in version 4.7.

property service_id_: ObjectId | None_

The service_id this command was sent to, or None.

Added in version 3.12.

class pymongo.monitoring.CommandSucceededEvent(duration, reply, command_name, request_id, connection_id, operation_id, service_id=None, database_name='', server_connection_id=None)

Event published when a command succeeds.

Parameters:

property command_name_: str_

The command name.

property connection_id_: Tuple[str, int | None]_

The address (host, port) of the server this command was sent to.

property database_name_: str_

The database_name this command was sent to, or "".

Added in version 4.6.

property duration_micros_: int_

The duration of this operation in microseconds.

property operation_id_: int | None_

An id for this series of events or None.

property reply_: _DocumentOut_

The server failure document for this operation.

property request_id_: int_

The request id for this operation.

property server_connection_id_: int | None_

The server-side connection id for the connection this command was sent on, or None.

Added in version 4.7.

property service_id_: ObjectId | None_

The service_id this command was sent to, or None.

Added in version 3.12.

class pymongo.monitoring.CommandFailedEvent(duration, failure, command_name, request_id, connection_id, operation_id, service_id=None, database_name='', server_connection_id=None)

Event published when a command fails.

Parameters:

property command_name_: str_

The command name.

property connection_id_: Tuple[str, int | None]_

The address (host, port) of the server this command was sent to.

property database_name_: str_

The database_name this command was sent to, or "".

Added in version 4.6.

property duration_micros_: int_

The duration of this operation in microseconds.

property failure_: _DocumentOut_

The server failure document for this operation.

property operation_id_: int | None_

An id for this series of events or None.

property request_id_: int_

The request id for this operation.

property server_connection_id_: int | None_

The server-side connection id for the connection this command was sent on, or None.

Added in version 4.7.

property service_id_: ObjectId | None_

The service_id this command was sent to, or None.

Added in version 3.12.

class pymongo.monitoring.ServerDescriptionChangedEvent(previous_description, new_description, *args)

Published when server description changes.

Added in version 3.3.

Parameters:

property new_description_: ServerDescription_

The newServerDescription.

property previous_description_: ServerDescription_

The previousServerDescription.

property server_address_: Tuple[str, int | None]_

The address (host, port) pair of the server

property topology_id_: ObjectId_

A unique identifier for the topology this server is a part of.

class pymongo.monitoring.ServerOpeningEvent(server_address, topology_id)

Published when server is initialized.

Added in version 3.3.

Parameters:

property server_address_: Tuple[str, int | None]_

The address (host, port) pair of the server

property topology_id_: ObjectId_

A unique identifier for the topology this server is a part of.

class pymongo.monitoring.ServerClosedEvent(server_address, topology_id)

Published when server is closed.

Added in version 3.3.

Parameters:

property server_address_: Tuple[str, int | None]_

The address (host, port) pair of the server

property topology_id_: ObjectId_

A unique identifier for the topology this server is a part of.

class pymongo.monitoring.TopologyDescriptionChangedEvent(previous_description, new_description, *args)

Published when the topology description changes.

Added in version 3.3.

Parameters:

property new_description_: TopologyDescription_

The newTopologyDescription.

property previous_description_: TopologyDescription_

The previousTopologyDescription.

property topology_id_: ObjectId_

A unique identifier for the topology this server is a part of.

class pymongo.monitoring.TopologyOpenedEvent(topology_id)

Published when the topology is initialized.

Added in version 3.3.

Parameters:

topology_id (ObjectId)

property topology_id_: ObjectId_

A unique identifier for the topology this server is a part of.

class pymongo.monitoring.TopologyClosedEvent(topology_id)

Published when the topology is closed.

Added in version 3.3.

Parameters:

topology_id (ObjectId)

property topology_id_: ObjectId_

A unique identifier for the topology this server is a part of.

class pymongo.monitoring.ServerHeartbeatStartedEvent(connection_id, awaited=False)

Published when a heartbeat is started.

Added in version 3.3.

Parameters:

property awaited_: bool_

Whether the heartbeat was issued as an awaitable hello command.

Added in version 4.6.

property connection_id_: Tuple[str, int | None]_

The address (host, port) of the server this heartbeat was sent to.

class pymongo.monitoring.ServerHeartbeatSucceededEvent(duration, reply, connection_id, awaited=False)

Fired when the server heartbeat succeeds.

Added in version 3.3.

Parameters:

property awaited_: bool_

Whether the heartbeat was awaited.

If true, then duration() reflects the sum of the round trip time to the server and the time that the server waited before sending a response.

Added in version 3.11.

property connection_id_: Tuple[str, int | None]_

The address (host, port) of the server this heartbeat was sent to.

property duration_: float_

The duration of this heartbeat in microseconds.

property reply_: Hello_

An instance of Hello.

class pymongo.monitoring.ServerHeartbeatFailedEvent(duration, reply, connection_id, awaited=False)

Fired when the server heartbeat fails, either with an “ok: 0” or a socket exception.

Added in version 3.3.

Parameters:

property awaited_: bool_

Whether the heartbeat was awaited.

If true, then duration() reflects the sum of the round trip time to the server and the time that the server waited before sending a response.

Added in version 3.11.

property connection_id_: Tuple[str, int | None]_

The address (host, port) of the server this heartbeat was sent to.

property duration_: float_

The duration of this heartbeat in microseconds.

property reply_: Exception_

A subclass of Exception.

class pymongo.monitoring.PoolCreatedEvent(address, options)

Published when a Connection Pool is created.

Parameters:

Added in version 3.9.

property address_: Tuple[str, int | None]_

The address (host, port) pair of the server the pool is attempting to connect to.

property options_: dict[str, Any]_

Any non-default pool options that were set on this Connection Pool.

class pymongo.monitoring.PoolClearedEvent(address, service_id=None, interrupt_connections=False)

Published when a Connection Pool is cleared.

Parameters:

Added in version 3.9.

property address_: Tuple[str, int | None]_

The address (host, port) pair of the server the pool is attempting to connect to.

property interrupt_connections_: bool_

If True, active connections are interrupted during clearing.

Added in version 4.7.

property service_id_: ObjectId | None_

Connections with this service_id are cleared.

When service_id is None, all connections in the pool are cleared.

Added in version 3.12.

class pymongo.monitoring.PoolClosedEvent(address)

Published when a Connection Pool is closed.

Parameters:

address (_Address) – The address (host, port) pair of the server this Pool is attempting to connect to.

Added in version 3.9.

property address_: Tuple[str, int | None]_

The address (host, port) pair of the server the pool is attempting to connect to.

class pymongo.monitoring.ConnectionCreatedEvent(address, connection_id)

Published when a Connection Pool creates a Connection object.

NOTE: This connection is not ready for use until theConnectionReadyEvent is published.

Parameters:

Added in version 3.9.

property address_: Tuple[str, int | None]_

The address (host, port) pair of the server this connection is attempting to connect to.

property connection_id_: int_

The ID of the connection.

class pymongo.monitoring.ConnectionReadyEvent(address, connection_id, duration)

Published when a Connection has finished its setup, and is ready to use.

Parameters:

Added in version 3.9.

property address_: Tuple[str, int | None]_

The address (host, port) pair of the server this connection is attempting to connect to.

property connection_id_: int_

The ID of the connection.

property duration_: float | None_

The duration of the connection event.

Added in version 4.7.

class pymongo.monitoring.ConnectionClosedReason

An enum that defines values for reason on aConnectionClosedEvent.

Added in version 3.9.

ERROR = 'error'

The connection experienced an error, making it no longer valid.

IDLE = 'idle'

The connection became stale by being idle for too long (maxIdleTimeMS).

POOL_CLOSED = 'poolClosed'

The pool was closed, making the connection no longer valid.

STALE = 'stale'

The pool was cleared, making the connection no longer valid.

class pymongo.monitoring.ConnectionClosedEvent(address, connection_id, reason)

Published when a Connection is closed.

Parameters:

Added in version 3.9.

property address_: Tuple[str, int | None]_

The address (host, port) pair of the server this connection is attempting to connect to.

property connection_id_: int_

The ID of the connection.

property reason_: str_

A reason explaining why this connection was closed.

The reason must be one of the strings from theConnectionClosedReason enum.

class pymongo.monitoring.ConnectionCheckOutStartedEvent(address)

Published when the driver starts attempting to check out a connection.

Parameters:

address (_Address) – The address (host, port) pair of the server this Connection is attempting to connect to.

Added in version 3.9.

property address_: Tuple[str, int | None]_

The address (host, port) pair of the server this connection is attempting to connect to.

class pymongo.monitoring.ConnectionCheckOutFailedReason

An enum that defines values for reason on aConnectionCheckOutFailedEvent.

Added in version 3.9.

CONN_ERROR = 'connectionError'

The connection check out attempt experienced an error while setting up a new connection.

POOL_CLOSED = 'poolClosed'

The pool was previously closed, and cannot provide new connections.

TIMEOUT = 'timeout'

The connection check out attempt exceeded the specified timeout.

class pymongo.monitoring.ConnectionCheckOutFailedEvent(address, reason, duration)

Published when the driver’s attempt to check out a connection fails.

Parameters:

Added in version 3.9.

property address_: Tuple[str, int | None]_

The address (host, port) pair of the server this connection is attempting to connect to.

property connection_id_: int_

The ID of the connection.

property duration_: float | None_

The duration of the connection event.

Added in version 4.7.

property reason_: str_

A reason explaining why connection check out failed.

The reason must be one of the strings from theConnectionCheckOutFailedReason enum.

class pymongo.monitoring.ConnectionCheckedOutEvent(address, connection_id, duration)

Published when the driver successfully checks out a connection.

Parameters:

Added in version 3.9.

property address_: Tuple[str, int | None]_

The address (host, port) pair of the server this connection is attempting to connect to.

property connection_id_: int_

The ID of the connection.

property duration_: float | None_

The duration of the connection event.

Added in version 4.7.

class pymongo.monitoring.ConnectionCheckedInEvent(address, connection_id)

Published when the driver checks in a Connection into the Pool.

Parameters:

Added in version 3.9.

property address_: Tuple[str, int | None]_

The address (host, port) pair of the server this connection is attempting to connect to.

property connection_id_: int_

The ID of the connection.