2015-06-23 09:51:48 +00:00
|
|
|
module.exports = EventLoopMonitor =
|
2015-12-03 16:32:20 +00:00
|
|
|
monitor: (logger, interval = 1000, log_threshold = 100) ->
|
2015-06-23 09:51:48 +00:00
|
|
|
Metrics = require "./metrics"
|
2018-05-10 09:10:34 +00: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 16:32:20 +00: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 09:51:48 +00:00
|
|
|
Metrics.registerDestructor () ->
|
2015-12-03 16:32:20 +00:00
|
|
|
clearInterval(intervalId)
|