under-pressure
Advanced tools
Comparing version 6.0.0 to 6.1.0
264
index.js
'use strict' | ||
const fe = require('@fastify/error') | ||
const fp = require('fastify-plugin') | ||
const assert = require('assert') | ||
const { monitorEventLoopDelay } = require('perf_hooks') | ||
const { eventLoopUtilization } = require('perf_hooks').performance | ||
const warning = require('process-warning')() | ||
warning.create('FastifyWarning.under-pressure', 'FST_MODULE_DEP_under-pressure'.toUpperCase(), 'under-pressure has been deprecated. Use @fastify/under-pressure@7.0.0 instead.') | ||
warning.emit('FST_MODULE_DEP_under-pressure'.toUpperCase()) | ||
const SERVICE_UNAVAILABLE = 503 | ||
const createError = (msg = 'Service Unavailable') => fe('FST_UNDER_PRESSURE', msg, SERVICE_UNAVAILABLE) | ||
const TYPE_EVENT_LOOP_DELAY = 'eventLoopDelay' | ||
const TYPE_HEAP_USED_BYTES = 'heapUsedBytes' | ||
const TYPE_RSS_BYTES = 'rssBytes' | ||
const TYPE_HEALTH_CHECK = 'healthCheck' | ||
const TYPE_EVENT_LOOP_UTILIZATION = 'eventLoopUtilization' | ||
function getSampleInterval (value, eventLoopResolution) { | ||
const defaultValue = monitorEventLoopDelay ? 1000 : 5 | ||
const sampleInterval = value || defaultValue | ||
return monitorEventLoopDelay ? Math.max(eventLoopResolution, sampleInterval) : sampleInterval | ||
} | ||
async function underPressure (fastify, opts) { | ||
opts = opts || {} | ||
const resolution = 10 | ||
const sampleInterval = getSampleInterval(opts.sampleInterval, resolution) | ||
const maxEventLoopDelay = opts.maxEventLoopDelay || 0 | ||
const maxHeapUsedBytes = opts.maxHeapUsedBytes || 0 | ||
const maxRssBytes = opts.maxRssBytes || 0 | ||
const healthCheck = opts.healthCheck || false | ||
const healthCheckInterval = opts.healthCheckInterval || -1 | ||
const UnderPressureError = opts.customError || createError(opts.message) | ||
const maxEventLoopUtilization = opts.maxEventLoopUtilization || 0 | ||
const pressureHandler = opts.pressureHandler | ||
const checkMaxEventLoopDelay = maxEventLoopDelay > 0 | ||
const checkMaxHeapUsedBytes = maxHeapUsedBytes > 0 | ||
const checkMaxRssBytes = maxRssBytes > 0 | ||
const checkMaxEventLoopUtilization = eventLoopUtilization ? maxEventLoopUtilization > 0 : false | ||
let heapUsed = 0 | ||
let rssBytes = 0 | ||
let eventLoopDelay = 0 | ||
let lastCheck | ||
let histogram | ||
let elu | ||
let eventLoopUtilized = 0 | ||
if (monitorEventLoopDelay) { | ||
histogram = monitorEventLoopDelay({ resolution }) | ||
histogram.enable() | ||
} else { | ||
lastCheck = now() | ||
} | ||
if (eventLoopUtilization) { | ||
elu = eventLoopUtilization() | ||
} | ||
fastify.decorate('memoryUsage', memoryUsage) | ||
const timer = setInterval(updateMemoryUsage, sampleInterval) | ||
timer.unref() | ||
let externalsHealthy = false | ||
let externalHealthCheckTimer | ||
if (healthCheck) { | ||
assert(typeof healthCheck === 'function', 'opts.healthCheck should be a function that returns a promise that resolves to true or false') | ||
assert(healthCheckInterval > 0 || opts.exposeStatusRoute, 'opts.healthCheck requires opts.healthCheckInterval or opts.exposeStatusRoute') | ||
const doCheck = async () => { | ||
try { | ||
externalsHealthy = await healthCheck(fastify) | ||
} catch (error) { | ||
externalsHealthy = false | ||
fastify.log.error({ error }, 'external healthCheck function supplied to `under-pressure` threw an error. setting the service status to unhealthy.') | ||
} | ||
} | ||
await doCheck() | ||
if (healthCheckInterval > 0) { | ||
externalHealthCheckTimer = setInterval(doCheck, healthCheckInterval) | ||
externalHealthCheckTimer.unref() | ||
} | ||
} else { | ||
externalsHealthy = true | ||
} | ||
fastify.addHook('onClose', onClose) | ||
opts.exposeStatusRoute = mapExposeStatusRoute(opts.exposeStatusRoute) | ||
if (opts.exposeStatusRoute) { | ||
fastify.route({ | ||
...opts.exposeStatusRoute.routeOpts, | ||
url: opts.exposeStatusRoute.url, | ||
method: 'GET', | ||
schema: Object.assign({}, opts.exposeStatusRoute.routeSchemaOpts, { | ||
response: { | ||
200: { | ||
type: 'object', | ||
properties: Object.assign( | ||
{ status: { type: 'string' } }, | ||
opts.exposeStatusRoute.routeResponseSchemaOpts | ||
) | ||
} | ||
} | ||
}), | ||
handler: onStatus | ||
}) | ||
} | ||
if (checkMaxEventLoopUtilization === false && checkMaxEventLoopDelay === false && | ||
checkMaxHeapUsedBytes === false && | ||
checkMaxRssBytes === false && | ||
healthCheck === false) { | ||
return | ||
} | ||
const underPressureError = new UnderPressureError() | ||
const retryAfter = opts.retryAfter || 10 | ||
fastify.addHook('onRequest', onRequest) | ||
function mapExposeStatusRoute (opts) { | ||
if (!opts) { | ||
return false | ||
} | ||
if (typeof opts === 'string') { | ||
return { url: opts } | ||
} | ||
return Object.assign({ url: '/status' }, opts) | ||
} | ||
function updateEventLoopDelay () { | ||
if (histogram) { | ||
eventLoopDelay = Math.max(0, histogram.mean / 1e6 - resolution) | ||
if (Number.isNaN(eventLoopDelay)) eventLoopDelay = Infinity | ||
histogram.reset() | ||
} else { | ||
const toCheck = now() | ||
eventLoopDelay = Math.max(0, toCheck - lastCheck - sampleInterval) | ||
lastCheck = toCheck | ||
} | ||
} | ||
function updateEventLoopUtilization () { | ||
if (elu) { | ||
eventLoopUtilized = eventLoopUtilization(elu).utilization | ||
} else { | ||
eventLoopUtilized = 0 | ||
} | ||
} | ||
function updateMemoryUsage () { | ||
const mem = process.memoryUsage() | ||
heapUsed = mem.heapUsed | ||
rssBytes = mem.rss | ||
updateEventLoopDelay() | ||
updateEventLoopUtilization() | ||
} | ||
function onRequest (req, reply, next) { | ||
if (checkMaxEventLoopDelay && eventLoopDelay > maxEventLoopDelay) { | ||
handlePressure(req, reply, next, TYPE_EVENT_LOOP_DELAY, eventLoopDelay) | ||
return | ||
} | ||
if (checkMaxHeapUsedBytes && heapUsed > maxHeapUsedBytes) { | ||
handlePressure(req, reply, next, TYPE_HEAP_USED_BYTES, heapUsed) | ||
return | ||
} | ||
if (checkMaxRssBytes && rssBytes > maxRssBytes) { | ||
handlePressure(req, reply, next, TYPE_RSS_BYTES, rssBytes) | ||
return | ||
} | ||
if (!externalsHealthy) { | ||
handlePressure(req, reply, next, TYPE_HEALTH_CHECK) | ||
return | ||
} | ||
if (checkMaxEventLoopUtilization && eventLoopUtilized > maxEventLoopUtilization) { | ||
handlePressure(req, reply, next, TYPE_EVENT_LOOP_UTILIZATION, eventLoopUtilized) | ||
return | ||
} | ||
next() | ||
} | ||
function handlePressure (req, reply, next, type, value) { | ||
if (typeof pressureHandler === 'function') { | ||
const result = pressureHandler(req, reply, type, value) | ||
if (result instanceof Promise) { | ||
result.then(() => next(), next) | ||
} else if (result == null) { | ||
next() | ||
} else { | ||
reply.send(result) | ||
} | ||
} else { | ||
reply.status(SERVICE_UNAVAILABLE).header('Retry-After', retryAfter) | ||
next(underPressureError) | ||
} | ||
} | ||
function memoryUsage () { | ||
return { | ||
eventLoopDelay, | ||
rssBytes, | ||
heapUsed, | ||
eventLoopUtilized | ||
} | ||
} | ||
async function onStatus (req, reply) { | ||
const okResponse = { status: 'ok' } | ||
if (healthCheck) { | ||
try { | ||
const checkResult = await healthCheck(fastify) | ||
if (!checkResult) { | ||
req.log.error('external health check failed') | ||
reply.status(SERVICE_UNAVAILABLE).header('Retry-After', retryAfter) | ||
throw underPressureError | ||
} | ||
return Object.assign(okResponse, checkResult) | ||
} catch (err) { | ||
req.log.error({ err }, 'external health check failed with error') | ||
reply.status(SERVICE_UNAVAILABLE).header('Retry-After', retryAfter) | ||
throw underPressureError | ||
} | ||
} | ||
return okResponse | ||
} | ||
function onClose (fastify, done) { | ||
clearInterval(timer) | ||
clearInterval(externalHealthCheckTimer) | ||
done() | ||
} | ||
} | ||
function now () { | ||
const ts = process.hrtime() | ||
return (ts[0] * 1e3) + (ts[1] / 1e6) | ||
} | ||
module.exports = fp(underPressure, { | ||
fastify: '4.x', | ||
name: 'under-pressure' | ||
}) | ||
module.exports.TYPE_EVENT_LOOP_DELAY = TYPE_EVENT_LOOP_DELAY | ||
module.exports.TYPE_EVENT_LOOP_UTILIZATION = TYPE_EVENT_LOOP_UTILIZATION | ||
module.exports.TYPE_HEALTH_CHECK = TYPE_HEALTH_CHECK | ||
module.exports.TYPE_HEAP_USED_BYTES = TYPE_HEAP_USED_BYTES | ||
module.exports.TYPE_RSS_BYTES = TYPE_RSS_BYTES | ||
module.exports = require('under-pressure-deprecated') |
{ | ||
"name": "under-pressure", | ||
"version": "6.0.0", | ||
"description": "Measure process load with automatic handling of 'Service Unavailable' plugin for Fastify.", | ||
"version": "6.1.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"lint": "standard | snazzy", | ||
"unit": "tap -j 1 test/*test.js", | ||
"test": "npm run lint && npm run unit && npm run typescript", | ||
"typescript": "tsd" | ||
}, | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/fastify/under-pressure.git" | ||
"url": "git://github.com/fastify/under-pressure.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/fastify/under-pressure/issues" | ||
}, | ||
"keywords": [ | ||
"fastify", | ||
"service unavailable", | ||
"limit", | ||
"delay", | ||
"retry" | ||
], | ||
"author": "Tomas Della Vedova - @delvedor (http://delved.org)", | ||
"license": "MIT", | ||
"homepage": "https://github.com/fastify/under-pressure", | ||
"dependencies": { | ||
"@fastify/error": "^2.0.0", | ||
"fastify-plugin": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^17.0.7", | ||
"fastify": "^4.0.0-rc.2", | ||
"pre-commit": "^1.2.2", | ||
"semver": "^7.3.2", | ||
"simple-get": "^4.0.0", | ||
"snazzy": "^9.0.0", | ||
"standard": "^17.0.0", | ||
"tap": "^16.2.0", | ||
"tsd": "^0.20.0", | ||
"typescript": "^4.0.3" | ||
"process-warning": "^1.0.0", | ||
"under-pressure-deprecated": "npm:under-pressure@6.0.0" | ||
} | ||
} |
230
README.md
# under-pressure | ||
![CI](https://github.com/fastify/under-pressure/workflows/CI/badge.svg) | ||
[![NPM version](https://img.shields.io/npm/v/under-pressure.svg?style=flat)](https://www.npmjs.com/package/under-pressure) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/fastify/under-pressure/badge.svg)](https://snyk.io/test/github/fastify/under-pressure) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/) | ||
Measure process load with automatic handling of *"Service Unavailable"* plugin for Fastify. | ||
It can check `maxEventLoopDelay`, `maxHeapUsedBytes`, `maxRssBytes` and `maxEventLoopUtilization` values. | ||
You can also specify a custom health check, to verify the status of | ||
external resources. | ||
<a name="requirements"></a> | ||
## Requirements | ||
Fastify ^2.0.0. Please refer to [this branch](https://github.com/fastify/under-pressure/tree/1.x) and related versions for Fastify ^1.1.0 compatibility. | ||
<a name="install"></a> | ||
## Install | ||
``` | ||
npm i under-pressure --save | ||
``` | ||
<a name="usage"></a> | ||
## Usage | ||
Require the plugin and register it into the Fastify instance. | ||
```js | ||
const fastify = require('fastify')() | ||
fastify.register(require('under-pressure'), { | ||
maxEventLoopDelay: 1000, | ||
maxHeapUsedBytes: 100000000, | ||
maxRssBytes: 100000000, | ||
maxEventLoopUtilization:0.98 | ||
}) | ||
fastify.get('/', (req, reply) => { | ||
reply.send({ hello: 'world'}) | ||
}) | ||
fastify.listen(3000, err => { | ||
if (err) throw err | ||
console.log(`server listening on ${fastify.server.address().port}`) | ||
}) | ||
``` | ||
`under-pressure` will automatically handle for you the `Service Unavailable` error once one of the thresholds has been reached. | ||
You can configure the error message and the `Retry-After` header. | ||
```js | ||
fastify.register(require('under-pressure'), { | ||
maxEventLoopDelay: 1000, | ||
message: 'Under pressure!', | ||
retryAfter: 50 | ||
}) | ||
``` | ||
You can also configure custom Error instance `under-pressure` will throw. | ||
```js | ||
class CustomError extends Error { | ||
constructor () { | ||
super('Custom error message') | ||
Error.captureStackTrace(this, CustomError) | ||
} | ||
} | ||
fastify.register(require('under-pressure'), { | ||
maxEventLoopDelay: 1000, | ||
customError: CustomError | ||
}) | ||
``` | ||
The default value for `maxEventLoopDelay`, `maxHeapUsedBytes`, `maxRssBytes` and `maxEventLoopUtilization` is `0`. | ||
If the value is `0` the check will not be performed. | ||
Since [`eventLoopUtilization`](https://nodejs.org/api/perf_hooks.html#perf_hooks_performance_eventlooputilization_utilization1_utilization2) is only available in Node version 14.0.0 and 12.19.0 the check will be disabled in other versions. | ||
Thanks to the encapsulation model of Fastify, you can selectively use this plugin in some subset of routes or even with different thresholds in different plugins. | ||
#### `memoryUsage` | ||
This plugin also exposes a function that will tell you the current values of `heapUsed`, `rssBytes`, `eventLoopDelay` and `eventLoopUtilized`. | ||
```js | ||
console.log(fastify.memoryUsage()) | ||
``` | ||
#### Pressure Handler | ||
You can provide a pressure handler in the options to handle the pressure errors. The advantage is that you know why the error occurred. Moreover, the request can be handled as if nothing happened. | ||
```js | ||
const fastify = require('fastify')() | ||
const underPressure = require('under-pressure')() | ||
fastify.register(underPressure, { | ||
maxHeapUsedBytes: 100000000, | ||
maxRssBytes: 100000000, | ||
pressureHandler: (req, rep, type, value) => { | ||
if (type === underPressure.TYPE_HEAP_USED_BYTES) { | ||
fastify.log.warn(`too many heap bytes used: ${value}`) | ||
} else if (type === underPressure.TYPE_RSS_BYTES) { | ||
fastify.log.warn(`too many rss bytes used: ${value}`) | ||
} | ||
rep.send('out of memory') // if you omit this line, the request will be handled normally | ||
} | ||
}) | ||
``` | ||
It is possible as well to return a Promise that will call `reply.send` (or something else). | ||
```js | ||
fastify.register(underPressure, { | ||
maxHeapUsedBytes: 100000000, | ||
pressureHandler: (req, rep, type, value) => { | ||
return getPromise().then(() => reply.send({hello: 'world'})) | ||
} | ||
}) | ||
``` | ||
Any other return value than a promise or nullish will be sent to client with `reply.send`. | ||
#### Status route | ||
If needed you can pass `{ exposeStatusRoute: true }` and `under-pressure` will expose a `/status` route for you that sends back a `{ status: 'ok' }` object. This can be useful if you need to attach the server to an ELB on AWS for example. | ||
If you need the change the exposed route path, you can pass `{ exposeStatusRoute: '/alive' }` options. | ||
To configure the endpoint more specifically you can pass an object. This consists of | ||
- *routeOpts* - Any Fastify [route options](https://www.fastify.io/docs/latest/Reference/Routes/#routes-options) except `schema` | ||
- *routeSchemaOpts* - As per the Fastify route options, an object containing the schema for request | ||
- *routeResponseSchemaOpts* - An object containing the schema for additional response items to be merged with the default response schema, see below | ||
- *url* - The URL to expose the status route on | ||
```js | ||
fastify.register(require('under-pressure'), { | ||
maxEventLoopDelay: 1000, | ||
exposeStatusRoute: { | ||
routeOpts: { | ||
logLevel: 'debug', | ||
config: { | ||
someAttr: 'value' | ||
} | ||
}, | ||
routeSchemaOpts: { // If you also want to set a custom route schema | ||
hide: true | ||
}, | ||
url: '/alive' // If you also want to set a custom route path and pass options | ||
} | ||
}) | ||
``` | ||
The above example will set the `logLevel` value for the `/alive` route to be `debug`. | ||
If you need to return other information in the response, you can return an object from the `healthCheck` function (see next paragraph) and use the `routeResponseSchemaOpts` property to describe your custom response schema (**note**: `status` will always be present in the response) | ||
```js | ||
fastify.register(underPressure, { | ||
... | ||
exposeStatusRoute: { | ||
routeResponseSchemaOpts: { | ||
extraValue: { type: 'string' }, | ||
metrics: { | ||
type: 'object', | ||
properties: { | ||
eventLoopDelay: { type: 'number' }, | ||
rssBytes: { type: 'number' }, | ||
heapUsed: { type: 'number' }, | ||
eventLoopUtilized: { type: 'number' }, | ||
}, | ||
}, | ||
// ... | ||
} | ||
}, | ||
healthCheck: async (fastifyInstance) => { | ||
return { | ||
extraValue: await getExtraValue(), | ||
metrics: fastifyInstance.memoryUsage(), | ||
// ... | ||
} | ||
}, | ||
} | ||
``` | ||
#### Custom health checks | ||
If needed you can pass a custom `healthCheck` property, which is an async function, and `under-pressure` will allow you to check the status of other components of your service. | ||
This function should return a promise that resolves to a boolean value or to an object. The `healthCheck` function can be called either: | ||
* every X milliseconds, the time can be | ||
configured with the `healthCheckInterval` option. | ||
* every time the status route is called, if `exposeStatusRoute` is set | ||
to `true`. | ||
By default when this function is supplied your service health is considered unhealthy, until it has started to return true. | ||
```js | ||
const fastify = require('fastify')() | ||
fastify.register(require('under-pressure'), { | ||
healthCheck: async function (fastifyInstance) { | ||
// do some magic to check if your db connection is healthy, etc... | ||
return true | ||
}, | ||
healthCheckInterval: 500 | ||
}) | ||
``` | ||
<a name="sample-interval"></a> | ||
#### Sample interval | ||
You can set a custom value for sampling the metrics returned by `memoryUsage` using the `sampleInterval` option, which accepts a number that represents the interval in milliseconds. | ||
The default value is different depending on which Node version is used. In version 8 and 10 it is `5`, while on version 11.10.0 and up it is `1000`. This difference is because from version 11.10.0 the event loop delay can be sampled with [`monitorEventLoopDelay`](https://nodejs.org/docs/latest-v12.x/api/perf_hooks.html#perf_hooks_perf_hooks_monitoreventloopdelay_options) and this allows to increase the interval value. | ||
```js | ||
const fastify = require('fastify')() | ||
fastify.register(require('under-pressure'), { | ||
sampleInterval: <your custom sample interval in ms> | ||
}) | ||
``` | ||
<a name="acknowledgements"></a> | ||
## Acknowledgements | ||
This project is kindly sponsored by [LetzDoIt](https://www.letzdoitapp.com/). | ||
<a name="license"></a> | ||
## License | ||
Licensed under [MIT](./LICENSE). | ||
`under-pressure@6.1.0` has been deprecated. Please use | ||
`@fastify/under-pressure@7.0.0` instead. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
0
0
1
817
3
5
2
5
3
+ Addedprocess-warning@^1.0.0
+ Addedprocess-warning@1.0.0(transitive)
+ Addedunder-pressure@6.0.0(transitive)
- Removed@fastify/error@^2.0.0
- Removedfastify-plugin@^3.0.0