What monitoring/health checking options does the golang mongodb driver support? Like connection count?

I recently started using the official driver instead of the globalsign project and one of the things I miss most is the ability to count how many active connections I have going with mongodb. I can see it on the server side in the monitoring section of the mongodb.com website. But I don’t know which of my servers is misbehaving without restarting them and watching the number go down. It would be nice if the mongo client gave health info like this. Does it already exist and I just can’t find it?

Have I posted in the wrong forum? Is there something I can do better to get a response?

I don’t think such thing exists. You may need to “emit metric” from all nodes in your cluster and do sort of aggregation. Host identifier is needed so that you know where the data is from.

In our system. We use k8s + prometheus, so we know which k8s pod is sending what data.

Sure, I can wrap the mongodb client and track what requests are made and when. But I can’t see the internal workings of the client. So, I can’t know how many connections currently exist (which matters because we can run out0. Is there really nothing to check the internal health of the client?

@David_Johnson1 thanks for the question! Which APIs from the globalsign/mgo driver did you find useful getting telemetry information? Is there information other than just connection count that mgo provided that you found useful?

As far as how to measure it in the official MongoDB Go Driver, the best way currently is using a PoolMonitor via ClientOptions.SetPoolMonitor configuration.

Here’s an example of counting total open connections:

var openConns int32
poolMonitor := &event.PoolMonitor{
	Event: func(evt *event.PoolEvent) {
		switch evt.Type {
		case event.ConnectionReady:
			atomic.AddInt32(&openConns, 1)
		case event.ConnectionClosed:
			atomic.AddInt32(&openConns, -1)
		}
	},
}

mongo.Connect(
	context.Background(),
	options.Client().
		ApplyURI("myURI").
		SetPoolMonitor(poolMonitor))