We’re having problems with connection pooling and the wait queue with mongo using the mongo c# driver.
Is there any way to get metrics from the MongoClient at all? Things like number of active connections, size of connection pool, size of wait queue etc.
Hi, Paul,
Thank you for reaching out to MongoDB about your question regarding MongoClient
metrics.
The .NET/C# driver exposes a wide variety of internal metrics via Eventing. This includes creation/destruction of connection pools, connections being added/removed from pools, connections checked in/out of pools, and more. For example, the wait queue is entered when ConnectionPoolCheckingOutConnectionEvent
is raised and exited when ConnectionPoolCheckedOutConnectionEvent
or ConnectionPoolCheckingOutConnectionFailedEvent
is raised. A connection is active between ConnectionPoolCheckedOutConnectionEvent
and ConnectionPoolCheckedInConnectionEvent
. You can keep track of the total number of connections by monitoring ConnectionPoolAddedConnectionEvent
and ConnectionPoolRemovedConnectionEvent
.
One point to note. Connection pools are per cluster member, not per MongoClient. So if you have a 3 member replica set, you will have 3 connection pools, one for each node in the cluster. There is an additional monitoring connection outside of the connection pools using for Server Discovery and Monitoring (SDAM).
For an example of how to build a monitoring solution, you can take a look at the PerformanceCounterEventSubscriber in the source code. You can see how various counters are incremented and decremented in response to various driver events, which can then be forwarded onto whatever monitoring solution (potentially as simple as Console.WriteLine
for debugging purposes) you want to use. Note that this particular subscriber neglected to handle the ConnectionPoolCheckingOutConnectionFailedEvent
, which means that counts will not be accurate in the face of connection checkout timeouts.
Hopefully that gives you a starting point for diagnosing the issues that you are observing. Please let us know if you have any additional questions.
Sincerely,
James