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

Extended options to allow custom path normalisation #56

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ npm i --save express-prometheus-middleware
| customLabels | Optional Array containing extra labels, used together with `transformLabels` | no extra labels: `[]` |
| transformLabels | Optional `function(labels, req, res)` adds to the labels object dynamic values for each label in `customLabels` | `null` |
| normalizeStatus | Optional parameter to disable normalization of the status code. Example of normalized and non-normalized status code respectively: 4xx and 422.| true
| customPathNormalizer | Optional `function(path, req, res)` to apply custom path normalization function | null


### Example

```js
Expand Down Expand Up @@ -73,6 +76,11 @@ app.use(promMid({
// // eslint-disable-next-line no-param-reassign
// labels.contentType = req.headers['content-type'];
// },
// customPathNormalizer(path) {
// if (path.includes('test')) {
// reutrn 'test';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// reutrn 'test';
// return 'test';

// }
// }
}));

// curl -X GET localhost:9091/hello?name=Chuck%20Norris
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const defaultOptions = {
customLabels: [],
transformLabels: null,
normalizeStatus: true,
customPathNormalizer: null,
};

module.exports = (userOptions = {}) => {
Expand Down Expand Up @@ -56,7 +57,10 @@ module.exports = (userOptions = {}) => {
// will replace ids from the route with `#val` placeholder this serves to
// measure the same routes, e.g., /image/id1, and /image/id2, will be
// treated as the same route
const route = normalizePath(originalUrl, options.extraMasks);
const customNormalizedPath = typeof options.customPathNormalizer === 'function'
? options.customPathNormalizer(originalUrl, req, res) : null;

const route = normalizePath(customNormalizedPath || originalUrl, options.extraMasks);

if (route !== metricsPath) {
const status = normalizeStatus
Expand Down