Skip to content

Instantly share code, notes, and snippets.

@ajdavis
Last active January 15, 2019 07:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajdavis/86e1cb6dfcbf8b29fb44362cf48021cd to your computer and use it in GitHub Desktop.
Save ajdavis/86e1cb6dfcbf8b29fb44362cf48021cd to your computer and use it in GitHub Desktop.
Example of MongoDB driver event monitoring with Motor. See https://emptysqua.re/blog/motor-monitoring/
import logging
import sys
import threading
from tornado import ioloop, gen
from motor import MotorClient
from pymongo import monitoring
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
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())
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:
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))
monitoring.register(ServerLogger())
class HeartbeatLogger(monitoring.ServerHeartbeatListener):
def started(self, event):
logging.info("Heartbeat sent to server "
"{0.connection_id}".format(event))
def succeeded(self, event):
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))
monitoring.register(HeartbeatLogger())
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:
logging.info(
"Topology {0.topology_id} changed type from "
"{0.previous_description.topology_type_name} to "
"{0.new_description.topology_type_name}".format(event))
def closed(self, event):
logging.info("Topology with id {0.topology_id} "
"closed".format(event))
monitoring.register(TopologyLogger())
client = MotorClient()
async def do_insert():
await client.test.collection.insert({'message': 'hi!'})
# For this example, wait 10 seconds for more monitoring events to fire.
await gen.sleep(10)
ioloop.IOLoop.current().run_sync(do_insert)
@ajdavis
Copy link
Author

ajdavis commented Feb 4, 2017

This prints output like:


INFO:root:Topology with id 58963025ca1ce952b974e733 opened
INFO:root:Topology description updated for topology id 58963025ca1ce952b974e733
INFO:root:Topology 58963025ca1ce952b974e733 changed type from Unknown to Single
INFO:root:Server ('localhost', 27017) added to topology 58963025ca1ce952b974e733
INFO:root:Heartbeat sent to server ('localhost', 27017)
INFO:root:Heartbeat to server ('localhost', 27017) succeeded with reply {'ismaster': True, 'maxBsonObjectSize': 16777216, 'maxWriteBatchSize': 1000, 'localTime': datetime.datetime(2017, 2, 4, 19, 48, 53, 111000), 'maxMessageSizeBytes': 48000000, 'ok': 1.0, 'maxWireVersion': 5, 'minWireVersion': 0, 'readOnly': False}
INFO:root:Command insert with request id 1622650073 started on server ('localhost', 27017)
INFO:root:Command insert with request id 1622650073 on server ('localhost', 27017) succeeded in 336 microseconds
INFO:root:Heartbeat sent to server ('localhost', 27017)
INFO:root:Heartbeat to server ('localhost', 27017) succeeded with reply {'ismaster': True, 'maxBsonObjectSize': 16777216, 'maxWriteBatchSize': 1000, 'localTime': datetime.datetime(2017, 2, 4, 19, 48, 53, 617000), 'maxMessageSizeBytes': 48000000, 'ok': 1.0, 'maxWireVersion': 5, 'minWireVersion': 0, 'readOnly': False}
INFO:root:Server ('localhost', 27017) changed type from Unknown to Standalone
INFO:root:Topology description updated for topology id 58963025ca1ce952b974e733
INFO:root:Topology description updated for topology id 58963025ca1ce952b974e733

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment