@financial-times/n-auto-metrics
v4.0.0-beta.2
Published
an [enhancer](https://github.com/Financial-Times/n-express-enhancer) to auto record metrics of function calls in operation/action model with a single line of code
Maintainers
Keywords
Readme
n-auto-metrics
an enhancer to auto record metrics of function calls in operation/action model with a single line of code
Quickstart
initialise metrics before using enhanced middleware
/* app.js */
import { metrics } from 'n-express'; // or any other source has the `next-metrics` instance
import { initAutoMetrics } from '@financial-times/n-auto-metrics';
initAutoMetrics(metrics); // do this before app.use() any enhanced middleware/controllerenhance action function to auto metrics, use tagService to add service before metricsAction if you want to set metrics of one service namespace
import { metricsAction, tagService, compose } from '@financial-times/n-auto-metrics';
// auto metrics function of its start, success/failure state
const result = metricsAction(someFunction)(args: Object, meta?: Object);
// auto metrics multiple functions wrapped in an object
const APIService = compose(
tagService('some-service'),
metricsAction
)({
methodA,
methodB,
methodC
});more details on action function
// auto log success/failure express middleware/controller as an operation function
// function name would be logged as `operation`, and available in meta
const operationFunction = (meta, req, res) => { /* try-catch-throw */ };
export default toMiddleware(metricsOperation(operationFunction));
// auto log multiple operation functions wrapped in an object as controller
const someController = toMiddlewares(metricsOperation({ operationFunctionA, operationFuncitonB }));check use res.render
more details on operation function
more details on chain with other enhancers
Install
npm install @financial-times/n-auto-metricsGotcha
reserved fields
operationdefault tooperationFunction.nameservicedefault toundefined, can be specified inparamsOrArgsormeta(autoLogActions('service-name'))actiondefault tocallFunction.namecategoryNError category would be recorded in metricstypeis used by convention to record custom error type names for monitoring and debuggingstatusin error object would be recorded for service action call failurestackused in Error or NError to store the stack traceresultdefault tosuccess/failure
metrics format
operation.${operation}.segment.${segment}.state.startoperation.${operation}.action.${action}.state.startoperation.${operation}.action.${action}.state.successoperation.${operation}.action.${action}.state.failure.category.${e.category}.type.${e.type}operation.${operation}.action.${action}.state.failure.category.${e.category}.status.${e.status}operation.${operation}.segment.${segment}.state.successoperation.${operation}.segment.${segment}.state.failure.category.${e.category}.type.${e.type}service.${service}.action.${action}.state.startservice.${service}.action.${action}.state.successservice.${service}.action.${action}.state.failure.category.${e.category}.type.${e.type}service.${service}.action.${action}.state.failure.category.${e.category}.status.${e.status}

