Merge pull request #16873 from overleaf/dp-mongo-command-monitoring

Use mongo command monitoring to add timing metrics

GitOrigin-RevId: e7e5dd5cca1ba3802c02198ccf81058d4da3f1e7
This commit is contained in:
David 2024-02-22 11:03:27 +00:00 committed by Copybot
parent 10e7be0a69
commit b59326e96f
14 changed files with 66 additions and 8 deletions

View file

@ -1,4 +1,4 @@
const { Gauge } = require('prom-client')
const { Gauge, Summary } = require('prom-client')
function monitor(mongoClient) {
const labelNames = ['mongo_server']
@ -25,6 +25,35 @@ function monitor(mongoClient) {
labelNames,
})
const mongoCommandTimer = new Summary({
name: 'mongo_command_time',
help: 'time taken to complete a mongo command',
percentiles: [],
labelNames: ['status', 'method'],
})
if (mongoClient.on) {
mongoClient.on('commandSucceeded', event => {
mongoCommandTimer.observe(
{
status: 'success',
method: event.commandName === 'find' ? 'read' : 'write',
},
event.duration
)
})
mongoClient.on('commandFailed', event => {
mongoCommandTimer.observe(
{
status: 'failed',
method: event.commandName === 'find' ? 'read' : 'write',
},
event.duration
)
})
}
function collect() {
// Reset all gauges in case they contain values for servers that
// disappeared

View file

@ -4,7 +4,10 @@ import { MongoClient } from 'mongodb'
export { ObjectId } from 'mongodb'
export const mongoClient = new MongoClient(Settings.mongo.url)
export const mongoClient = new MongoClient(
Settings.mongo.url,
Settings.mongo.options
)
const mongoDb = mongoClient.db()
export const db = {

View file

@ -20,5 +20,8 @@ module.exports = {
url:
process.env.MONGO_CONNECTION_STRING ||
`mongodb://${process.env.MONGO_HOST || 'localhost'}/sharelatex`,
options: {
monitorCommands: true,
},
},
}

View file

@ -4,7 +4,10 @@ import { MongoClient } from 'mongodb'
export { ObjectId } from 'mongodb'
export const mongoClient = new MongoClient(Settings.mongo.url)
export const mongoClient = new MongoClient(
Settings.mongo.url,
Settings.mongo.options
)
const mongoDb = mongoClient.db()
export const db = {

View file

@ -13,5 +13,8 @@ module.exports = {
url:
process.env.MONGO_CONNECTION_STRING ||
`mongodb://${process.env.MONGO_HOST || 'localhost'}/sharelatex`,
options: {
monitorCommands: true,
},
},
}

View file

@ -2,7 +2,7 @@ const Metrics = require('@overleaf/metrics')
const Settings = require('@overleaf/settings')
const { MongoClient, ObjectId } = require('mongodb-legacy')
const mongoClient = new MongoClient(Settings.mongo.url)
const mongoClient = new MongoClient(Settings.mongo.url, Settings.mongo.options)
const mongoDb = mongoClient.db()
const db = {

View file

@ -9,7 +9,11 @@ const Settings = {
},
},
mongo: {},
mongo: {
options: {
monitorCommands: true,
},
},
docstore: {
archiveOnSoftDelete: process.env.ARCHIVE_ON_SOFT_DELETE === 'true',

View file

@ -2,7 +2,7 @@ const Metrics = require('@overleaf/metrics')
const Settings = require('@overleaf/settings')
const { MongoClient, ObjectId } = require('mongodb-legacy')
const mongoClient = new MongoClient(Settings.mongo.url)
const mongoClient = new MongoClient(Settings.mongo.url, Settings.mongo.options)
const mongoDb = mongoClient.db()
const db = {

View file

@ -159,6 +159,9 @@ module.exports = {
url:
process.env.MONGO_CONNECTION_STRING ||
`mongodb://${process.env.MONGO_HOST || '127.0.0.1'}/sharelatex`,
options: {
monitorCommands: true,
},
},
sentry: {

View file

@ -2,7 +2,7 @@ const Metrics = require('@overleaf/metrics')
const Settings = require('@overleaf/settings')
const { MongoClient, ObjectId } = require('mongodb-legacy')
const mongoClient = new MongoClient(Settings.mongo.url)
const mongoClient = new MongoClient(Settings.mongo.url, Settings.mongo.options)
const mongoDb = mongoClient.db()
const db = {

View file

@ -10,5 +10,8 @@ module.exports = {
url:
process.env.MONGO_CONNECTION_STRING ||
`mongodb://${process.env.MONGO_HOST || 'localhost'}/sharelatex`,
options: {
monitorCommands: true,
},
},
}

View file

@ -5,7 +5,10 @@ const { MongoClient, ObjectId } = mongodb
export { ObjectId }
export const mongoClient = new MongoClient(Settings.mongo.url)
export const mongoClient = new MongoClient(
Settings.mongo.url,
Settings.mongo.options
)
const mongoDb = mongoClient.db()
Metrics.mongodb.monitor(mongoClient)

View file

@ -3,6 +3,9 @@ module.exports = {
url:
process.env.MONGO_CONNECTION_STRING ||
`mongodb://${process.env.MONGO_HOST || 'localhost'}/sharelatex`,
options: {
monitorCommands: true,
},
},
internal: {
history: {

View file

@ -129,6 +129,7 @@ module.exports = {
process.env.MONGO_SOCKET_TIMEOUT ?? '60000',
10
),
monitorCommands: true,
},
url:
process.env.MONGO_CONNECTION_STRING ||