express-slas

1.1.1 • Public • Published

Build Status Dependencies Status Codacy Badge Node

NPM

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 = require("express-slas");
// Assuming 'app' is an express object
app.use(slas());

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 = require("express"),
    app = express(),
    slas = require("express-slas");
    
var monitor = function(t){
    // Notify monitoring system
};
 
// Setting a SLA of 100ms, disabling default error message and passing a callback
app.use( slas({sla: 100, logError: false, cb: monitor}) );
 
app.get("/", function(req, res){
    res.send("hello world");
});
 
app.listen(8080, function(){
   console.log("app started"); 
});

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.

Package Sidebar

Install

npm i express-slas

Weekly Downloads

1

Version

1.1.1

License

GPL-3.0

Last publish

Collaborators

  • elfido