express-prom-bundle
Advanced tools
Comparing version 4.0.0 to 4.1.0
{ | ||
"name": "express-prom-bundle", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "express middleware with popular prometheus metrics in one bundle", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -55,6 +55,9 @@ [![build status](https://travis-ci.org/jochen-schweizer/express-prom-bundle.png)](https://travis-ci.org/jochen-schweizer/express-prom-bundle) [![Coverage Status](https://coveralls.io/repos/github/jochen-schweizer/express-prom-bundle/badge.svg?branch=master)](https://coveralls.io/github/jochen-schweizer/express-prom-bundle?branch=master) [![license](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000)](https://www.tldrlegal.com/l/mit) [![NPM version](https://badge.fury.io/js/express-prom-bundle.png)](http://badge.fury.io/js/express-prom-bundle) | ||
* **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. | ||
* **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`. | ||
@@ -71,8 +74,2 @@ * **transformLabels**: `function(labels, req, res)` transforms the **labels** object, e.g. setting dynamic values to **customLabels** | ||
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. | ||
* **httpDurationMetricName**: name of the request duration histogram metric. (Default: `http_request_duration_seconds`) | ||
### More details on includePath option | ||
@@ -88,8 +85,31 @@ | ||
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. | ||
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? */)); | ||
@@ -102,4 +122,4 @@ | ||
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; | ||
}; | ||
@@ -113,3 +133,2 @@ ``` | ||
## express example | ||
@@ -186,3 +205,3 @@ | ||
"extraMasks": [ | ||
"^[^@]+@[^@]+\\.[^@]+$" | ||
"^[0-9]+\\.[0-9]+\\.[0-9]+$" | ||
] | ||
@@ -189,0 +208,0 @@ } |
@@ -141,3 +141,5 @@ 'use strict'; | ||
if (opts.includePath) { | ||
labels.path = opts.normalizePath(req, opts); | ||
labels.path = typeof opts.normalizePath == 'function' | ||
? opts.normalizePath(req, opts) | ||
: main.normalizePath(req, opts); | ||
} | ||
@@ -144,0 +146,0 @@ if (opts.customLabels) { |
@@ -13,4 +13,15 @@ 'use strict'; | ||
/// i.e. always in the tail of the middleware chain | ||
const path = url.parse(req.originalUrl || req.url).pathname; | ||
let path = url.parse(req.originalUrl || req.url).pathname; | ||
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) { | ||
@@ -17,0 +28,0 @@ urlValueParser = new UrlValueParser(opts && opts.urlValueParser); |
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
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
16162
178
214