2024-02-22 11:03:27 +00:00
|
|
|
const { Gauge, Summary } = require('prom-client')
|
2023-02-09 12:24:44 +00:00
|
|
|
|
|
|
|
function monitor(mongoClient) {
|
|
|
|
const labelNames = ['mongo_server']
|
|
|
|
const poolSize = new Gauge({
|
|
|
|
name: 'mongo_connection_pool_size',
|
|
|
|
help: 'number of connections in the connection pool',
|
|
|
|
labelNames,
|
|
|
|
// Use this one metric's collect() to set all metrics' values.
|
|
|
|
collect,
|
|
|
|
})
|
|
|
|
const availableConnections = new Gauge({
|
|
|
|
name: 'mongo_connection_pool_available',
|
|
|
|
help: 'number of connections that are not busy',
|
|
|
|
labelNames,
|
|
|
|
})
|
|
|
|
const waitQueueSize = new Gauge({
|
|
|
|
name: 'mongo_connection_pool_waiting',
|
|
|
|
help: 'number of operations waiting for an available connection',
|
|
|
|
labelNames,
|
|
|
|
})
|
|
|
|
const maxPoolSize = new Gauge({
|
|
|
|
name: 'mongo_connection_pool_max',
|
|
|
|
help: 'max size for the connection pool',
|
|
|
|
labelNames,
|
|
|
|
})
|
|
|
|
|
2024-02-22 11:03:27 +00:00
|
|
|
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
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-09 12:24:44 +00:00
|
|
|
function collect() {
|
|
|
|
// Reset all gauges in case they contain values for servers that
|
|
|
|
// disappeared
|
|
|
|
poolSize.reset()
|
|
|
|
availableConnections.reset()
|
|
|
|
waitQueueSize.reset()
|
|
|
|
maxPoolSize.reset()
|
|
|
|
|
|
|
|
const servers = mongoClient.topology?.s?.servers
|
|
|
|
if (servers != null) {
|
|
|
|
for (const [address, server] of servers) {
|
|
|
|
const pool = server.s?.pool
|
|
|
|
if (pool == null) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
const labels = { mongo_server: address }
|
|
|
|
poolSize.set(labels, pool.totalConnectionCount)
|
|
|
|
availableConnections.set(labels, pool.availableConnectionCount)
|
|
|
|
waitQueueSize.set(labels, pool.waitQueueSize)
|
|
|
|
maxPoolSize.set(labels, pool.options.maxPoolSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { monitor }
|