Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

Financial-Times/n-auto-metrics

Repository files navigation

n-auto-metrics

a metrics decorator to automate function metrics in operation/action model to monitor system and upstream services as well as business metrics

It has been consolidated into n-express-monitor, please use that instead unless you're curious about things under the hood or want to customise your own tool chain

npm version npm download node version

CircleCI Coverage Status Known Vulnerabilities Scrutinizer Code Quality Dependencies devDependencies



Install

npm i @financial-times/n-auto-metrics --save

Usage

setup

/* 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

decorate functions

A top level execution is categorised as an Operation, this can be an express middleware or controller function. Any lower level execution is categorised as an Action, and a two-level model of operation-action is encouraged.

With different log level settings, it would log the start, success/failure status of the function execution, function names to scope the operation/action, description of the error and params you need to recreate the error.

import { compose, autoNext, metricsOperation } from '@financial-times/n-auto-metrics';

const operation = (req, res) => {
  //let the error to be thrown
};

export default compose(
  autoNext,
  metricsOperation,
)(operation);
import { metricsAction } from '@financial-times/n-auto-metrics';

const action = (params: Object, meta: Object) => {}; // the function signature needs to follow the convention

export default metricsAction(action);
const operation = ({ meta }, res, next) => {
  action(param, meta); // pass the meta object from req.meta to thread operation/action
  //...
};

want even less lines of code?

Gotcha

the Metrics

for business metrics:

operation.${operation}.segment.${segment}.state.start operation.${operation}.segment.${segment}.state.success operation.${operation}.segment.${segment}.state.failure.category.${e.category}.type.${e.type}

for system reliability:

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}

for upstream service reliability:

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}

the Meta

check the common meta used by n-auto-logger

meta.segment would be picked up in the metrics to help measure business metrics with user segment breakdown.

Licence

MIT

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published