2017-03-15 11:06:54 -04:00
|
|
|
|
2017-03-17 07:47:38 -04:00
|
|
|
module.exports = (obj, methodName, prefix, logger) ->
|
2017-03-15 11:06:54 -04:00
|
|
|
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]
|
2017-03-17 07:47:38 -04:00
|
|
|
key = "#{prefix}.#{methodName}"
|
2017-03-15 11:06:54 -04:00
|
|
|
|
|
|
|
obj[methodName] = (originalArgs...) ->
|
|
|
|
|
|
|
|
[firstArgs..., callback] = originalArgs
|
2017-03-20 12:17:22 -04:00
|
|
|
|
2017-03-15 11:06:54 -04:00
|
|
|
if !callback? || typeof callback != 'function'
|
2017-03-20 12:37:53 -04:00
|
|
|
if logger?
|
|
|
|
logger.log "[Metrics] expected wrapped method '#{methodName}' to be invoked with a callback"
|
|
|
|
return realMethod.apply this, originalArgs
|
2017-03-15 11:06:54 -04:00
|
|
|
|
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-20 12:54:40 -04:00
|
|
|
possibleError = callbackArgs[0]
|
2017-03-21 10:19:12 -04:00
|
|
|
if possibleError?
|
2017-03-20 12:54:40 -04:00
|
|
|
metrics.inc "#{key}.failure"
|
|
|
|
else
|
|
|
|
metrics.inc "#{key}.success"
|
2017-03-15 11:06:54 -04:00
|
|
|
if logger?
|
2017-03-17 10:20:13 -04:00
|
|
|
loggableArgs = {}
|
|
|
|
try
|
|
|
|
for arg, idx in firstArgs
|
|
|
|
if arg.toString().match(/^[0-9a-f]{24}$/)
|
|
|
|
loggableArgs["#{idx}"] = arg
|
|
|
|
logger.log {key, args: loggableArgs, elapsedTime}, "[Metrics] timed async method call"
|
2017-03-15 11:06:54 -04:00
|
|
|
callback.apply this, callbackArgs
|