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

Add useOrginalUrl flag #15

Open
wants to merge 6 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) => {
Expand All @@ -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);
Expand Down