Compare commits

...

93 Commits
2.1.0 ... 5.1.4

Author SHA1 Message Date
Konstantin Pogorelov
b00d88c4a7 bump 5.1.4 2019-04-16 13:13:12 +02:00
Konstantin Pogorelov
11988cb025 Merge pull request #37 from cerodriguezl/fix-ts-definition
Fix the definition of the Opts interface
2019-04-16 13:12:39 +02:00
Konstantin Pogorelov
df6854ee04 Merge pull request #36 from gabordobrei/master
make transformLabels optional in index.d.ts
2019-04-16 13:08:14 +02:00
Carlos Rodriguez
48d883b7f2 Fix the definition of the Opts interface
The `buckets` property was defined as a `Tuple` (`[number]`) instead of an Array
(`number[]`).
2019-04-15 18:21:35 -05:00
Gábor Döbrei
b835e90560 make transformLabels optional in index.d.ts 2019-04-15 15:53:11 +02:00
Konstantin Pogorelov
088ff5a063 make customLabels optional in index.d.ts 2019-04-09 22:15:36 +02:00
Konstantin Pogorelov
fd12de6ea3 bump 5.1.3 2019-04-09 22:10:00 +02:00
Konstantin Pogorelov
0b8706dd42 remove trailing spaces 2019-04-09 22:09:44 +02:00
Konstantin Pogorelov
982ff86543 Merge pull request #35 from aaronleesmith/master
add customLabels to index.d.ts
2019-04-09 22:05:51 +02:00
Aaron Smith
5f39d8f357 Update index.d.ts
Custom labels was added as an option but never added to the typescript definition file, breaking the usage during typescript compilation.
2019-04-09 13:54:17 -06:00
Konstantin Pogorelov
d405e3f584 bump 5.1.2 2019-04-08 12:02:45 +02:00
Konstantin Pogorelov
ff147d8fc4 Merge pull request #34 from cliedeman/fix-typedef
typedefs: normalizePath should return a string
2019-04-08 12:01:47 +02:00
Ciaran Liedeman
43f5a2b04f typedefs: normalizePath should return a string 2019-04-08 10:21:53 +02:00
Konstantin Pogorelov
09ee5d954a bump 5.1.1 2019-04-05 18:10:33 +02:00
Konstantin Pogorelov
33cca0d2cf remove .npmignore because files are now whitelisted 2019-04-05 18:09:56 +02:00
Konstantin Pogorelov
2040c043fd Merge branch 'cliedeman-feat/typescript-definitions' 2019-04-05 18:02:10 +02:00
Konstantin Pogorelov
f6466d007b Merge pull request #33 from cliedeman/feat/typescript-definitions
Added Typescript definitions
2019-04-05 18:01:33 +02:00
Konstantin Pogorelov
e68466d7af experimental workaround for testing cluster on travis 2019-04-05 17:56:22 +02:00
Ciaran Liedeman
6b49ffab08 Added Typescript definitions 2019-04-04 11:31:41 +02:00
Konstantin Pogorelov
cb128c7520 bump 5.1.0 2019-03-14 13:37:08 +01:00
Konstantin Pogorelov
d336165848 extract metricType as a seprate odc section 2019-03-14 13:36:54 +01:00
Konstantin Pogorelov
cfe0065146 minor wording changes 2019-03-14 13:17:47 +01:00
Konstantin Pogorelov
963a66a25e Merge pull request #30 from caraboides/master
pass maxAgeSeconds & ageBuckets to summary metrics
2019-03-14 13:06:33 +01:00
Christian Hennig
389684b426 Documentation for sliding window for summary metrics 2019-03-14 11:58:10 +01:00
Konstantin Pogorelov
846ccfc641 Update src/index.js
Co-Authored-By: caraboides <caraboides@googlemail.com>
2019-03-14 12:47:08 +02:00
Christian Hennig
17fe5f4c70 pass maxAgeSeconds & ageBuckets to summary metrics 2019-03-14 10:18:55 +01:00
Konstantin Pogorelov
66ef4e3176 bump 5.0.2 2018-12-24 12:50:44 +01:00
Konstantin Pogorelov
f55d397a31 disable notifications in travis-ci 2018-12-24 12:50:25 +01:00
Konstantin Pogorelov
68ad108f77 correct sample usage 2018-12-24 12:49:41 +01:00
Konstantin Pogorelov
6000056f7a bump 5.0.1 2018-12-24 02:26:14 +01:00
Konstantin Pogorelov
48856afbe3 add .vscode and Makefile to .npmignore 2018-12-24 02:18:48 +01:00
Konstantin Pogorelov
e035e2b991 add before_install to .travis.yml for peer dependency 2018-12-24 02:12:03 +01:00
Konstantin Pogorelov
c6d5964768 add metricPath and includeUp options, make whitelist/blacklist obsolete, simnplify code, update unit tests and code, add node10 to .travis.yml 2018-12-24 02:06:16 +01:00
Konstantin Pogorelov
94722e908b make prom-client a peer dependency, upgrade all packages, bump 5.0.0 2018-12-23 21:39:00 +01:00
Konstantin Pogorelov
00b8369329 Merge branch 'develop' 2018-12-23 18:48:10 +01:00
Konstantin Pogorelov
59221f891b bump 4.3.0 2018-12-23 18:41:02 +01:00
Konstantin Pogorelov
3a0b2caf61 upgrade prom-cliet to 11.2.1 (latest) 2018-12-23 18:40:28 +01:00
Konstantin Pogorelov
e7d004f0cc rename metricsType -> metricType, move corresponding readme block 2 paragraphs lower 2018-12-23 16:45:38 +01:00
Chen Li
0dd3116f23 Feature/add metrics type summary (#24)
* add metric type summary

* add tests for percentile option

* throw errors for unknown metricType

* set histogram as default metrics type
2018-12-23 16:36:47 +01:00
Konstantin Pogorelov
0606bf5f4f remove a duplicate cluster docs piece, versio 4.2.1 2018-08-26 14:00:25 +02:00
Konstantin Pogorelov
6054824c67 Merge branch 'master' into develop 2018-08-25 14:43:58 +02:00
Konstantin Pogorelov
ec9835270f version 4.2.0 2018-08-25 13:11:31 +02:00
Konstantin Pogorelov
01c78bcc1d Merge branch 'master' into develop 2018-08-25 13:10:39 +02:00
Konstantin Pogorelov
1cf8c86acb Squashed commit of the following:
commit bc65dc45cb
Author: Konstantin Pogorelov <or@pluseq.com>
Date:   Sat Aug 25 12:57:15 2018 +0200

    #16 fix typo in docs

commit 2003e7743f
Author: Konstantin Pogorelov <or@pluseq.com>
Date:   Sat Aug 25 12:44:24 2018 +0200

    #16 fix unit test (cluster_metrics/metrics_cluster), cover the error branch

commit 568c87216a
Author: Konstantin Pogorelov <or@pluseq.com>
Date:   Sat Aug 25 12:42:53 2018 +0200

    #16 remove unnecessary check for existance after new, break on error with 500

commit 98be36244e
Author: Konstantin Pogorelov <or@pluseq.com>
Date:   Sat Aug 25 12:40:15 2018 +0200

    #16 make the cluster example runnable
2018-08-25 13:07:36 +02:00
Konstantin Pogorelov
43b51d4ab1 fix jasme path (again) 2018-08-25 12:58:44 +02:00
Konstantin Pogorelov
f6e87b7697 revert way jasme was called, add npm run coverage 2018-08-25 12:58:34 +02:00
Konstantin Pogorelov
3be8d53a27 fix newlines in index.spec.js 2018-08-25 12:58:24 +02:00
Konstantin Pogorelov
34e6a21be1 update package-lock.json 2018-08-25 12:58:20 +02:00
Adam Yost
02fcda4721 Add support for clusterMaster option re: #16 (#17)
* Add support for clusterMaster option re: #16
* Add Cluster instructions to README
* Use the approach recommended in PR
* use console.error for errors
* Update with new method signature
* add code coverage for new clusterMetrics middleware
2018-08-25 12:58:09 +02:00
Konstantin Pogorelov
bc65dc45cb #16 fix typo in docs 2018-08-25 12:57:15 +02:00
Konstantin Pogorelov
8cae5b6ef3 fix jasme path (again) 2018-08-25 12:52:38 +02:00
Konstantin Pogorelov
2003e7743f #16 fix unit test (cluster_metrics/metrics_cluster), cover the error branch 2018-08-25 12:44:24 +02:00
Konstantin Pogorelov
568c87216a #16 remove unnecessary check for existance after new, break on error with 500 2018-08-25 12:42:53 +02:00
Konstantin Pogorelov
98be36244e #16 make the cluster example runnable 2018-08-25 12:40:15 +02:00
Konstantin Pogorelov
6ff1204db4 revert way jasme was called, add npm run coverage 2018-08-25 09:49:28 +02:00
Konstantin Pogorelov
f71d837660 fix newlines in index.spec.js 2018-08-25 09:45:09 +02:00
Konstantin Pogorelov
4aa2bfa6ae update package-lock.json 2018-08-24 23:48:22 +02:00
Adam Yost
1fff877787 Add support for clusterMaster option re: #16 (#17)
* Add support for clusterMaster option re: #16
* Add Cluster instructions to README
* Use the approach recommended in PR
* use console.error for errors
* Update with new method signature
* add code coverage for new clusterMetrics middleware
2018-08-24 23:36:23 +02:00
Konstantin Pogorelov
5e0cd75673 add normalizePath option as tuple array, improve docs and advanced example, version to 4.1.0 2018-08-09 11:27:04 +02:00
Konstantin Pogorelov
d292dcab33 remove changelog from readme as it duplicates github releases function 2018-08-08 12:00:29 +02:00
Konstantin Pogorelov
84f99cc49c add urlValueParser option, update docs accordingly 2018-08-08 11:23:27 +02:00
Konstantin Pogorelov
fffe35ce5e update dependencies, use url-value-parser v2 2018-08-08 11:22:03 +02:00
Konstantin Pogorelov
06341c227a wip: 4.0.0, prom-client to v11, url-value-parser to v2 2018-08-07 17:55:25 +02:00
Konstantin Pogorelov
45b8f373be include collectDefaultMetrics in kraken example 2018-01-26 17:40:05 +01:00
Konstantin Pogorelov
4ee269faee make test more specific 2018-01-23 16:58:49 +01:00
Konstantin Pogorelov
e4d6113ff2 replace node 7 with node 8 in .travis.yml 2018-01-23 16:52:12 +01:00
Konstantin Pogorelov
06f55c9ab8 add promClient option for smuggling collectDefaultMetrics, upgrade prom-client to ~10.2.2, version 3.3.0 2018-01-23 16:43:38 +01:00
Konstantin Pogorelov
c8996a7730 version 3.2.1 2017-10-19 20:26:56 +02:00
Konstantin Pogorelov
aed7edc684 remove fixed version of prom-client from readme 2017-10-19 20:25:40 +02:00
Muhammad Saiful Islam
4a840cfce6 minor typo (#13) 2017-10-19 20:23:36 +02:00
Konstantin Pogorelov
d12248dcaf upgrade prom-client, update package-lock.json 2017-09-23 12:31:12 +02:00
Konstantin Pogorelov
68eb617f88 version 3.2.0 2017-09-23 12:20:12 +02:00
Konstantin Pogorelov
bb0e453078 extends docs in regards of: customLabels, transformLabels, httpDurationMetricName 2017-09-23 12:19:56 +02:00
Konstantin Pogorelov
b0928b1b94 make template name consistent with httpDurationMetricName, fix typo in variable name 2017-09-23 12:18:00 +02:00
Konstantin Pogorelov
26f9f5ca10 add customLabels and transformLabels to advanced example 2017-09-23 12:13:14 +02:00
Konstantin Pogorelov
a261ab76a3 split includeCustomLabels into customLabels and transformLabels 2017-09-23 12:03:23 +02:00
Konstantin Pogorelov
8318e0ec1d fix typo 2017-09-23 11:49:13 +02:00
Konstantin Pogorelov
d58b434f0a fix codestyle 2017-09-23 11:35:30 +02:00
Eric Uldall
47fd051aba added customLabels feature to main() 2017-09-22 15:21:32 -07:00
Konstantin Pogorelov
313673f16f inc minor version, update README 2017-07-11 13:21:06 +02:00
Jonathan Creamer
bbd62e34d6 Upgrade prom-client... again (#9)
* Update package.json

* Update call to Histogram

* Update package.json

* Update index.js

* Update index.js
2017-07-11 11:41:40 +02:00
Konstantin Pogorelov
b8ba87009e extend unittests, simplify API, update README, use npm5 and package-lock.json, add node7 to travis config 2017-06-04 16:31:26 +02:00
Konstantin Pogorelov
1cc588c2da stop supporting node v4 and v5 2017-05-31 16:32:45 +02:00
Konstantin Pogorelov
5b1517ca91 #7 upgrade dependencies, workaround for koa-connect, update docs 2017-05-31 15:27:08 +02:00
Konstantin Pogorelov
5b1aa494cb version 2.2.0 - normalizeStatus draft 2017-03-28 17:14:33 +02:00
The Experimentalist
65549a769b Merge pull request #4 from PauloPaquielli/feature/normalizeStatusCode 2017-03-28 17:08:57 +02:00
Paulo Duarte
de83ac09a0 Remove if unnecessary 2017-03-27 17:35:13 -03:00
Paulo Duarte
52865dfb02 Back version to prom-client 2017-03-27 17:17:59 -03:00
Paulo Duarte
5c6ed64a31 Fix lint error 2017-03-27 17:16:51 -03:00
Paulo Duarte
d8c6492163 Implement test 2017-03-27 12:03:53 -03:00
Paulo Duarte
c92b85ae96 Update prom-client 2017-03-24 11:08:29 -03:00
Paulo Duarte
48f8b992fd Make normalizeStatusCode generic 2017-03-24 02:18:41 -03:00
Paulo Duarte
61e4343a8c Implements group in status code metrics 2017-03-23 14:12:29 -03:00
14 changed files with 3152 additions and 239 deletions

2
.gitignore vendored
View File

@@ -1,3 +1,5 @@
.npmrc
node_modules
coverage
/.vscode

View File

@@ -1,6 +0,0 @@
advanced-example.js
docker-compose.yml
spec
.travis.yml
.eslintrc
coverage

View File

@@ -1,5 +1,9 @@
language: node_js
node_js:
- "6"
- "5"
- "4"
- "8"
- "10"
notifications:
email: false
before_install:
- npm install prom-client

185
README.md
View File

@@ -2,29 +2,27 @@
# express prometheus bundle
Express middleware with popular prometheus metrics in one bundle. It's also compatible with koa v1 (see below).
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
Since version 5 it uses **prom-client** as a peer dependency. See: https://github.com/siimon/prom-client
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**
* `http_request_duration_seconds`: http latency histogram/summary labeled with `status_code`, `method` and `path`
## Install
```
npm install express-prom-bundle
npm install prom-client express-prom-bundle
```
## Usage
## Sample Usage
```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,40 +42,98 @@ 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**
* **customLabels**: an object containing extra labels, e.g. ```{project_name: 'hello_world'}```.
Most useful together with **transformLabels** callback, otherwise it's better to use native Prometheus relabeling.
* **includeUp**: include an auxiliary "up"-metric which always returns 1, default: **true**
* **metricsPath**: replace the `/metrics` route with a **regex** or exact **string**. Note: it is highly recommended to just stick to the default
* **metricType**: histogram/summary selection. See more details below
### metricType option ###
Two metric types are supported for `http_request_duration_seconds` metric:
* [histogram](https://prometheus.io/docs/concepts/metric_types/#histogram) (default)
* [summary](https://prometheus.io/docs/concepts/metric_types/#summary)
Additional options for **histogram**:
* **buckets**: buckets used for the `http_request_duration_seconds` histogram
Additional options for **summary**:
* **percentiles**: percentiles used for `http_request_duration_seconds` summary
* **ageBuckets**: ageBuckets configures how many buckets we have in our sliding window for the summary
* **maxAgeSeconds**: the maxAgeSeconds will tell how old a bucket can be before it is reset
### Transformation callbacks ###
* **normalizePath**: `function(req)` or `Array`
* if function is provided, then it should generate path value from express `req`
* if array is provided, then it should be an array of tuples `[regex, replacement]`. The `regex` can be a string and is automatically converted into JS regex.
* ... see more details in the section below
* **urlValueParser**: options passed when instantiating [url-value-parser](https://github.com/disjunction/url-value-parser).
This is the easiest way to customize which parts of the URL should be replaced with "#val".
See the [docs](https://github.com/disjunction/url-value-parser) of url-value-parser module for details.
* **formatStatusCode**: `function(res)` producing final status code from express `res` object, e.g. you can combine `200`, `201` and `204` to just `2xx`.
* **transformLabels**: `function(labels, req, res)` transforms the **labels** object, e.g. setting dynamic values to **customLabels**
### Other options ###
* **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.
* **promClient**: options for promClient startup, e.g. **collectDefaultMetrics**. This option was added
to keep `express-prom-bundle` runnable using confit (e.g. with kraken.js) without writing any JS code,
see [advanced example](https://github.com/jochen-schweizer/express-prom-bundle/blob/master/advanced-example.js)
### 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.
The module tries to figure out what parts of the path are values or IDs,
and what is an actual path. The example mentioned before would be
normalized to `/user/#val/profile` and that will become the value for the label.
These conversions are handled by `normalizePath` function.
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`.
You can extend this magical behavior by providing
additional RegExp rules to be performed,
or override `normalizePath` with your own function.
#### Example 1 (add custom RegExp):
```javascript
app.use(promBundle({
normalizePath: [
// collect paths like "/customer/johnbobson" as just one "/custom/#name"
['^/customer/.*', '/customer/#name'],
// collect paths like "/bobjohnson/order-list" as just one "/#name/order-list"
['^.*/order-list', '/#name/order-list']
],
urlValueParser: {
minHexLength: 5,
extraMasks: [
'ORD[0-9]{5,}' // replace strings like ORD1243423, ORD673562 as #val
]
}
}));
```
#### Example 2 (override normalizePath function):
```javascript
app.use(promBundle(/* options? */));
// let's reuse the existing one and just add some
// functionality on top
const originalNormalize = promBunle.normalizePath;
promBunle.normalizePath = (req, opts) => {
const originalNormalize = promBundle.normalizePath;
promBundle.normalizePath = (req, opts) => {
const path = originalNormalize(req, opts);
// count all docs (no matter which file) as a single path
return path.match(/^\/docs/) ? '/docs/*' : path;
// count all docs as one path, but /docs/login as a separate one
return (path.match(/^\/docs/) && !path.match(/^\/login/)) ? '/docs/*' : path;
};
```
@@ -86,7 +142,6 @@ For more details:
* [normalizePath.js](https://github.com/jochen-schweizer/express-prom-bundle/blob/master/src/normalizePath.js) - source code for path processing
## express example
setup std. metrics but exclude `up`-metric:
@@ -116,24 +171,62 @@ app.listen(3000);
See an [advanced example on github](https://github.com/jochen-schweizer/express-prom-bundle/blob/master/advanced-example.js)
## koa v1 example
## koa v2 example
```javascript
const promBundle = require("express-prom-bundle");
const koa = require("koa");
const Koa = require("koa");
const c2k = require("koa-connect");
const metricsMiddleware = promBundle({/* options */ });
const app = koa();
const app = new Koa();
app.use(c2k(metricsMiddleware));
app.use(/* your middleware */);
app.listen(3000);
```
## using with cluster
You'll need to use an additional **clusterMetrics()** middleware.
In the example below the master process will expose an API with a single endpoint `/metrics`
which returns an aggregate of all metrics from all the workers.
``` javascript
const cluster = require('cluster');
const promBundle = require('./src/index');
const numCPUs = Math.max(2, require('os').cpus().length);
const express = require('express');
if (cluster.isMaster) {
for (let i = 1; i < numCPUs; i++) {
cluster.fork();
}
const metricsApp = express();
metricsApp.use('/metrics', promBundle.clusterMetrics());
metricsApp.listen(9999);
console.log('cluster metrics listening on 9999');
console.log('call localhost:9999/metrics for aggregated metrics');
} else {
const app = express();
app.use(promBundle({
autoregister: false, // disable /metrics for single workers
includeMethod: true
}));
app.use((req, res) => res.send(`hello from pid ${process.pid}\n`));
app.listen(3000);
console.log(`worker ${process.pid} listening on 3000`);
}
```
## using with kraken.js
Here is meddleware config sample, which can be used in a standard **kraken.js** application:
Here is meddleware config sample, which can be used in a standard **kraken.js** application.
In this case the stats for URI paths and HTTP methods are collected separately,
while replacing all HEX values starting from 5 characters and all IP addresses in the path as #val.
```json
{
@@ -146,7 +239,19 @@ Here is meddleware config sample, which can be used in a standard **kraken.js**
"arguments": [
{
"includeMethod": true,
"buckets": [0.1, 1, 5]
"includePath": true,
"buckets": [0.1, 1, 5],
"promClient": {
"collectDefaultMetrics": {
"timeout": 2000
}
},
"urlValueParser": {
"minHexLength": 5,
"extraMasks": [
"^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$"
]
}
}
]
}
@@ -155,26 +260,6 @@ Here is meddleware config sample, which can be used in a standard **kraken.js**
}
```
## Changelog
* **2.1.0**
* deprecate **excludeRoutes**, use **req.originalPath** instead of **req.path**
* **2.0.0**
* the reason for the version lift were:
* compliance to official naming recommendation: https://prometheus.io/docs/practices/naming/
* stopping promotion of an anti-pattern - see https://groups.google.com/d/msg/prometheus-developers/XjlOnDCK9qc/ovKzV3AIBwAJ
* dealing with **prom-client** being a singleton with a built-in registry
* main histogram metric renamed from `http_request_seconds` to `http_request_duration_seconds`
* options removed: **prefix**, **keepDefaultMetrics**
* factory removed (as the only reason of it was adding the prefix)
* upgrade prom-client to 6.3.0
* code style changed to the one closer to express
* **1.2.1**
* upgrade prom-client to 6.1.2
* add options: includeMethod, includePath, keepDefaultMetrics
## License
MIT

View File

@@ -2,24 +2,36 @@
const express = require('express');
const app = express();
const promClient = require('prom-client');
const promBundle = require('express-prom-bundle');
// here we want to remove default metrics provided in prom-client
// this must be done before initializing promBundle
clearInterval(promBundle.promClient.defaultMetrics());
promBundle.promClient.register.clear();
const bundle = promBundle({
blacklist: [/up/],
buckets: [0.1, 0.4, 0.7],
includeMethod: true,
includePath: true
includePath: true,
customLabels: {year: null},
transformLabels: labels => Object.assign(labels, {year: new Date().getFullYear()}),
metricsPath: '/prometheus',
promClient: {
collectDefaultMetrics: {
timeout: 1000
}
},
urlValueParser: {
minHexLength: 5,
extraMasks: [
"^[0-9]+\\.[0-9]+\\.[0-9]+$" // replace dot-separated dates with #val
]
},
normalizePath: [
['^/foo', '/example'] // replace /foo with /example
]
});
app.use(bundle);
// native prom-client metric (no prefix)
const c1 = new bundle.promClient.Counter('c1', 'c1 help');
const c1 = new promClient.Counter({name: 'c1', help: 'c1 help'});
c1.inc(10);
app.get('/foo/:id', (req, res) => {
@@ -35,10 +47,13 @@ app.delete('/foo/:id', (req, res) => {
app.get('/bar', (req, res) => res.send('bar response\n'));
app.listen(3000, () => console.info( // eslint-disable-line
'listening on 3000\n'
+ 'test in shell console\n\n'
+ 'curl localhost:3000/foo/1234\n'
+ 'curl -X DELETE localhost:3000/foo/5432\n'
+ 'curl localhost:3000/bar\n'
+ 'curl localhost:3000/metrics\n'
`listening on 3000
test in shell console:
curl localhost:3000/foo/1234
curl localhost:3000/foo/09.08.2018
curl -X DELETE localhost:3000/foo/5432
curl localhost:3000/bar
curl localhost:3000/prometheus
`
));

47
index.d.ts vendored Normal file
View File

@@ -0,0 +1,47 @@
import * as express from "express";
import { DefaultMetricsCollectorConfiguration } from "prom-client";
type NormalizePathRegexs = [string, string];
interface Labels {
[key: string]: string | number;
}
interface Opts {
autoregister?: boolean;
buckets?: number[];
customLabels?: { [key: string]: any };
includeStatusCode?: boolean;
includeMethod?: boolean;
includePath?: boolean;
includeUp?: boolean;
metricType?: "summary" | "histogram";
metricsPath?: string;
promClient?: DefaultMetricsCollectorConfiguration;
normalizePath?: NormalizePathRegexs;
formatStatusCode?: (res: express.Response) => number | string;
transformLabels?: (
labels: Labels,
req: express.Request,
res: express.Response
) => Labels;
// https://github.com/disjunction/url-value-parser#options
urlValueParser?: {
minHexLength?: number;
minBase64Length?: number;
replaceMasks?: string[];
extraMasks?: string[];
};
}
export function normalizePath(
req: express.Request,
opts?: { normalizePath?: NormalizePathRegexs }
): string;
export default function(opts: Opts): express.RequestHandler;

2383
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{
"name": "express-prom-bundle",
"version": "2.1.0",
"version": "5.1.4",
"description": "express middleware with popular prometheus metrics in one bundle",
"main": "src/index.js",
"keywords": [
@@ -10,29 +10,39 @@
"path",
"method"
],
"files": [
"src",
"index.d.ts"
],
"scripts": {
"test": "node_modules/jasme/run.js"
"test": "node_modules/jasme/run.js",
"coverage": "make coverage"
},
"author": "Konstantin Pogorelov <or@pluseq.com>",
"license": "MIT",
"dependencies": {
"on-finished": "^2.3.0",
"prom-client": "^6.3.0",
"url-value-parser": "^1.0.0"
"url-value-parser": "^2.0.0"
},
"devDependencies": {
"coveralls": "^2.11.15",
"eslint": "^3.11.1",
"express": "^4.14.0",
"coveralls": "^3.0.2",
"eslint": "^5.11.0",
"express": "^4.16.4",
"istanbul": "^0.4.5",
"jasme": "^5.2.0",
"koa": "^1.2.4",
"koa-connect": "^1.0.0",
"supertest": "^2.0.1",
"jasme": "^6.0.0",
"koa": "^2.6.2",
"koa-connect": "^2.0.1",
"supertest": "^3.3.0",
"supertest-koa-agent": "^0.3.0"
},
"peerDependencies": {
"prom-client": "^11.1.2"
},
"repository": {
"type": "git",
"url": "https://github.com/jochen-schweizer/express-prom-bundle.git"
},
"engines": {
"node": ">=6.0.0"
}
}

View File

@@ -4,10 +4,11 @@
const express = require('express');
const supertest = require('supertest');
const bundle = require('../');
const koa = require('koa');
const Koa = require('koa');
const c2k = require('koa-connect');
const supertestKoa = require('supertest-koa-agent');
const promClient = require('prom-client');
const cluster = require('cluster');
describe('index', () => {
beforeEach(() => {
@@ -17,7 +18,7 @@ describe('index', () => {
it('metrics returns up=1', done => {
const app = express();
const bundled = bundle({
whitelist: ['up']
excludeRoutes: ['/irrelevant', /at.all/]
});
app.use(bundled);
app.use('/test', (req, res) => res.send('it worked'));
@@ -34,12 +35,83 @@ describe('index', () => {
});
});
it('metrics should be attached to /metrics by default', done => {
it('"up"-metric can be excluded', done => {
const app = express();
const bundled = bundle({
whitelist: ['up']
includeUp: false
});
app.use(bundled);
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(/up\s1/);
done();
});
});
});
it('metrics path can be defined with a regex', done => {
const app = express();
const bundled = bundle({
metricsPath: /^\/prometheus$/
});
app.use(bundled);
app.use('/test', (req, res) => res.send('it worked'));
const agent = supertest(app);
agent.get('/test').end(() => {
agent
.get('/prometheus')
.end((err, res) => {
expect(res.status).toBe(200);
expect(res.text).toMatch(/up\s1/);
done();
});
});
});
it('metrics path can be defined as regexp', done => {
const app = express();
const bundled = bundle();
app.use(bundled);
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(/up\s1/);
done();
});
});
});
it('httpDurationMetricName overrides histogram metric name', done => {
const app = express();
const bundled = bundle({
httpDurationMetricName: 'my_http_duration'
});
app.use(bundled);
const agent = supertest(app);
agent.get('/metrics')
.end((err, res) => {
expect(res.text).toMatch(/my_http_duration/m);
done();
});
});
it('metrics should be attached to /metrics by default', done => {
const app = express();
const bundled = bundle();
app.use(bundled);
const agent = supertest(app);
agent.get('/metrics')
@@ -67,27 +139,6 @@ describe('index', () => {
});
});
it('metrics can be filtered using exect match', () => {
const instance = bundle({blacklist: ['up']});
expect(instance.metrics.up).not.toBeDefined();
expect(instance.metrics.http_request_duration_seconds).toBeDefined();
});
it('metrics can be filtered using regex', () => {
const instance = bundle({blacklist: [/http/]});
expect(instance.metrics.up).toBeDefined();
expect(instance.metrics.http_request_duration_seconds).not.toBeDefined();
});
it('metrics can be whitelisted', () => {
const instance = bundle({whitelist: [/^up$/]});
expect(instance.metrics.up).toBeDefined();
expect(instance.metrics.nodejs_memory_heap_total_bytes).not.toBeDefined();
expect(instance.metrics.http_request_duration_seconds).not.toBeDefined();
});
it('throws on both white and blacklist', () => {
expect(() => {
bundle({whitelist: [/up/], blacklist: [/up/]});
}).toThrow();
});
it('returns error 500 on incorrect middleware usage', done => {
const app = express();
app.use(bundle);
@@ -124,22 +175,27 @@ describe('index', () => {
it('filters out the excludeRoutes', done => {
const app = express();
const instance = bundle({
excludeRoutes: ['/test']
excludeRoutes: ['/test', /bad.word/]
});
app.use(instance);
app.use('/test', (req, res) => res.send('it worked'));
app.use('/some/bad-word', (req, res) => res.send('it worked too'));
const agent = supertest(app);
agent
.get('/test')
.end(() => {
const metricHashMap = instance.metrics.http_request_duration_seconds.hashMap;
expect(metricHashMap['status_code:200']).not.toBeDefined();
agent
.get('/metrics')
.end((err, res) => {
expect(res.status).toBe(200);
done();
.get('/some/bad-word')
.end(() => {
const metricHashMap = instance.metrics.http_request_duration_seconds.hashMap;
expect(metricHashMap['status_code:200']).not.toBeDefined();
agent
.get('/metrics')
.end((err, res) => {
expect(res.status).toBe(200);
done();
});
});
});
});
@@ -148,11 +204,12 @@ describe('index', () => {
expect(() => bundle({prefix: 'hello'})).toThrow();
});
it('tolerates includePath, includeMethod', done => {
it('tolerates includePath, includeMethod, includeCustomLabels', done => {
const app = express();
const instance = bundle({
includePath: true,
includeMethod: true
includeMethod: true,
includeCustomLabels: {foo: 'bar'}
});
app.use(instance);
app.use('/test', (req, res) => res.send('it worked'));
@@ -169,12 +226,111 @@ describe('index', () => {
});
});
it('normalizePath can be replaced', done => {
it('metric type histogram works', done => {
const app = express();
const bundled = bundle({
metricType: 'histogram',
buckets: [10, 100],
});
app.use(bundled);
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(/le="100"/);
done();
});
});
});
it('throws on unknown metricType ', () => {
expect(() => {
bundle({metricType: 'hello'});
}).toThrow();
});
describe('usage of normalizePath()', () => {
it('normalizePath can be replaced gloablly', done => {
const app = express();
const original = bundle.normalizePath;
bundle.normalizePath = () => 'dummy';
const instance = bundle({
includePath: true,
});
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(/"dummy"/m);
bundle.normalizePath = original;
done();
});
});
});
it('normalizePath function 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('normalizePath can be passed as an array of [regex, replacement]', done => {
const app = express();
const instance = bundle({
includePath: true,
normalizePath: [
['^/docs/whatever/.*$', '/docs'],
[/^\/docs$/, '/mocks']
]
});
app.use(instance);
app.use('/docs/whatever/:andmore', (req, res) => res.send('it worked'));
const agent = supertest(app);
agent
.get('/docs/whatever/unimportant')
.end(() => {
agent
.get('/metrics')
.end((err, res) => {
expect(res.status).toBe(200);
expect(res.text).toMatch(/"\/mocks"/m);
done();
});
});
});
});
it('formatStatusCode can be overridden', done => {
const app = express();
const original = bundle.normalizePath;
bundle.normalizePath = () => 'dummy';
const instance = bundle({
includePath: true,
formatStatusCode: () => 555
});
app.use(instance);
app.use('/test', (req, res) => res.send('it worked'));
@@ -186,25 +342,88 @@ describe('index', () => {
.get('/metrics')
.end((err, res) => {
expect(res.status).toBe(200);
expect(res.text).toMatch(/"dummy"/m);
bundle.normalizePath = original;
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('customLabels={foo: "bar"} adds foo="bar" label to metrics', done => {
const app = express();
const instance = bundle({
customLabels: {foo: 'bar'}
});
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(/foo="bar"/);
done();
});
});
});
it('tarnsformLabels can set label values', done => {
const app = express();
const instance = bundle({
includePath: true,
customLabels: {foo: 'bar'},
transformLabels: labels => {
labels.foo = 'baz';
labels.path += '/ok';
}
});
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(/foo="baz"/);
expect(res.text).toMatch(/path="\/test\/ok"/);
done();
});
});
});
it('Koa: metrics returns up=1', done => {
const app = koa();
const bundled = bundle({
whitelist: ['up']
});
const app = new Koa();
const bundled = bundle();
app.use(c2k(bundled));
app.use(function*(next) {
if (this.path !== 'test') {
return yield next;
}
this.body = 'it worked';
app.use(function(ctx, next) {
return next().then(() => ctx.body = 'it worked');
});
const agent = supertestKoa(app);
@@ -218,4 +437,98 @@ describe('index', () => {
});
});
});
it('calls promClient.collectDefaultMetrics', () => {
const spy = spyOn(promClient, 'collectDefaultMetrics');
bundle({
promClient: {
collectDefaultMetrics: {
timeout: 3000
}
}
});
expect(spy).toHaveBeenCalledWith({timeout: 3000});
});
describe('usage of clusterMetrics()', () => {
it('clusterMetrics returns 200 even without a cluster', (done) => {
const app = express();
cluster.workers = [];
app.use('/cluster_metrics', bundle.clusterMetrics());
const agent = supertest(app);
agent
.get('/cluster_metrics')
.end((err, res) => {
expect(res.status).toBe(200);
done();
});
});
it('clusterMetrics returns 500 in case of an error', (done) => {
const app = express();
app.use('/cluster_metrics', bundle.clusterMetrics());
const agent = supertest(app);
// create a fake worker, which would not respond in time
cluster.workers = [{
isConnected: () => true,
send: () => {}
}];
const errorSpy = spyOn(console, 'error'); // mute console.error
agent
.get('/cluster_metrics')
.end((err, res) => {
expect(res.status).toBe(500);
expect(errorSpy).toHaveBeenCalled();
done();
});
}, 6000);
});
describe('metricType: summary', () => {
it('metric type summary works', done => {
const app = express();
const bundled = bundle({
metricType: 'summary'
});
app.use(bundled);
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(/quantile="0.98"/);
done();
});
});
});
it('custom pecentiles work', done => {
const app = express();
const bundled = bundle({
metricType: 'summary',
percentiles: [0.5, 0.85, 0.99],
});
app.use(bundled);
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(/quantile="0.85"/);
done();
});
});
});
});
});

View File

@@ -4,25 +4,28 @@
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');
});
it('uses normalizePath option', () => {
const url = '/hello/world/i/am/finally/free!!!';
const result = normalizePath({url}, {
normalizePath: [
['/hello','/goodbye'],
['[^/]+$','happy'],
]
});
expect(result).toBe('/goodbye/world/i/am/finally/happy');
});
it('throws error is bad tuples provided as normalizePath', () => {
const subject = () => normalizePath({url: '/test'}, {
normalizePath: [
['/hello','/goodbye', 'test']
]
});
expect(subject).toThrow();
});
});

View File

@@ -0,0 +1,12 @@
'use strict';
/* eslint-env jasmine */
const normalizeStatusCode = require('../src/normalizeStatusCode');
describe('normalizeStatusCode', () => {
it('returns run callback if configured', () => {
expect(
normalizeStatusCode({status_code: 500})
).toBe(500);
});
});

View File

@@ -1,10 +1,10 @@
'use strict';
const onFinished = require('on-finished');
const promClient = require('prom-client');
const normalizePath = require('./normalizePath');
const normalizeStatusCode = require('./normalizeStatusCode');
function matchVsRegExps(element, regexps) {
for (let regexp of regexps) {
for (const regexp of regexps) {
if (regexp instanceof RegExp) {
if (element.match(regexp)) {
return true;
@@ -16,29 +16,24 @@ function matchVsRegExps(element, regexps) {
return false;
}
function filterArrayByRegExps(array, regexps) {
return array.filter(element => {
return matchVsRegExps(element, regexps);
});
}
function clusterMetrics() {
const aggregatorRegistry = new promClient.AggregatorRegistry();
function prepareMetricNames(opts, metricTemplates) {
const names = Object.keys(metricTemplates);
if (opts.whitelist) {
if (opts.blacklist) {
throw new Error('you cannot have whitelist and blacklist at the same time');
}
return filterArrayByRegExps(names, opts.whitelist);
}
if (opts.blacklist) {
const blacklisted = filterArrayByRegExps(names, opts.blacklist);
return names.filter(name => blacklisted.indexOf(name) === -1);
}
return names;
const metricsMiddleware = function(req, res) {
aggregatorRegistry.clusterMetrics((err, clusterMetrics) => {
if (err) {
console.error(err);
return res.sendStatus(500);
}
res.set('Content-Type', aggregatorRegistry.contentType);
res.send(clusterMetrics);
});
};
return metricsMiddleware;
}
function main(opts) {
opts = Object.assign({autoregister: true}, opts);
if (arguments[2] && arguments[1] && arguments[1].send) {
arguments[1].status(500)
.send('<h1>500 Error</h1>\n'
@@ -48,50 +43,78 @@ function main(opts) {
return;
}
if (opts.prefix || opts.keepDefaultMetrics !== undefined) {
opts = Object.assign(
{
autoregister: true,
includeStatusCode: true,
normalizePath: main.normalizePath,
formatStatusCode: main.normalizeStatusCode,
metricType: 'histogram',
promClient: {}
}, opts
);
if (opts.prefix
|| opts.keepDefaultMetrics !== undefined
|| opts.whitelist !== undefined
|| opts.blacklist !== undefined
) {
throw new Error(
'express-prom-bundle detected obsolete options:'
+ 'prefix and/or keepDefaultMetrics. '
'express-prom-bundle detected one of the obsolete options: '
+ 'prefix, keepDefaultMetrics, whitelist, blacklist. '
+ 'Please refer to oficial docs. '
+ 'Most likely you upgraded the module without necessary code changes'
+ 'Most likely you upgraded the module without the necessary code changes'
);
}
const httpMtricName = opts.httpDurationMetricName || 'http_request_duration_seconds';
const metricTemplates = {
'up': () => new promClient.Gauge(
'up',
'1 = up, 0 = not up'
),
'http_request_duration_seconds': () => {
const labels = ['status_code'];
if (opts.includeMethod) {
labels.push('method');
}
if (opts.includePath) {
labels.push('path');
}
const metric = new promClient.Histogram(
httpMtricName,
'duration histogram of http responses labeled with: ' + labels.join(', '),
labels,
{
buckets: opts.buckets || [0.003, 0.03, 0.1, 0.3, 1.5, 10]
}
);
return metric;
}
};
const metrics = {};
const names = prepareMetricNames(opts, metricTemplates);
for (let name of names) {
metrics[name] = metricTemplates[name]();
if (opts.promClient.collectDefaultMetrics) {
promClient.collectDefaultMetrics(opts.promClient.collectDefaultMetrics);
}
if (metrics.up) {
const httpMetricName = opts.httpDurationMetricName || 'http_request_duration_seconds';
function makeHttpMetric() {
const labels = ['status_code'];
if (opts.includeMethod) {
labels.push('method');
}
if (opts.includePath) {
labels.push('path');
}
if (opts.customLabels) {
labels.push.apply(labels, Object.keys(opts.customLabels));
}
if (opts.metricType === 'summary') {
return new promClient.Summary({
name: httpMetricName,
help: 'duration summary of http responses labeled with: ' + labels.join(', '),
labelNames: labels,
percentiles: opts.percentiles || [0.5, 0.75, 0.95, 0.98, 0.99, 0.999],
maxAgeSeconds: opts.maxAgeSeconds,
ageBuckets: opts.ageBuckets
});
} else if (opts.metricType === 'histogram' || !opts.metricType) {
return new promClient.Histogram({
name: httpMetricName,
help: 'duration histogram of http responses labeled with: ' + labels.join(', '),
labelNames: labels,
buckets: opts.buckets || [0.003, 0.03, 0.1, 0.3, 1.5, 10]
});
} else {
throw new Error('metricType option must be histogram or summary');
}
}
const metrics = {
[httpMetricName]: makeHttpMetric()
};
if (opts.includeUp !== false) {
metrics.up = new promClient.Gauge({
name: 'up',
help: '1 = up, 0 = not up'
});
metrics.up.set(1);
}
@@ -100,43 +123,57 @@ function main(opts) {
res.end(promClient.register.metrics());
};
const middleware = function (req, res, next) {
const path = req.originalUrl;
let labels;
const metricsMatch = opts.metricsPath instanceof RegExp ? opts.metricsPath
: new RegExp('^' + (opts.metricsPath || '/metrics') + '/?$');
if (opts.autoregister && path.match(/^\/metrics\/?$/)) {
return metricsMiddleware(req, res);
const middleware = function (req, res, next) {
const path = req.originalUrl || req.url; // originalUrl gets lost in koa-connect?
if (opts.autoregister && path.match(metricsMatch)) {
return metricsMiddleware(req, res);
}
if (opts.excludeRoutes && matchVsRegExps(path, opts.excludeRoutes)) {
return next();
}
if (metrics[httpMtricName]) {
labels = {'status_code': 0};
let timer = metrics[httpMtricName].startTimer(labels);
onFinished(res, () => {
labels.status_code = res.statusCode;
if (opts.includeMethod) {
labels.method = req.method;
}
if (opts.includePath) {
labels.path = main.normalizePath(req, opts);
}
timer();
});
}
const labels = {};
const timer = metrics[httpMetricName].startTimer(labels);
onFinished(res, () => {
if (opts.includeStatusCode) {
labels.status_code = opts.formatStatusCode(res, opts);
}
if (opts.includeMethod) {
labels.method = req.method;
}
if (opts.includePath) {
labels.path = opts.normalizePath instanceof Function
? opts.normalizePath(req, opts)
: main.normalizePath(req, opts);
}
if (opts.customLabels) {
Object.assign(labels, opts.customLabels);
}
if (opts.transformLabels) {
opts.transformLabels(labels, req, res);
}
timer();
});
next();
};
middleware.metricTemplates = metricTemplates;
middleware.metrics = metrics;
middleware.promClient = promClient;
middleware.metricsMiddleware = metricsMiddleware;
return middleware;
}
// this is kept only for compatibility with the code relying on older version
main.promClient = promClient;
main.normalizePath = normalizePath;
main.normalizeStatusCode = normalizeStatusCode;
main.clusterMetrics = clusterMetrics;
module.exports = main;

View File

@@ -2,26 +2,29 @@
const UrlValueParser = require('url-value-parser');
const url = require('url');
// ATTENTION! urlValueParser is a singleton!
let urlValueParser;
module.exports = function(req, opts) {
opts = opts || {};
// 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).pathname;
let 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);
const normalizePath = opts && opts.normalizePath;
if (Array.isArray(normalizePath)) {
for (const tuple of normalizePath) {
if (!Array.isArray(tuple) || tuple.length !== 2) {
throw new Error('Bad tuple provided in normalizePath option, expected: [regex, replacement]');
}
const regex = typeof tuple[0] === 'string' ? RegExp(tuple[0]) : tuple[0];
path = path.replace(regex, tuple[1]);
}
}
if (!urlValueParser) {
urlValueParser = new UrlValueParser();
urlValueParser = new UrlValueParser(opts && opts.urlValueParser);
}
return urlValueParser.replacePathValues(path);
};

View File

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