express-slas
SLA's (Service Level Agreement) handler for Express.JS
What does it do?
Set a max time for your API and this middleware will report back if the response time goes over it. It does not cancel the request, it just
logs an error by default (using console.error
).
0 dependencies, all native, all fast.
You can pass a callback to perform custom actions such as sending notifications to slack / hipchat / JIRA tickets. This callback will be executed asynchronously, you don't have to be worried about it delaying the response even more.
As per Express nature, this middleware will evaluate the time between the time when the request comest to it and then when the headers are set.
Note Remember that you can, and maybe you you should, apply this middleware to those routes that actually require to meet certain SLA (like get by id operations). That is not implmented in the library intentionally since express already supports that functionality out of the box (for instance http://stackoverflow.com/questions/15877342/nodejs-express-apply-session-middleware-to-some-routes).
Usage
Install it:
npm install -S express-slas
Declare it and use it
var slas = ;// Assuming 'app' is an express objectapp;
Note Don't forget that for more accurate results this middleware should be loaded before all other middleware (except you really require to load something before i.e. security reasons).
Full example:
'use strict';var express = app = slas = ; var { // Notify monitoring system}; // Setting a SLA of 100ms, disabling default error message and passing a callbackapp; app; app;
Options
Options are passed in the initialization as an object with the following properties:
sla (time in milliseconds, default 1000ms, optional)
Time in milliseconds.
logError (boolean, default true, optional)
Logs an error message to the console using console.error
cb (callback, optional)
Custom callback that will be executed asynchronously after the response is sent to the client. This callback is only executed if the SLA is not met. It passes the time spent as an argument.
disableFor (time in seconds, default 10, optional)
Defines a pause time after a SLA is not met, otherwise the library could cause a log flood with all the messages making the situation even worse. By default, it waits for 10 seconds before starting to check again.