overleaf/libraries/metrics/timeAsyncMethod.coffee

26 lines
760 B
CoffeeScript
Raw Normal View History

2017-03-15 11:06:54 -04:00
module.exports = (obj, methodName, key, logger) ->
metrics = require('./metrics')
if typeof obj[methodName] != 'function'
2017-03-16 06:02:19 -04:00
throw new Error("[Metrics] expected object property '#{methodName}' to be a function")
2017-03-15 11:06:54 -04:00
realMethod = obj[methodName]
key = "methods.#{key}"
obj[methodName] = (originalArgs...) ->
[firstArgs..., callback] = originalArgs
if !callback? || typeof callback != 'function'
throw new Error(
"[Metrics] expected wrapped method '#{methodName}' to be invoked with a callback"
)
2017-03-15 12:07:36 -04:00
timer = new metrics.Timer(key)
2017-03-15 11:06:54 -04:00
2017-03-15 12:07:36 -04:00
realMethod.call this, firstArgs..., (callbackArgs...) ->
2017-03-16 11:07:25 -04:00
elapsedTime = timer.done()
2017-03-15 11:06:54 -04:00
if logger?
2017-03-15 12:07:36 -04:00
logger.log {key, elapsedTime}, "[Metrics] timed async method call"
2017-03-15 11:06:54 -04:00
callback.apply this, callbackArgs