@financial-times/n-express-monitor

a configurable [express](https://github.com/expressjs/express) decorator to automate log, metrics to enhance debug and monitor experience

Stats

StarsIssuesVersionUpdatedCreatedSize
@financial-times/n-express-monitor
5331.1.23 years ago3 years agoMinified + gzip package size for @financial-times/n-express-monitor in KB

Readme

n-express-monitor

a configurable express decorator to automate log, metrics to enhance debug and monitor experience

npm version npm download node version

CircleCI Coverage Status Known Vulnerabilities Scrutinizer Code Quality Dependencies devDependencies



Install

npm install @financial-times/n-express-monitor --save

Demo

next-monitor-express

Usage

setupMonitor

import express, { metrics } from '@financial-times/n-express';
import { setupMonitor } from '@financial-times/n-express-monitor';

const app = express();

setupMonitor({ app, metrics }); // if metrics is not set, it would only do the log

// ...middlewares and routes

it uses n-logger by default, use custom logger instance, check example of setupMonitor with n-mask-logger

monitor

import { monitor } from '@financial-times/n-express-monitor';

const getUserProfileBySession = async (req, res) => {
    const { meta } = req;
    const { sessionId } = req.params;
    if (sessionId === 'uncovered') {
        throw Error('an uncovered function has thrown an error');
    }
    const { userId } = await SessionApi.verifySession({ sessionId }, meta);
    const userProfile = await UserProfileSvc.getUserProfileById({ userId }, meta);
    res.json(userProfile);
};

export default monitor(getUserProfileBySession);

monitorService

import { monitorService } from '@financial-times/n-express-monitor';

/*
    SHORTHAND DEFAULT: in case we don't need to add extra error handling,
    the default method from n-api-factory can be used to setup a client method
 */
const getUserProfileById = async ({ userId }, meta) =>
    userProfileSvc.get({
        endpoint: `/user-profile/${userId}`,
        meta,
    });

export default monitorService('user-profile-svc', {
    getUserProfileById,
});

monitorModule

import { monitorModule } from '@financial-times/n-express-monitor';

export default monitorModule({
    validateUserId: () => {},
    mapUserProfileToView: () => {},
});

Convention

operation function

same as express middleware/controller but without next: (req, res) => {}

action function

(param, meta) => {}

How It Works

If you are interested in how it works under the hood, it is using n-auto-logger, n-auto-metrics, and you can build your own customised decorator and add it into the chain with n-express-enhancer.

Licence

MIT

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.