overleaf/services/document-updater/app/js/mongodb.js
David b59326e96f Merge pull request #16873 from overleaf/dp-mongo-command-monitoring
Use mongo command monitoring to add timing metrics

GitOrigin-RevId: e7e5dd5cca1ba3802c02198ccf81058d4da3f1e7
2024-02-23 09:02:55 +00:00

28 lines
709 B
JavaScript

const Metrics = require('@overleaf/metrics')
const Settings = require('@overleaf/settings')
const { MongoClient, ObjectId } = require('mongodb-legacy')
const mongoClient = new MongoClient(Settings.mongo.url, Settings.mongo.options)
const mongoDb = mongoClient.db()
const db = {
docs: mongoDb.collection('docs'),
docSnapshots: mongoDb.collection('docSnapshots'),
projects: mongoDb.collection('projects'),
}
async function healthCheck() {
const res = await mongoDb.command({ ping: 1 })
if (!res.ok) {
throw new Error('failed mongo ping')
}
}
Metrics.mongodb.monitor(mongoClient)
module.exports = {
db,
ObjectId,
mongoClient,
healthCheck: require('util').callbackify(healthCheck),
}