diff --git a/README.md b/README.md index d68069f..b4bf13b 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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'; + // } + // } })); // curl -X GET localhost:9091/hello?name=Chuck%20Norris diff --git a/src/index.js b/src/index.js index a502854..22f98ae 100644 --- a/src/index.js +++ b/src/index.js @@ -25,6 +25,7 @@ const defaultOptions = { customLabels: [], transformLabels: null, normalizeStatus: true, + customPathNormalizer: null, }; module.exports = (userOptions = {}) => { @@ -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