Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

Financial-Times/n-express-monitor

Repository files navigation

n-express-monitor

a configurable express decorator to automate log, metrics to ensure monitor consistency and make debugging more predictable across micro-services

npm version CircleCI Coverage Status gitter chat FOSSA Status

node version 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: () => {},
});

Configuration

Apart from the configuration you need for the logger, metrics instance you used, you can config n-express-monitor with the following ENV_VAR for the log automation behaviour:

ENV_VAR values function
AUTO_LOG_LEVEL verbose, standard(default), concise, error control how operation and action would be logged, check example
LOGGER_MUTE_FIELDS key names of values in the meta or error use to reduce amount of log information

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

About

🎯 configurable express decorator to automate log, metrics for more consistent monitor and debugging across micro-services

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published