项目作者: bbc

项目描述 :
An http-transport plugin that adds stats information about the upstream request/response, errors, cache events etc. This plugin can be used in combination with CloudWatch EMF.
高级语言: JavaScript
项目地址: git://github.com/bbc/http-transport-emf-stats.git
创建时间: 2020-07-15T08:18:55Z
项目社区:https://github.com/bbc/http-transport-emf-stats

开源协议:Apache License 2.0

下载


BBC

HTTP transport EMF stats

EMF stats is a plugin that can be used by http-transport to return a stats object alongside the HTTP response.

Installation

  1. npm install @bbc/http-transport-emf-stats

or

  1. yarn add @bbc/http-transport-emf-stats

Usage

  1. import HttpTransport from '@bbc/http-transport';
  2. const client = HttpTransport
  3. .createBuilder()
  4. .userAgent('...')
  5. .retries(...)
  6. .retryDelay(...)
  7. .use(stats())
  8. .createClient();

With the cache enabled

Used in conjunction with @bbc/http-transport-cache

  1. import HttpTransport from '@bbc/http-transport';
  2. import { maxAge, staleIfError } from '@bbc/http-transport-cache';
  3. const catbox = ...; // a catbox instance
  4. const cacheOpts = {
  5. name: 'rms',
  6. ...
  7. ...
  8. }
  9. const client = HttpTransport
  10. .createBuilder()
  11. .userAgent('...')
  12. .retries(...)
  13. .retryDelay(...)
  14. .use(stats())
  15. .use(maxAge(catbox, cacheOpts))
  16. .use(staleIfError(catbox, cacheOpts))
  17. .createClient();

Stats

The stats object is structured as following:

  1. {
  2. metrics: {
  3. attemptCount: 0, // The number of request attempts in total. This number is equal to "attempts.length".
  4. retryCount: 0, // The number of retried attempts. This number is equal to "attemptCount - 1".
  5. requestCount: 0, // The number of times a request is sent to and a response is received from the upstream
  6. requestErrorCount: 0, // The number of times the upstream service doesn't respond
  7. response5xxCount: 0, // The number of 5xx responses
  8. response4xxCount: 0, // The number of 4xx responses
  9. response3xxCount: 0, // The number of 3XX responses
  10. response2xxCount: 0, // The number of 2XX responses
  11. response1xxCount: 0, // The number of 1XX responses
  12. responseInvalidCount: 0, // The number of invalid response statuses
  13. responseTime: 0,
  14. cacheHitCount: 0,
  15. cacheMissCount: 0,
  16. cacheStaleCount: 0,
  17. cacheErrorCount: 0,
  18. cacheTimeoutCount: 0,
  19. cacheConnectionErrorCount: 0
  20. },
  21. cacheAudit: [] // A list of cache events triggered by the cache plugin. It is used for troubleshooting purposes only.
  22. }

The stats object is set within res when the request resolves or in the error object when it rejects. If you want to access the stats info do as following:

  1. try {
  2. const response = await client.get(uri).asResponse();
  3. const { stats } = response.res;
  4. // can access stats data
  5. } catch (error) {
  6. const { stats } = error;
  7. // can access stats data
  8. }

Testing and linting

To run unit tests and coverage reports

  1. npm run test

or just tests without coverage report

  1. npm run test:only

To run code linting

  1. npm run lint