mongodbpymongo

PyMongo MongoClient - is there a way to see number of open connections?


The docs say that each mongo client maintains a connection pool, the max size of which I can specify. Is there anyway to check the number of open connections made by a specific mongo client at a given time? I can't seem to find anything in the docs


Solution

  • As of PyMongo 4.13, there is no public API which would allow checking number of currently opened connections to the database by specific client. However, you could try achieving this goal using more "hacky" approaches:

    1. Use aggregate() method on admin database with $currentOp stage. Alternatively, you could use command('currentOp') method, which is functionally same, but it's deprecated since Mongo 6.2. Aggregation will return you a cursor with documents describing current connections. You can specify some filters in stage definition, see docs above for details (you may be interested in idleConnections option, which is false by default). Unluckily, AFAIK, in returned information there is nothing like clientId, which would let you easily filter connections initiated by exact Mongo client instance. The best thing you can do is try to match IP address, present in client field, or driver version, present in clientMetadata.driver field (in case there are other, non PyMongo apps connected to the same database, using the same user). I attach sample code for happy path in which you have single app connecting from single IP below:
    client = MongoClient()
    YOUR_IP = '192.168.1.254'  # Enter your app host IP here
    current_op_data = list(client['admin'].aggregate([{'$currentOp': {'idleConnections': True}}]))
    print(sum(doc.get('client', '').startswith(YOUR_IP) for doc in current_op_data))
    

    Keep in mind that this method requires administrator privileges to your MongoDB.

    1. If you don't have administrator privileges, you could try finding open connections using your operating system tools, like ss on Linux. After filtering by PID of your python application and destination MongoDB host name and/or port, you should be able to get fairly accurate estimation of currently open MongoDB connections which are managed by your MongoDB client (assuming that you follow best practices and you create single MongoDB client in your application).

    Example simple scenario: I have a Linux machine with local instance of MongoDB available at localhost with my Python app connected to it (using single Mongo client), running in process with ID 8973. Then you would want to run ss -np | grep -E 127.0.0.1:27017.*8973 | wc -l command to get number of connections opened by this app.

    Finally, you could try diving deep into PyMongo source code and try to fetch connection information using non-public methods and attributes of Pool class, but I would highly advise against it, as it would likely take the most time to figure out correctly and be the least robust solution.