a configurable express decorator to automate log, metrics to ensure monitor consistency and make debugging more predictable across micro-services
npm install @financial-times/n-express-monitor --save
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
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);
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,
});
import { monitorModule } from '@financial-times/n-express-monitor';
export default monitorModule({
validateUserId: () => {},
mapUserProfileToView: () => {},
});
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 |
same as express middleware/controller but without next: (req, res) => {}
(param, meta) => {}
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.