Mongo C# Driver metrics

Viewed 270

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.

1 Answers

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);
Related