Skip to content
This repository was archived by the owner on Jan 8, 2023. It is now read-only.

Commit 01e5910

Browse files
author
Piotr Wyrobek
committed
Extended options to allow custom path normalization
1 parent 9743a84 commit 01e5910

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ npm i --save express-prometheus-middleware
3535
| customLabels | Optional Array containing extra labels, used together with `transformLabels` | no extra labels: `[]` |
3636
| transformLabels | Optional `function(labels, req, res)` adds to the labels object dynamic values for each label in `customLabels` | `null` |
3737
| normalizeStatus | Optional parameter to disable normalization of the status code. Example of normalized and non-normalized status code respectively: 4xx and 422.| true
38+
| customPathNormalizer | Optional `function(path, req, res)` to apply custom path normalization function | null
39+
40+
3841
### Example
3942

4043
```js
@@ -73,6 +76,11 @@ app.use(promMid({
7376
// // eslint-disable-next-line no-param-reassign
7477
// labels.contentType = req.headers['content-type'];
7578
// },
79+
// customPathNormalizer(path) {
80+
// if (path.includes('test')) {
81+
// reutrn 'test';
82+
// }
83+
// }
7684
}));
7785

7886
// curl -X GET localhost:9091/hello?name=Chuck%20Norris

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const defaultOptions = {
2525
customLabels: [],
2626
transformLabels: null,
2727
normalizeStatus: true,
28+
customPathNormalizer: null,
2829
};
2930

3031
module.exports = (userOptions = {}) => {
@@ -56,7 +57,10 @@ module.exports = (userOptions = {}) => {
5657
// will replace ids from the route with `#val` placeholder this serves to
5758
// measure the same routes, e.g., /image/id1, and /image/id2, will be
5859
// treated as the same route
59-
const route = normalizePath(originalUrl, options.extraMasks);
60+
const customNormalizedPath = typeof option.customUrlNormalizer === 'function' ?
61+
option.customPathNormalizer(originalUrl, req, res) : null;
62+
63+
const route = normalizePath(customNormalizedPath|| originalUrl, options.extraMasks);
6064

6165
if (route !== metricsPath) {
6266
const status = normalizeStatus

0 commit comments

Comments
 (0)