monitoring – Tools for monitoring driver events.

Tools to monitor driver events.

New in version 3.1.

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

For example, a simple command logger might be implemented like this:

  1. import logging
  2.  
  3. from pymongo import monitoring
  4.  
  5. class CommandLogger(monitoring.CommandListener):
  6.  
  7. def started(self, event):
  8. logging.info("Command {0.command_name} with request id "
  9. "{0.request_id} started on server "
  10. "{0.connection_id}".format(event))
  11.  
  12. def succeeded(self, event):
  13. logging.info("Command {0.command_name} with request id "
  14. "{0.request_id} on server {0.connection_id} "
  15. "succeeded in {0.duration_micros} "
  16. "microseconds".format(event))
  17.  
  18. def failed(self, event):
  19. logging.info("Command {0.command_name} with request id "
  20. "{0.request_id} on server {0.connection_id} "
  21. "failed in {0.duration_micros} "
  22. "microseconds".format(event))
  23.  
  24. monitoring.register(CommandLogger())

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

  1. class ServerLogger(monitoring.ServerListener):
  2.  
  3. def opened(self, event):
  4. logging.info("Server {0.server_address} added to topology "
  5. "{0.topology_id}".format(event))
  6.  
  7. def description_changed(self, event):
  8. previous_server_type = event.previous_description.server_type
  9. new_server_type = event.new_description.server_type
  10. if new_server_type != previous_server_type:
  11. # server_type_name was added in PyMongo 3.4
  12. logging.info(
  13. "Server {0.server_address} changed type from "
  14. "{0.previous_description.server_type_name} to "
  15. "{0.new_description.server_type_name}".format(event))
  16.  
  17. def closed(self, event):
  18. logging.warning("Server {0.server_address} removed from topology "
  19. "{0.topology_id}".format(event))
  20.  
  21.  
  22. class HeartbeatLogger(monitoring.ServerHeartbeatListener):
  23.  
  24. def started(self, event):
  25. logging.info("Heartbeat sent to server "
  26. "{0.connection_id}".format(event))
  27.  
  28. def succeeded(self, event):
  29. # The reply.document attribute was added in PyMongo 3.4.
  30. logging.info("Heartbeat to server {0.connection_id} "
  31. "succeeded with reply "
  32. "{0.reply.document}".format(event))
  33.  
  34. def failed(self, event):
  35. logging.warning("Heartbeat to server {0.connection_id} "
  36. "failed with error {0.reply}".format(event))
  37.  
  38. class TopologyLogger(monitoring.TopologyListener):
  39.  
  40. def opened(self, event):
  41. logging.info("Topology with id {0.topology_id} "
  42. "opened".format(event))
  43.  
  44. def description_changed(self, event):
  45. logging.info("Topology description updated for "
  46. "topology id {0.topology_id}".format(event))
  47. previous_topology_type = event.previous_description.topology_type
  48. new_topology_type = event.new_description.topology_type
  49. if new_topology_type != previous_topology_type:
  50. # topology_type_name was added in PyMongo 3.4
  51. logging.info(
  52. "Topology {0.topology_id} changed type from "
  53. "{0.previous_description.topology_type_name} to "
  54. "{0.new_description.topology_type_name}".format(event))
  55. # The has_writable_server and has_readable_server methods
  56. # were added in PyMongo 3.4.
  57. if not event.new_description.has_writable_server():
  58. logging.warning("No writable servers available.")
  59. if not event.new_description.has_readable_server():
  60. logging.warning("No readable servers available.")
  61.  
  62. def closed(self, event):
  63. logging.info("Topology with id {0.topology_id} "
  64. "closed".format(event))

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

  1. class ConnectionPoolLogger(ConnectionPoolListener):
  2.  
  3. def pool_created(self, event):
  4. logging.info("[pool {0.address}] pool created".format(event))
  5.  
  6. def pool_cleared(self, event):
  7. logging.info("[pool {0.address}] pool cleared".format(event))
  8.  
  9. def pool_closed(self, event):
  10. logging.info("[pool {0.address}] pool closed".format(event))
  11.  
  12. def connection_created(self, event):
  13. logging.info("[pool {0.address}][conn #{0.connection_id}] "
  14. "connection created".format(event))
  15.  
  16. def connection_ready(self, event):
  17. logging.info("[pool {0.address}][conn #{0.connection_id}] "
  18. "connection setup succeeded".format(event))
  19.  
  20. def connection_closed(self, event):
  21. logging.info("[pool {0.address}][conn #{0.connection_id}] "
  22. "connection closed, reason: "
  23. "{0.reason}".format(event))
  24.  
  25. def connection_check_out_started(self, event):
  26. logging.info("[pool {0.address}] connection check out "
  27. "started".format(event))
  28.  
  29. def connection_check_out_failed(self, event):
  30. logging.info("[pool {0.address}] connection check out "
  31. "failed, reason: {0.reason}".format(event))
  32.  
  33. def connection_checked_out(self, event):
  34. logging.info("[pool {0.address}][conn #{0.connection_id}] "
  35. "connection checked out of pool".format(event))
  36.  
  37. def connection_checked_in(self, event):
  38. logging.info("[pool {0.address}][conn #{0.connection_id}] "
  39. "connection checked into pool".format(event))

Event listeners can also be registered per instance ofMongoClient:

  1. client = MongoClient(event_listeners=[CommandLogger()])

Note that previously registered global listeners are automatically includedwhen configuring per client event listeners. Registering a new global listenerwill not add that listener to existing client instances.

Note

Events are delivered synchronously. Application threads blockwaiting for event handlers (e.g. started()) toreturn. Care must be taken to ensure that your event handlers are efficientenough 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 eventhandler first.

  • pymongo.monitoring.register(listener)
  • Register a global event listener.

Parameters:

  • class pymongo.monitoring.CommandListener
  • Abstract base class for command listeners.

Handles CommandStartedEvent, CommandSucceededEvent,and CommandFailedEvent.

  • failed(event)
  • Abstract method to handle a CommandFailedEvent.

Parameters:

  1. - _event_: An instance of [<code>CommandFailedEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.CommandFailedEvent).
  • started(event)
  • Abstract method to handle a CommandStartedEvent.

Parameters:

  1. - _event_: An instance of [<code>CommandStartedEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.CommandStartedEvent).
  • succeeded(event)
  • Abstract method to handle a CommandSucceededEvent.

Parameters:

  1. - _event_: An instance of [<code>CommandSucceededEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.CommandSucceededEvent).
  • class pymongo.monitoring.ServerListener
  • Abstract base class for server listeners.Handles ServerOpeningEvent, ServerDescriptionChangedEvent, andServerClosedEvent.

New in version 3.3.

  • closed(event)
  • Abstract method to handle a ServerClosedEvent.

Parameters:

  1. - _event_: An instance of [<code>ServerClosedEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.ServerClosedEvent).
  • descriptionchanged(_event)
  • Abstract method to handle a ServerDescriptionChangedEvent.

Parameters:

  1. - _event_: An instance of [<code>ServerDescriptionChangedEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.ServerDescriptionChangedEvent).
  • opened(event)
  • Abstract method to handle a ServerOpeningEvent.

Parameters:

  1. - _event_: An instance of [<code>ServerOpeningEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.ServerOpeningEvent).
  • class pymongo.monitoring.ServerHeartbeatListener
  • Abstract base class for server heartbeat listeners.

Handles ServerHeartbeatStartedEvent, ServerHeartbeatSucceededEvent,and ServerHeartbeatFailedEvent.

New in version 3.3.

  • failed(event)
  • Abstract method to handle a ServerHeartbeatFailedEvent.

Parameters:

  1. - _event_: An instance of [<code>ServerHeartbeatFailedEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.ServerHeartbeatFailedEvent).
  • started(event)
  • Abstract method to handle a ServerHeartbeatStartedEvent.

Parameters:

  1. - _event_: An instance of [<code>ServerHeartbeatStartedEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.ServerHeartbeatStartedEvent).
  • succeeded(event)
  • Abstract method to handle a ServerHeartbeatSucceededEvent.

Parameters:

  1. - _event_: An instance of [<code>ServerHeartbeatSucceededEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.ServerHeartbeatSucceededEvent).
  • class pymongo.monitoring.TopologyListener
  • Abstract base class for topology monitoring listeners.Handles TopologyOpenedEvent, TopologyDescriptionChangedEvent, andTopologyClosedEvent.

New in version 3.3.

  • closed(event)
  • Abstract method to handle a TopologyClosedEvent.

Parameters:

  1. - _event_: An instance of [<code>TopologyClosedEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.TopologyClosedEvent).
  • descriptionchanged(_event)
  • Abstract method to handle a TopologyDescriptionChangedEvent.

Parameters:

  1. - _event_: An instance of [<code>TopologyDescriptionChangedEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.TopologyDescriptionChangedEvent).
  • opened(event)
  • Abstract method to handle a TopologyOpenedEvent.

Parameters:

  1. - _event_: An instance of [<code>TopologyOpenedEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.TopologyOpenedEvent).
  • class pymongo.monitoring.ConnectionPoolListener
  • Abstract base class for connection pool listeners.

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

New in version 3.9.

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

Parameters:

  1. - _event_: An instance of [<code>ConnectionCheckOutFailedEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.ConnectionCheckOutFailedEvent).

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

Parameters:

  1. - _event_: An instance of [<code>ConnectionCheckOutStartedEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.ConnectionCheckOutStartedEvent).

Emitted when the driver checks in a Connection back to the ConnectionPool.

Parameters:

  1. - _event_: An instance of [<code>ConnectionCheckedInEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.ConnectionCheckedInEvent).

Emitted when the driver successfully checks out a Connection.

Parameters:

  1. - _event_: An instance of [<code>ConnectionCheckedOutEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.ConnectionCheckedOutEvent).

Emitted when a Connection Pool closes a Connection.

Parameters:

  1. - _event_: An instance of [<code>ConnectionClosedEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.ConnectionClosedEvent).

Emitted when a Connection Pool creates a Connection object.

Parameters:

  1. - _event_: An instance of [<code>ConnectionCreatedEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.ConnectionCreatedEvent).

Emitted when a Connection has finished its setup, and is now ready touse.

Parameters:

  1. - _event_: An instance of [<code>ConnectionReadyEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.ConnectionReadyEvent).
  • poolcleared(_event)
  • Abstract method to handle a PoolClearedEvent.

Emitted when a Connection Pool is cleared.

Parameters:

  1. - _event_: An instance of [<code>PoolClearedEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.PoolClearedEvent).
  • poolclosed(_event)
  • Abstract method to handle a PoolClosedEvent.

Emitted when a Connection Pool is closed.

Parameters:

  1. - _event_: An instance of [<code>PoolClosedEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.PoolClosedEvent).

Emitted when a Connection Pool is created.

Parameters:

  1. - _event_: An instance of [<code>PoolCreatedEvent</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.monitoring.PoolCreatedEvent).
  • 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 commandwas 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 commandwas 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 commandwas 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.
  • class pymongo.monitoring.ServerDescriptionChangedEvent(previous_description, new_description, *args)
  • Published when server description changes.

New in version 3.3.

  • new_description
  • The newServerDescription.

  • previous_description

  • The previousServerDescription.

  • server_address

  • The address (host, port) pair of the server

  • topology_id

  • 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.

New in version 3.3.

  • server_address
  • The address (host, port) pair of the server

  • topology_id

  • 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.

New in version 3.3.

  • server_address
  • The address (host, port) pair of the server

  • topology_id

  • 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.

New in version 3.3.

  • class pymongo.monitoring.TopologyOpenedEvent(topology_id)
  • Published when the topology is initialized.

New in version 3.3.

  • topology_id
  • A unique identifier for the topology this server is a part of.
  • class pymongo.monitoring.TopologyClosedEvent(topology_id)
  • Published when the topology is closed.

New in version 3.3.

  • topology_id
  • A unique identifier for the topology this server is a part of.
  • class pymongo.monitoring.ServerHeartbeatStartedEvent(connection_id)
  • Published when a heartbeat is started.

New in version 3.3.

  • connection_id
  • The address (host, port) of the server this heartbeat was sentto.
  • class pymongo.monitoring.ServerHeartbeatSucceededEvent(duration, reply, *args)
  • Fired when the server heartbeat succeeds.

New in version 3.3.

  • connection_id
  • The address (host, port) of the server this heartbeat was sentto.

  • duration

  • The duration of this heartbeat in microseconds.

  • reply

  • An instance of IsMaster.
  • class pymongo.monitoring.ServerHeartbeatFailedEvent(duration, reply, *args)
  • Fired when the server heartbeat fails, either with an “ok: 0”or a socket exception.

New in version 3.3.

  • connection_id
  • The address (host, port) of the server this heartbeat was sentto.

  • duration

  • The duration of this heartbeat in microseconds.

  • reply

  • A subclass of Exception.
  • class pymongo.monitoring.PoolCreatedEvent(address, options)
  • Published when a Connection Pool is created.

Parameters:

  • address: The address (host, port) pair of the server this Pool isattempting to connect to.

New in version 3.9.

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

  • options

  • Any non-default pool options that were set on this Connection Pool.
  • class pymongo.monitoring.PoolClearedEvent(address)
  • Published when a Connection Pool is cleared.

Parameters:

  • address: The address (host, port) pair of the server this Pool isattempting to connect to.

New in version 3.9.

  • address
  • The address (host, port) pair of the server the pool is attemptingto connect to.
  • class pymongo.monitoring.PoolClosedEvent(address)
  • Published when a Connection Pool is closed.

Parameters:

  • address: The address (host, port) pair of the server this Pool isattempting to connect to.

New in version 3.9.

  • address
  • The address (host, port) pair of the server the pool is attemptingto 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:

  • address: The address (host, port) pair of the server thisConnection is attempting to connect to.
  • connection_id: The integer ID of the Connection in this Pool.

New in version 3.9.

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

  • connection_id

  • The ID of the Connection.
  • class pymongo.monitoring.ConnectionReadyEvent(address, connection_id)
  • Published when a Connection has finished its setup, and is ready to use.

Parameters:

  • address: The address (host, port) pair of the server thisConnection is attempting to connect to.
  • connection_id: The integer ID of the Connection in this Pool.

New in version 3.9.

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

  • connection_id

  • The ID of the Connection.
  • class pymongo.monitoring.ConnectionClosedReason
  • An enum that defines values for reason on aConnectionClosedEvent.

New 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).

  • POOLCLOSED = '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:

  • address: The address (host, port) pair of the server thisConnection is attempting to connect to.
  • connection_id: The integer ID of the Connection in this Pool.
  • reason: A reason explaining why this connection was closed.

New in version 3.9.

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

  • connection_id

  • The ID of the Connection.

  • reason

  • 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: The address (host, port) pair of the server thisConnection is attempting to connect to.

New in version 3.9.

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

New in version 3.9.

  • CONNERROR = 'connectionError'_
  • The connection check out attempt experienced an error while setting upa new connection.

  • POOLCLOSED = '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)
  • Published when the driver’s attempt to check out a connection fails.

Parameters:

  • address: The address (host, port) pair of the server thisConnection is attempting to connect to.
  • reason: A reason explaining why connection check out failed.

New in version 3.9.

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

  • reason

  • 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)
  • Published when the driver successfully checks out a Connection.

Parameters:

  • address: The address (host, port) pair of the server thisConnection is attempting to connect to.
  • connection_id: The integer ID of the Connection in this Pool.

New in version 3.9.

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

  • connection_id

  • The ID of the Connection.
  • class pymongo.monitoring.ConnectionCheckedInEvent(address, connection_id)
  • Published when the driver checks in a Connection into the Pool.

Parameters:

  • address: The address (host, port) pair of the server thisConnection is attempting to connect to.
  • connection_id: The integer ID of the Connection in this Pool.

New in version 3.9.

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

  • connection_id

  • The ID of the Connection.