2015-06-23 05:51:48 -04:00
|
|
|
module.exports = EventLoopMonitor =
|
2015-12-03 11:32:20 -05:00
|
|
|
monitor: (logger, interval = 1000, log_threshold = 100) ->
|
2015-06-23 05:51:48 -04:00
|
|
|
Metrics = require "./metrics"
|
2018-05-10 05:10:34 -04:00
|
|
|
# check for logger on startup to avoid exceptions later if undefined
|
|
|
|
throw new Error("logger is undefined") if !logger?
|
|
|
|
# monitor delay in setInterval to detect event loop blocking
|
2015-12-03 11:32:20 -05:00
|
|
|
previous = Date.now()
|
|
|
|
intervalId = setInterval () ->
|
|
|
|
now = Date.now()
|
|
|
|
offset = now - previous - interval
|
|
|
|
if offset > log_threshold
|
|
|
|
logger.warn {offset: offset}, "slow event loop"
|
|
|
|
previous = now
|
|
|
|
Metrics.timing("event-loop-millsec", offset)
|
|
|
|
, interval
|
|
|
|
|
2015-06-23 05:51:48 -04:00
|
|
|
Metrics.registerDestructor () ->
|
2015-12-03 11:32:20 -05:00
|
|
|
clearInterval(intervalId)
|