extend unittests, simplify API, update README, use npm5 and package-lock.json, add node7 to travis config

This commit is contained in:
Konstantin Pogorelov
2017-06-04 16:31:26 +02:00
parent 1cc588c2da
commit b8ba87009e
9 changed files with 1984 additions and 73 deletions

View File

@@ -1,3 +1,4 @@
language: node_js
node_js:
- "6"
- "7"

View File

@@ -4,27 +4,25 @@
Express middleware with popular prometheus metrics in one bundle. It's also compatible with koa v1 and v2 (see below).
Internally it uses **prom-client**. See: https://github.com/siimon/prom-client
Internally it uses **prom-client**. See: https://github.com/siimon/prom-client (^9.0.0)
Included metrics:
* `up`: normally is just 1
* `http_request_duration_seconds`: http latency histogram labeled with `status_code`, `method` and `path`
**Please note version 2.x is NOT backwards compatible with 1.x**
## Install
```
npm install express-prom-bundle
```
## Usage
## Sample uUsage
```javascript
const promBundle = require("express-prom-bundle");
const metricsMiddleware = promBundle({/* options */ });
const app = require("express")();
const metricsMiddleware = promBundle({includeMethod: true});
app.use(metricsMiddleware);
app.use(/* your middleware */);
@@ -44,17 +42,31 @@ See the example below.
## Options
* **buckets**: buckets used for `http_request_seconds` histogram
* **includeMethod**: include HTTP method (GET, PUT, ...) as a label to `http_request_duration_seconds`
* **includePath**: include URL path as a label (see below)
* **normalizePath**: boolean or `function(req)` - path normalization for `includePath` option
Which labels to include in `http_request_duration_seconds` metric:
* **includeStatusCode**: HTTP status code (200, 400, 404 etc.), default: **true**
* **includeMethod**: HTTP method (GET, PUT, ...), default: **false**
* **includePath**: URL path (see importent details below), default: **false**
Extra transformation callbacks:
* **normalizePath**: `function(req)` generates path values from express `req` (see details below)
* **formatStatusCode**: `function(res)` producing final status code from express `res` object, e.g. you can combine `200`, `201` and `204` to just `2xx`.
Other options:
* **buckets**: buckets used for `http_request_duration_seconds` histogram
* **autoregister**: if `/metrics` endpoint should be registered. (Default: **true**)
* **whitelist**, **blacklist**: array of strings or regexp specifying which metrics to include/exclude
* **excludeRoutes**: (deprecated) array of strings or regexp specifying which routes should be skipped for `http_request_duration_seconds` metric. It uses `req.originalUrl` as subject when checking. You want normally use express or meddleware features instead of this options.
Deprecated:
* **whitelist**, **blacklist**: array of strings or regexp specifying which metrics to include/exclude (there are only 2 metrics)
* **excludeRoutes**: array of strings or regexp specifying which routes should be skipped for `http_request_duration_seconds` metric. It uses `req.originalUrl` as subject when checking. You want to use express or meddleware features instead of this option.
### More details on includePath option
The goal is to have separate latency statistics by URL path, e.g. `/my-app/user/`, `/products/by-category` etc.
Let's say you want to have latency statistics by URL path,
e.g. separate metrics for `/my-app/user/`, `/products/by-category` etc.
Just taking `req.path` as a label value won't work as IDs are often part of the URL,
like `/user/12352/profile`. So what we actually need is a path template.
@@ -65,8 +77,6 @@ normalized to `/user/#val/profile` and that will become the value for the label.
You can override this magical behavior and define your own function by
providing an optional callback using **normalizePath** option.
You can also replace the default **normalizePath** function globally.
This is handy if the rest of the middleware is done elsewhere
e.g. via `kraken.js meddleware`.
```javascript
app.use(promBundle(/* options? */));
@@ -160,7 +170,9 @@ Here is meddleware config sample, which can be used in a standard **kraken.js**
* **3.0.0**
* upgrade dependencies, most notably **prom-client** to 9.0.0
* switch to koa v2 in koa unittest
* only node v6 is supported (stop supporting node v4 and v5)
* only node v6 or higher is supported (stop supporting node v4 and v5)
* switch to npm5 and use package-lock.json
* options added: includeStatusCode, formatStatusCode
* **2.1.0**
* deprecate **excludeRoutes**, use **req.originalUrl** instead of **req.path**
* **2.0.0**

1872
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -169,7 +169,7 @@ describe('index', () => {
});
});
it('normalizePath can be replaced', done => {
it('normalizePath can be replaced gloablly', done => {
const app = express();
const original = bundle.normalizePath;
bundle.normalizePath = () => 'dummy';
@@ -193,6 +193,70 @@ describe('index', () => {
});
});
it('normalizePath can be overridden', done => {
const app = express();
const instance = bundle({
includePath: true,
normalizePath: req => req.originalUrl + '-suffixed'
});
app.use(instance);
app.use('/test', (req, res) => res.send('it worked'));
const agent = supertest(app);
agent
.get('/test')
.end(() => {
agent
.get('/metrics')
.end((err, res) => {
expect(res.status).toBe(200);
expect(res.text).toMatch(/"\/test-suffixed"/m);
done();
});
});
});
it('formatStatusCode can be overridden', done => {
const app = express();
const instance = bundle({
formatStatusCode: () => 555
});
app.use(instance);
app.use('/test', (req, res) => res.send('it worked'));
const agent = supertest(app);
agent
.get('/test')
.end(() => {
agent
.get('/metrics')
.end((err, res) => {
expect(res.status).toBe(200);
expect(res.text).toMatch(/555/);
done();
});
});
});
it('includeStatusCode=false removes status_code label from metrics', done => {
const app = express();
const instance = bundle({
includeStatusCode: false
});
app.use(instance);
app.use('/test', (req, res) => res.send('it worked'));
const agent = supertest(app);
agent
.get('/test')
.end(() => {
agent
.get('/metrics')
.end((err, res) => {
expect(res.status).toBe(200);
expect(res.text).not.toMatch(/200/);
done();
});
});
});
it('Koa: metrics returns up=1', done => {
const app = new Koa();
const bundled = bundle({

View File

@@ -4,25 +4,8 @@
const normalizePath = require('../src/normalizePath');
describe('normalizePath', () => {
it('returns original if disabled in opts', () => {
expect(
normalizePath({originalUrl: '/a/12345'}, {normalizePath: false})
).toBe('/a/12345');
});
it('returns run callback if configured', () => {
expect(
normalizePath(
{originalUrl: '/a/12345'},
{
normalizePath: req => req.originalUrl + '-ok'
}
)
).toBe('/a/12345-ok');
});
it('uses UrlValueParser by default', () => {
expect(normalizePath({originalUrl: '/a/12345'}))
expect(normalizePath({url: '/a/12345'}))
.toBe('/a/#val');
});
});

View File

@@ -4,20 +4,9 @@
const normalizeStatusCode = require('../src/normalizeStatusCode');
describe('normalizeStatusCode', () => {
it('returns original if disabled in opts', () => {
expect(
normalizeStatusCode({status_code: 404}, {normalizeStatusCode: false})
).toBe(404);
});
it('returns run callback if configured', () => {
expect(
normalizeStatusCode(
{status_code: 500},
{
formatStatusCode: res => String(res.status_code).slice(0, -2) + 'xx'
}
)
).toBe('5xx');
normalizeStatusCode({status_code: 500})
).toBe(500);
});
});

View File

@@ -39,7 +39,15 @@ function prepareMetricNames(opts, metricTemplates) {
}
function main(opts) {
opts = Object.assign({autoregister: true}, opts);
opts = Object.assign(
{
autoregister: true,
includeStatusCode: true,
normalizePath: main.normalizePath,
formatStatusCode: main.normalizeStatusCode
},
opts
);
if (arguments[2] && arguments[1] && arguments[1].send) {
arguments[1].status(500)
.send('<h1>500 Error</h1>\n'
@@ -114,20 +122,17 @@ function main(opts) {
}
if (metrics[httpMtricName]) {
labels = {'status_code': 0};
labels = {};
let timer = metrics[httpMtricName].startTimer(labels);
onFinished(res, () => {
if (opts.normalizeStatusCode) {
labels.status_code = main.normalizeStatusCode(res, opts);
} else {
labels.status_code = res.statusCode;
if (opts.includeStatusCode) {
labels.status_code = opts.formatStatusCode(res, opts);
}
if (opts.includeMethod) {
labels.method = req.method;
}
if (opts.includePath) {
labels.path = main.normalizePath(req, opts);
labels.path = opts.normalizePath(req, opts);
}
timer();
});

View File

@@ -4,21 +4,12 @@ const UrlValueParser = require('url-value-parser');
const url = require('url');
let urlValueParser;
module.exports = function(req, opts) {
opts = opts || {};
module.exports = function(req) {
// originalUrl is taken, because url and path can be changed
// by middlewares such as 'router'. Note: this function is called onFinish
/// i.e. always in the tail of the middleware chain
const path = url.parse(req.originalUrl || req.url).pathname;
if (opts.normalizePath !== undefined && !opts.normalizePath) {
return path;
}
if (typeof opts.normalizePath === 'function') {
return opts.normalizePath(req, opts);
}
if (!urlValueParser) {
urlValueParser = new UrlValueParser();
}

View File

@@ -1,11 +1,5 @@
'use strict';
module.exports = function(res, opts) {
opts = opts || {};
if (typeof opts.formatStatusCode === 'function') {
return opts.formatStatusCode(res, opts);
}
return res.status_code;
module.exports = function(res) {
return res.status_code || res.statusCode;
};