From fb2af590f463de00bf21fd0493c4ee1580865059 Mon Sep 17 00:00:00 2001 From: Piotr Wyrobek Date: Mon, 29 Mar 2021 16:05:05 +0200 Subject: [PATCH 1/3] feat(feature): extended options to allow custom path normalization --- README.md | 8 ++++++++ src/index.js | 6 +++++- 2 files changed, 13 insertions(+), 1 deletion(-) 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..a62e628 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 option.customUrlNormalizer === 'function' ? + option.customPathNormalizer(originalUrl, req, res) : null; + + const route = normalizePath(customNormalizedPath|| originalUrl, options.extraMasks); if (route !== metricsPath) { const status = normalizeStatus From c4f21e9af3abacf69451cd82db7b27949e5a8bb7 Mon Sep 17 00:00:00 2001 From: Piotr Wyrobek Date: Mon, 29 Mar 2021 16:11:04 +0200 Subject: [PATCH 2/3] feat(linter): fixing linting --- src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index a62e628..eb1ae75 100644 --- a/src/index.js +++ b/src/index.js @@ -57,10 +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 customNormalizedPath = typeof option.customUrlNormalizer === 'function' ? - option.customPathNormalizer(originalUrl, req, res) : null; + const customNormalizedPath = typeof options.customUrlNormalizer === 'function' + ? options.customPathNormalizer(originalUrl, req, res) : null; - const route = normalizePath(customNormalizedPath|| originalUrl, options.extraMasks); + const route = normalizePath(customNormalizedPath || originalUrl, options.extraMasks); if (route !== metricsPath) { const status = normalizeStatus From 21be9c60234df61fa7692cbcf32aa512b64463ba Mon Sep 17 00:00:00 2001 From: Piotr Wyrobek Date: Mon, 29 Mar 2021 16:45:46 +0200 Subject: [PATCH 3/3] feat(fix): rename --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index eb1ae75..22f98ae 100644 --- a/src/index.js +++ b/src/index.js @@ -57,7 +57,7 @@ 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 customNormalizedPath = typeof options.customUrlNormalizer === 'function' + const customNormalizedPath = typeof options.customPathNormalizer === 'function' ? options.customPathNormalizer(originalUrl, req, res) : null; const route = normalizePath(customNormalizedPath || originalUrl, options.extraMasks);