Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make hapi good ignore a specific route

I'd like to have good ignore logging for a a specific route since it's a health check route gets polled by amazon every few seconds - it just adds noise to our logs.

Is there route specific configuration I can set for the good plugin so that it just ignores a single specific route?

like image 953
MonkeyBonkey Avatar asked Sep 28 '15 19:09

MonkeyBonkey


2 Answers

Here’s something that works at least with hapi 16.1, good 7.1, and good-squeeze 5.0. The idea is to tag the health check route’s logging, and then exclude that route with good-squeeze.

Configuring good/good-squeeze to exclude “health”:

server.register({
  register: Good,
  options: {
    reporters: {
      console: [
        {
          module: 'good-squeeze',
          name: 'Squeeze',
          args: [
            {
              // keep health checks from appearing in logs
              response: { exclude: 'health' },
              log: '*',
            },
          ],
        },
        {
          module: 'good-console',
        },
        'stdout',
      ],
    },
  },
});

And then tag your route:

server.route({
  method: 'GET',
  path: '/admin/ok',
  handler: (request, reply) => reply('ok'),
  config: {
    tags: ['health'],
  },
});
like image 177
Fiona Hopkins Avatar answered Nov 13 '22 07:11

Fiona Hopkins


It is possible to do. Here is the documentation https://github.com/hapijs/good/blob/master/API.md#stream-transforms-using-plugin-configs

Hapi route configuration

var routeConfig = {
    plugins: {
        good: {
            suppressResponseEvent: true
        }
    }
};

server.route({
    method: '*',
    path: '/suscribe/{path*}',
    handler: function(req, rep){
            rep("OK!");
    },
    config: routeConfig
});

Creating a custom filter for good. You will need to create a new npm package. For this example, we are going to name it good-filter Follow the architecture from https://github.com/hapijs/good-squeeze/ The main file (index.js) contains:

'use strict';

 module.exports = {
     Filter: require("./filter.js")
 };

This package must be available when good is loading. The following code goes into filter.js in the good-filter package.

'use strict';

const Stream = require('stream');

class Filter extends Stream.Transform {
    constructor(options) {
        options = Object.assign({}, options, {
            objectMode: true
        });
        super(options);
    }
    _transform(data, enc, next) {

        if (data.event === 'response' && data.config.suppressResponseEvent === true) {

            return next();
        }

        return next(null, data);
    }
}

module.exports = Filter;

Finally, add your filter to good configuration.

const options = {
    ops: {
        interval: 1000
    },
    reporters: {
        myConsoleReporter: [{
            module: 'good-squeeze',
            name: 'Squeeze',
            args: [{ log: '*', response: '*' }]
        }, 
        {
            module: 'good-filter',
            name: 'Filter',
            args: [{ log: '*', response: '*' }]
        },
        {
            module: 'good-console'
        }, 'stdout']
    }
};
like image 22
Juan Avatar answered Nov 13 '22 07:11

Juan