2

I saw in this question how to setup Hapi Server to monitor the delay in the event loop of Node.js.

But I would like to log every time the delay exceeds a threshold, instead of returning 503.

How could I do it?

According to the API reference, there is an 'load' property in the server instance that contains the information I want, but I don't know how to listen to the event (or if there is such event) triggered when the load is checked.

UPDATE:

Ok, I found out there is a 'log' event that is triggered "when the server rejects a request due to high load", according to this.
But I just want to log, I don't want to reject any requests, so this doesn't solve the problem.

What I have so far, which logs when requests are rejected, is this:

server.on('log', (event) => {
  if (event.tags && event.tags.indexOf('load') > -1) {
    console.error(`Event loop lag detected! Latency: ${event.data.eventLoopDelay} ms`);
  }
});
luislhl
  • 1,136
  • 1
  • 10
  • 23

1 Answers1

0

I found a solution that works for now, using node-toobusy, but I would be happy to know if it's possible to use only Hapi for this.

In case it helps someone, what I did was disabling the monitoring in Hapi, and keeping only node-toobusy checking it:

import toobusy from 'node-toobusy'

toobusy.maxLag(1000);
toobusy.onLag(currentLag => {
  logger.error(`Event loop lag detected! Latency: ${currentLag} ms`);
});

This way no requests are blocked, I have just the logging, as I wanted.

luislhl
  • 1,136
  • 1
  • 10
  • 23