n-express-monitor
a configurable express decorator to automate log, metrics to enhance debug and monitor experience
Install
npm install @financial-times/n-express-monitor --save
Demo
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.