From fb8e8ae625e7b70c3ad111e06ea2af97e1623b62 Mon Sep 17 00:00:00 2001 From: Mnwa Date: Tue, 17 Dec 2019 13:33:01 +0300 Subject: [PATCH 1/5] Change orginalUrl to route --- src/index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 350b44b..1890a27 100644 --- a/src/index.js +++ b/src/index.js @@ -40,11 +40,15 @@ module.exports = (userOptions = {}) => { * of the RED metrics. */ const redMiddleware = ResponseTime((req, res, time) => { - const { originalUrl, method } = req; + const { route: router, method } = req; // 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); + if (!router) { + return; + } + + const route = normalizePath(router.path, options.extraMasks); if (route !== metricsPath) { const status = normalizeStatusCode(res.statusCode); From d2d3fc71ce18b667e53908522ced35c9be1573b6 Mon Sep 17 00:00:00 2001 From: Mnwa Date: Tue, 17 Dec 2019 13:43:55 +0300 Subject: [PATCH 2/5] add use original url flag --- src/index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 1890a27..f8d3578 100644 --- a/src/index.js +++ b/src/index.js @@ -21,6 +21,8 @@ const defaultOptions = { // these are aribtrary values since i dont know any better ¯\_(ツ)_/¯ requestDurationBuckets: Prometheus.exponentialBuckets(0.05, 1.75, 8), extraMasks: [], + // this is the flag which swap using originalUrl and route.path params + useOriginalUrl: true, }; module.exports = (userOptions = {}) => { @@ -40,15 +42,15 @@ module.exports = (userOptions = {}) => { * of the RED metrics. */ const redMiddleware = ResponseTime((req, res, time) => { - const { route: router, method } = req; + const { originalUrl, route: routeData, method } = req; // 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 - if (!router) { + if (!options.useOriginalUrl && !routeData) { return; } - const route = normalizePath(router.path, options.extraMasks); + const route = normalizePath(options.useOriginalUrl ? originalUrl : routeData.path, options.extraMasks); if (route !== metricsPath) { const status = normalizeStatusCode(res.statusCode); From 59ad2ce138f365b93710a5039d1fa4b3d43cb8de Mon Sep 17 00:00:00 2001 From: Mnwa Date: Tue, 17 Dec 2019 13:47:11 +0300 Subject: [PATCH 3/5] Add flag --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 7178706..2b715ec 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,10 @@ app.use(promMid({ * reformat URL path names and replace the values found with a placeholder value */ // extraMasks: [/..:..:..:..:..:../], + /** + * Uncommenting the `useOriginalUrl` config will swap using you route label from '/data/1' to '/data/:id' + /* + // useOriginalUrl: true, })); // curl -X GET localhost:9091/hello?name=Chuck%20Norris From cb77c1c09a05ae838994530a8a7230487767a810 Mon Sep 17 00:00:00 2001 From: Mnwa Date: Tue, 17 Dec 2019 13:52:35 +0300 Subject: [PATCH 4/5] add pitch --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2b715ec..122ce52 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ app.use(promMid({ */ // extraMasks: [/..:..:..:..:..:../], /** - * Uncommenting the `useOriginalUrl` config will swap using you route label from '/data/1' to '/data/:id' + * Uncommenting the `useOriginalUrl` config and setting it to `false` will swap using you route label from '/data/1' to '/data/:id' /* // useOriginalUrl: true, })); From 7a08a0b650dd06b3691b089293ccba58ed06d222 Mon Sep 17 00:00:00 2001 From: Mnwa Date: Tue, 17 Dec 2019 13:55:42 +0300 Subject: [PATCH 5/5] add doc to table --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 122ce52..87e0acf 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ npm i --save express-prometheus-middleware | extraMasks | Optional, list of regexes to be used as argument to [url-value-parser](https://www.npmjs.com/package/url-value-parser), this will cause extra route params, to be replaced with a `#val` placeholder. | no extra masks: `[]` | | authenticate | Optional authentication callback, the function should receive as argument, the `req` object and return truthy for sucessfull authentication, or falsy, otherwise. This option supports Promise results. | `null` | | prefix | Optional prefix for the metrics name | no prefix added | | +| useOriginalUrl | Optional flag, in `false` will change you route label from url to the router mask | `true` | | ### Example