2020-07-17 15:01:58 +00:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-09-11 18:18:22 +00:00
|
|
|
module.exports = {
|
|
|
|
monitor(logger, interval, logThreshold) {
|
|
|
|
if (interval == null) {
|
|
|
|
interval = 1000
|
|
|
|
}
|
|
|
|
if (logThreshold == null) {
|
|
|
|
logThreshold = 100
|
|
|
|
}
|
|
|
|
const Metrics = require('./index')
|
|
|
|
// check for logger on startup to avoid exceptions later if undefined
|
|
|
|
if (logger == null) {
|
|
|
|
throw new Error('logger is undefined')
|
|
|
|
}
|
|
|
|
// monitor delay in setInterval to detect event loop blocking
|
|
|
|
let previous = Date.now()
|
2021-12-16 09:04:32 +00:00
|
|
|
const intervalId = setInterval(function () {
|
2020-09-11 18:18:22 +00:00
|
|
|
const now = Date.now()
|
|
|
|
const offset = now - previous - interval
|
|
|
|
if (offset > logThreshold) {
|
|
|
|
logger.warn({ offset }, 'slow event loop')
|
|
|
|
}
|
|
|
|
previous = now
|
|
|
|
return Metrics.timing('event-loop-millsec', offset)
|
|
|
|
}, interval)
|
|
|
|
|
|
|
|
return Metrics.registerDestructor(() => clearInterval(intervalId))
|
2021-12-16 09:04:32 +00:00
|
|
|
},
|
2020-09-11 18:18:22 +00:00
|
|
|
}
|