4.0.0-beta.2 • Published 6 years ago

@financial-times/n-auto-metrics v4.0.0-beta.2

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

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

4.0.0-beta.2

6 years ago

4.0.0-beta.1

6 years ago

3.5.1

6 years ago

3.5.0

6 years ago

3.4.5

6 years ago

3.4.4

6 years ago

3.4.3

6 years ago

3.4.2

6 years ago

3.4.0

6 years ago

3.3.1

6 years ago

3.3.0

6 years ago

3.2.0

6 years ago

3.1.0

6 years ago

3.0.0

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.1.7

6 years ago

1.1.6

6 years ago

1.0.2

6 years ago