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/controller
enhance 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-metrics
Gotcha
reserved fields
-
operation
default tooperationFunction.name
-
service
default toundefined
, can be specified inparamsOrArgs
ormeta
(autoLogActions('service-name')
) -
action
default tocallFunction.name
-
category
NError category would be recorded in metrics -
type
is used by convention to record custom error type names for monitoring and debugging -
status
in error object would be recorded for service action call failure -
stack
used in Error or NError to store the stack trace -
result
default tosuccess/failure
metrics format
operation.${operation}.segment.${segment}.state.start
operation.${operation}.action.${action}.state.start
operation.${operation}.action.${action}.state.success
operation.${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.success
operation.${operation}.segment.${segment}.state.failure.category.${e.category}.type.${e.type}
service.${service}.action.${action}.state.start
service.${service}.action.${action}.state.success
service.${service}.action.${action}.state.failure.category.${e.category}.type.${e.type}
service.${service}.action.${action}.state.failure.category.${e.category}.status.${e.status}