overleaf/libraries/metrics/timeAsyncMethod.js

87 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-07-17 11:01:58 -04:00
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS201: Simplify complex destructure assignments
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
module.exports = function (obj, methodName, prefix, logger) {
2020-09-11 14:18:22 -04:00
let modifedMethodName
const metrics = require('./index')
2020-07-17 11:01:58 -04:00
2020-09-11 14:18:22 -04:00
if (typeof obj[methodName] !== 'function') {
throw new Error(
`[Metrics] expected object property '${methodName}' to be a function`
)
}
2020-07-17 11:01:58 -04:00
2020-09-11 14:18:22 -04:00
const key = `${prefix}.${methodName}`
2020-07-17 11:01:58 -04:00
2020-09-11 14:18:22 -04:00
const realMethod = obj[methodName]
2020-07-17 11:01:58 -04:00
2020-09-11 14:18:22 -04:00
const splitPrefix = prefix.split('.')
const startPrefix = splitPrefix[0]
2020-07-17 11:01:58 -04:00
2020-09-11 14:18:22 -04:00
if (splitPrefix[1] != null) {
modifedMethodName = `${splitPrefix[1]}_${methodName}`
} else {
modifedMethodName = methodName
}
return (obj[methodName] = function (...originalArgs) {
2020-09-11 14:18:22 -04:00
const adjustedLength = Math.max(originalArgs.length, 1)
const firstArgs = originalArgs.slice(0, adjustedLength - 1)
const callback = originalArgs[adjustedLength - 1]
2020-07-17 11:01:58 -04:00
2020-09-11 14:18:22 -04:00
if (callback == null || typeof callback !== 'function') {
if (logger != null) {
logger.log(
`[Metrics] expected wrapped method '${methodName}' to be invoked with a callback`
)
}
return realMethod.apply(this, originalArgs)
}
2020-07-17 11:01:58 -04:00
2020-09-11 14:18:22 -04:00
const timer = new metrics.Timer(startPrefix, 1, {
method: modifedMethodName,
2020-09-11 14:18:22 -04:00
})
2020-07-17 11:01:58 -04:00
return realMethod.call(
this,
...Array.from(firstArgs),
function (...callbackArgs) {
const elapsedTime = timer.done()
const possibleError = callbackArgs[0]
if (possibleError != null) {
metrics.inc(`${startPrefix}_result`, 1, {
status: 'failed',
method: modifedMethodName,
})
} else {
metrics.inc(`${startPrefix}_result`, 1, {
status: 'success',
method: modifedMethodName,
})
}
if (logger != null) {
const loggableArgs = {}
try {
for (let idx = 0; idx < firstArgs.length; idx++) {
const arg = firstArgs[idx]
if (arg.toString().match(/^[0-9a-f]{24}$/)) {
loggableArgs[`${idx}`] = arg
}
2020-09-11 14:18:22 -04:00
}
} catch (error) {}
logger.log(
{ key, args: loggableArgs, elapsedTime },
'[Metrics] timed async method call'
)
}
return callback.apply(this, callbackArgs)
2020-09-11 14:18:22 -04:00
}
)
2020-09-11 14:18:22 -04:00
})
}