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.
EMF stats is a plugin that can be used by http-transport to return a stats object alongside the HTTP response.
npm install @bbc/http-transport-emf-stats
or
yarn add @bbc/http-transport-emf-stats
import HttpTransport from '@bbc/http-transport';
const client = HttpTransport
.createBuilder()
.userAgent('...')
.retries(...)
.retryDelay(...)
.use(stats())
.createClient();
With the cache enabled
Used in conjunction with @bbc/http-transport-cache
import HttpTransport from '@bbc/http-transport';
import { maxAge, staleIfError } from '@bbc/http-transport-cache';
const catbox = ...; // a catbox instance
const cacheOpts = {
name: 'rms',
...
...
}
const client = HttpTransport
.createBuilder()
.userAgent('...')
.retries(...)
.retryDelay(...)
.use(stats())
.use(maxAge(catbox, cacheOpts))
.use(staleIfError(catbox, cacheOpts))
.createClient();
The stats object is structured as following:
{
metrics: {
attemptCount: 0, // The number of request attempts in total. This number is equal to "attempts.length".
retryCount: 0, // The number of retried attempts. This number is equal to "attemptCount - 1".
requestCount: 0, // The number of times a request is sent to and a response is received from the upstream
requestErrorCount: 0, // The number of times the upstream service doesn't respond
response5xxCount: 0, // The number of 5xx responses
response4xxCount: 0, // The number of 4xx responses
response3xxCount: 0, // The number of 3XX responses
response2xxCount: 0, // The number of 2XX responses
response1xxCount: 0, // The number of 1XX responses
responseInvalidCount: 0, // The number of invalid response statuses
responseTime: 0,
cacheHitCount: 0,
cacheMissCount: 0,
cacheStaleCount: 0,
cacheErrorCount: 0,
cacheTimeoutCount: 0,
cacheConnectionErrorCount: 0
},
cacheAudit: [] // A list of cache events triggered by the cache plugin. It is used for troubleshooting purposes only.
}
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:
try {
const response = await client.get(uri).asResponse();
const { stats } = response.res;
// can access stats data
} catch (error) {
const { stats } = error;
// can access stats data
}
To run unit tests and coverage reports
npm run test
or just tests without coverage report
npm run test:only
To run code linting
npm run lint