2020-07-17 11:01:58 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
module.exports = {
|
2020-09-11 14:18:22 -04:00
|
|
|
monitor(mongodbRequirePath, logger) {
|
|
|
|
let mongodb, mongodbCore
|
|
|
|
try {
|
|
|
|
// for the v1 driver the methods to wrap are in the mongodb
|
|
|
|
// module in lib/mongodb/db.js
|
|
|
|
mongodb = require(mongodbRequirePath)
|
|
|
|
} catch (error) {}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// for the v2 driver the relevant methods are in the mongodb-core
|
|
|
|
// module in lib/topologies/{server,replset,mongos}.js
|
|
|
|
const v2Path = mongodbRequirePath.replace(/\/mongodb$/, '/mongodb-core')
|
|
|
|
mongodbCore = require(v2Path)
|
|
|
|
} catch (error1) {}
|
|
|
|
|
|
|
|
const Metrics = require('./index')
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
const monitorMethod = function (base, method, type) {
|
2020-09-11 14:18:22 -04:00
|
|
|
let _method
|
|
|
|
if (base == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ((_method = base[method]) == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const arglen = _method.length
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
const mongoDriverV1Wrapper = function (dbCommand, options, callback) {
|
2020-09-11 14:18:22 -04:00
|
|
|
let query
|
|
|
|
if (typeof callback === 'undefined') {
|
|
|
|
callback = options
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
const collection = dbCommand.collectionName
|
|
|
|
if (collection.match(/\$cmd$/)) {
|
|
|
|
// Ignore noisy command methods like authenticating, ismaster and ping
|
|
|
|
return _method.call(this, dbCommand, options, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dbCommand.query != null) {
|
2021-12-16 04:04:32 -05:00
|
|
|
query = Object.keys(dbCommand.query).sort().join('_')
|
2020-09-11 14:18:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const timer = new Metrics.Timer('mongo', { collection, query })
|
|
|
|
const start = new Date()
|
2021-12-16 04:04:32 -05:00
|
|
|
return _method.call(this, dbCommand, options, function () {
|
2020-09-11 14:18:22 -04:00
|
|
|
timer.done()
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(
|
2020-09-11 14:18:22 -04:00
|
|
|
{
|
|
|
|
query: dbCommand.query,
|
|
|
|
query_type: type,
|
|
|
|
collection,
|
2021-12-16 04:04:32 -05:00
|
|
|
'response-time': new Date() - start,
|
2020-09-11 14:18:22 -04:00
|
|
|
},
|
|
|
|
'mongo request'
|
|
|
|
)
|
|
|
|
return callback.apply(this, arguments)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
const mongoDriverV2Wrapper = function (ns, ops, options, callback) {
|
2020-09-11 14:18:22 -04:00
|
|
|
let query
|
|
|
|
if (typeof callback === 'undefined') {
|
|
|
|
callback = options
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ns.match(/\$cmd$/)) {
|
|
|
|
// Ignore noisy command methods like authenticating, ismaster and ping
|
|
|
|
return _method.call(this, ns, ops, options, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
let key = `mongo-requests.${ns}.${type}`
|
|
|
|
if (ops[0].q != null) {
|
|
|
|
// ops[0].q
|
2021-12-16 04:04:32 -05:00
|
|
|
query = Object.keys(ops[0].q).sort().join('_')
|
2020-09-11 14:18:22 -04:00
|
|
|
key += '.' + query
|
|
|
|
}
|
|
|
|
|
|
|
|
const timer = new Metrics.Timer(key)
|
|
|
|
const start = new Date()
|
2021-12-16 04:04:32 -05:00
|
|
|
return _method.call(this, ns, ops, options, function () {
|
2020-09-11 14:18:22 -04:00
|
|
|
timer.done()
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(
|
2020-09-11 14:18:22 -04:00
|
|
|
{
|
|
|
|
query: ops[0].q,
|
|
|
|
query_type: type,
|
|
|
|
collection: ns,
|
2021-12-16 04:04:32 -05:00
|
|
|
'response-time': new Date() - start,
|
2020-09-11 14:18:22 -04:00
|
|
|
},
|
|
|
|
'mongo request'
|
|
|
|
)
|
|
|
|
return callback.apply(this, arguments)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arglen === 3) {
|
|
|
|
return (base[method] = mongoDriverV1Wrapper)
|
|
|
|
} else if (arglen === 4) {
|
|
|
|
return (base[method] = mongoDriverV2Wrapper)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
monitorMethod(
|
|
|
|
mongodb != null ? mongodb.Db.prototype : undefined,
|
|
|
|
'_executeQueryCommand',
|
|
|
|
'query'
|
|
|
|
)
|
|
|
|
monitorMethod(
|
|
|
|
mongodb != null ? mongodb.Db.prototype : undefined,
|
|
|
|
'_executeRemoveCommand',
|
|
|
|
'remove'
|
|
|
|
)
|
|
|
|
monitorMethod(
|
|
|
|
mongodb != null ? mongodb.Db.prototype : undefined,
|
|
|
|
'_executeInsertCommand',
|
|
|
|
'insert'
|
|
|
|
)
|
|
|
|
monitorMethod(
|
|
|
|
mongodb != null ? mongodb.Db.prototype : undefined,
|
|
|
|
'_executeUpdateCommand',
|
|
|
|
'update'
|
|
|
|
)
|
|
|
|
|
|
|
|
monitorMethod(
|
|
|
|
mongodbCore != null ? mongodbCore.Server.prototype : undefined,
|
|
|
|
'command',
|
|
|
|
'command'
|
|
|
|
)
|
|
|
|
monitorMethod(
|
|
|
|
mongodbCore != null ? mongodbCore.Server.prototype : undefined,
|
|
|
|
'remove',
|
|
|
|
'remove'
|
|
|
|
)
|
|
|
|
monitorMethod(
|
|
|
|
mongodbCore != null ? mongodbCore.Server.prototype : undefined,
|
|
|
|
'insert',
|
|
|
|
'insert'
|
|
|
|
)
|
|
|
|
monitorMethod(
|
|
|
|
mongodbCore != null ? mongodbCore.Server.prototype : undefined,
|
|
|
|
'update',
|
|
|
|
'update'
|
|
|
|
)
|
|
|
|
|
|
|
|
monitorMethod(
|
|
|
|
mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined,
|
|
|
|
'command',
|
|
|
|
'command'
|
|
|
|
)
|
|
|
|
monitorMethod(
|
|
|
|
mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined,
|
|
|
|
'remove',
|
|
|
|
'remove'
|
|
|
|
)
|
|
|
|
monitorMethod(
|
|
|
|
mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined,
|
|
|
|
'insert',
|
|
|
|
'insert'
|
|
|
|
)
|
|
|
|
monitorMethod(
|
|
|
|
mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined,
|
|
|
|
'update',
|
|
|
|
'update'
|
|
|
|
)
|
|
|
|
|
|
|
|
monitorMethod(
|
|
|
|
mongodbCore != null ? mongodbCore.Mongos.prototype : undefined,
|
|
|
|
'command',
|
|
|
|
'command'
|
|
|
|
)
|
|
|
|
monitorMethod(
|
|
|
|
mongodbCore != null ? mongodbCore.Mongos.prototype : undefined,
|
|
|
|
'remove',
|
|
|
|
'remove'
|
|
|
|
)
|
|
|
|
monitorMethod(
|
|
|
|
mongodbCore != null ? mongodbCore.Mongos.prototype : undefined,
|
|
|
|
'insert',
|
|
|
|
'insert'
|
|
|
|
)
|
|
|
|
return monitorMethod(
|
|
|
|
mongodbCore != null ? mongodbCore.Mongos.prototype : undefined,
|
|
|
|
'update',
|
|
|
|
'update'
|
|
|
|
)
|
2021-12-16 04:04:32 -05:00
|
|
|
},
|
2020-09-11 14:18:22 -04:00
|
|
|
}
|