[web] collect mongo stats on native client (#32909)

* [metrics] mongo: fail when command monitoring is not available

* [metrics] mongo: add optional client label to pool metrics

* [web] collect mongo stats on native client

* [metrics] mongo: record namespace of find commands

* [metrics] mongo: add counter for all the commands with collection label

* [web] add missing mock

GitOrigin-RevId: 9f378d8aa8d7167f56cf512681d63ef115c6dd98
This commit is contained in:
Jakob Ackermann
2026-04-20 08:05:44 +00:00
committed by Copybot
parent 26820bc327
commit ad79c85cea
5 changed files with 113 additions and 55 deletions
@@ -11,17 +11,7 @@ const connectionPromise = mongoose.connect(
Settings.mongo.url,
Settings.mongo.options
)
connectionPromise
.then(mongooseInstance => {
Metrics.mongodb.monitor(mongooseInstance.connection.client)
})
.catch(error => {
logger.error(
{ error },
'Failed to connect to MongoDB - cannot set up monitoring'
)
})
Metrics.mongodb.monitor(mongoose.connection.client, 'mongoose')
addConnectionDrainer('mongoose', async () => {
await connectionPromise
@@ -4,6 +4,7 @@ import Settings from '@overleaf/settings'
import MongoUtils from '@overleaf/mongo-utils'
import Mongoose from './Mongoose.mjs'
import { addConnectionDrainer } from './GracefulShutdown.mjs'
import Metrics from '@overleaf/metrics'
// Ensure Mongoose is using the same mongodb instance as the mongodb module,
// otherwise we will get multiple versions of the ObjectId class. Mongoose
@@ -27,6 +28,7 @@ const mongoClient = new mongodb.MongoClient(
Settings.mongo.url,
Settings.mongo.options
)
Metrics.mongodb.monitor(mongoClient, 'native')
addConnectionDrainer('mongodb', async () => {
await mongoClient.close()
+1
View File
@@ -59,6 +59,7 @@ vi.mock('@overleaf/metrics', () => {
}
},
prom: { Counter: sinon.stub(), Histogram: sinon.stub() },
mongodb: { monitor: sinon.stub() },
},
}
})