event_loop.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. /*
  2. * decaffeinate suggestions:
  3. * DS102: Remove unnecessary code created because of implicit returns
  4. * DS207: Consider shorter variations of null checks
  5. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  6. */
  7. module.exports = {
  8. monitor(logger, interval, logThreshold) {
  9. if (interval == null) {
  10. interval = 1000
  11. }
  12. if (logThreshold == null) {
  13. logThreshold = 100
  14. }
  15. const Metrics = require('./index')
  16. // check for logger on startup to avoid exceptions later if undefined
  17. if (logger == null) {
  18. throw new Error('logger is undefined')
  19. }
  20. // monitor delay in setInterval to detect event loop blocking
  21. let previous = Date.now()
  22. const intervalId = setInterval(function() {
  23. const now = Date.now()
  24. const offset = now - previous - interval
  25. if (offset > logThreshold) {
  26. logger.warn({ offset }, 'slow event loop')
  27. }
  28. previous = now
  29. return Metrics.timing('event-loop-millsec', offset)
  30. }, interval)
  31. return Metrics.registerDestructor(() => clearInterval(intervalId))
  32. }
  33. }