Add a 'timeAsyncMethod' helper

This commit is contained in:
Shane Kilkelly 2017-03-15 15:06:54 +00:00
parent caeac717fc
commit 3cb0ab2784
2 changed files with 33 additions and 1 deletions

View file

@ -10,7 +10,7 @@ destructors = []
require "./uv_threadpool_size"
module.exports =
module.exports = Metrics =
initialize: (_name) ->
name = _name
@ -48,6 +48,8 @@ module.exports =
event_loop: require "./event_loop"
memory: require "./memory"
timeAsyncMethod: require('./timeAsyncMethod')
close: () ->
for func in destructors
func()

View file

@ -0,0 +1,30 @@
module.exports = (obj, methodName, key, logger) ->
metrics = require('./metrics')
if typeof obj[methodName] != 'function'
throw new Error("[Metrics] expected object property #{methodName} to be a function")
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"
)
timer = new Metrics.Timer(key)
start = new Date()
realMethod.call this, firstArgs, (callbackArgs...) ->
timer.done()
elapsedTime = new Date() - start
if logger?
logger.log
key: key
time: elapsedTime
"[Metrics] timed async method call"
callback.apply this, callbackArgs