@financial-times/n-auto-metrics

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

Stats

StarsIssuesVersionUpdatedCreatedSize
@financial-times/n-auto-metrics
244.0.0-beta.23 years ago4 years agoMinified + gzip package size for @financial-times/n-auto-metrics in KB

Readme

n-auto-metrics

an enhancer to auto record metrics of function calls in operation/action model with a single line of code

npm version npm download node version

CircleCI Coverage Status Known Vulnerabilities Scrutinizer Code Quality Dependencies devDependencies



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 to operationFunction.name
  • service default to undefined, can be specified in paramsOrArgs or meta(autoLogActions('service-name'))
  • action default to callFunction.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 to success/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}

Licence

MIT

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.