2

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.

Paul
  • 9,409
  • 13
  • 64
  • 113
  • what's your OS? windows or linux? typically you start seeing waitqueue/pooling issues when there are too many slow/long running queries keeping the connections occupied. i've had once instance where the issue was the bandwidth of the spinning harddisk. upgrading the server to an SSD fixed it. also make sure your queries are using indexes properly and not too many collection scans are happenning. you can also try changing the pool size: `new MongoClientSettings { MinConnectionPoolSize = 25, MaxConnectionPoolSize = 250, }` – Dĵ ΝιΓΞΗΛψΚ May 05 '21 at 08:39
  • @ĐĵΝιΓΞΗΛψΚ It's linux - on azure. That's the kind of thing I'd like to be able to monitor - to see how many of the connections in the pool are currently being occupied. – Paul May 05 '21 at 08:42
  • check this post (and underlying comments): https://stackoverflow.com/questions/64024753/how-to-get-current-connection-pool-occupancy-on-client-using-mongo-net-driver/64026465#64026465 – dododo May 05 '21 at 14:38

1 Answers1

3

There are loads of connection pool events that you can subscribe to which might help you. These are:

  • ConnectionPoolCheckingOutConnectionEvent
  • ConnectionPoolCheckedOutConnectionEvent
  • ConnectionPoolCheckingOutConnectionFailedEvent
  • ConnectionPoolCheckingInConnectionEvent
  • ConnectionPoolCheckedInConnectionEvent
  • ConnectionPoolAddingConnectionEvent
  • ConnectionPoolAddedConnectionEvent
  • ConnectionPoolOpeningEvent
  • ConnectionPoolOpenedEvent
  • ConnectionPoolClosingEvent
  • ConnectionPoolClosedEvent
  • ConnectionPoolClearingEvent
  • ConnectionPoolClearedEvent
  • ConnectionCreatedEvent

You can subscribe to these when you setup the MongoClient

    var mongoClientSettings = MongoClientSettings.FromUrl(new MongoUrl("mongodb://localhost"));

    mongoClientSettings.ClusterConfigurator = clusterConfigurator =>
    {
        clusterConfigurator.Subscribe<ConnectionPoolCheckingOutConnectionEvent>(e =>
        {
            
        });
    };

    var mongoClient = new MongoClient(mongoClientSettings);
Kevin Smith
  • 13,746
  • 4
  • 52
  • 77
  • This is helpful thank you. Do you think it would be sufficient to just inc and dec some static variables in each of those events to get out some numbers? – Paul Jun 03 '21 at 08:54
  • 1
    I'd see no problem with that just make sure they're concurrency safe with using things like `Interlock` etc... – Kevin Smith Jun 03 '21 at 14:37