diff --git a/README.md b/README.md index 7178706..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 @@ -57,6 +58,10 @@ app.use(promMid({ * reformat URL path names and replace the values found with a placeholder value */ // extraMasks: [/..:..:..:..:..:../], + /** + * Uncommenting the `useOriginalUrl` config and setting it to `false` will swap using you route label from '/data/1' to '/data/:id' + /* + // useOriginalUrl: true, })); // curl -X GET localhost:9091/hello?name=Chuck%20Norris diff --git a/src/index.js b/src/index.js index 350b44b..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,11 +42,15 @@ module.exports = (userOptions = {}) => { * of the RED metrics. */ const redMiddleware = ResponseTime((req, res, time) => { - const { originalUrl, 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 - const route = normalizePath(originalUrl, options.extraMasks); + if (!options.useOriginalUrl && !routeData) { + return; + } + + const route = normalizePath(options.useOriginalUrl ? originalUrl : routeData.path, options.extraMasks); if (route !== metricsPath) { const status = normalizeStatusCode(res.statusCode);