Skip to content

Commit

Permalink
(fix): Prometheus path normalisation improvements
Browse files Browse the repository at this point in the history
- Make case insensitive
- Ensure all regexes have anchors
- Omit query string with request.path
  • Loading branch information
cblanc committed May 7, 2019
1 parent 4aa0c93 commit 30d5c77
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
9 changes: 4 additions & 5 deletions config/prometheus.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,22 @@ const paths = [
],
["^/postcodes/[^/]+$", "/postcodes/:postcode"],
["^/postcodes/.+/validate$", "/postcodes/:postcode/validate"],
["^/postcodes/.+/nearest", "/postcodes/:postcode/nearest"],
["^/postcodes/.+/autocomplete", "/postcodes/:postcode/autocomplete"],
["^/postcodes/.+/nearest$", "/postcodes/:postcode/nearest"],
["^/postcodes/.+/autocomplete$", "/postcodes/:postcode/autocomplete"],
["^/outcodes/[^/]+$", "/outcodes/:outcode"],
["^/outcodes/.+/nearest$", "/outcodes/:outcode/nearest"],
["^/terminated_postcodes/[^/]+$", "/terminated_postcodes/:postcode"],
["^/places/[^/]+$", "/places/:code"],
].map(([regex, path]) => [new RegExp(regex), path]);
].map(([regex, path]) => [new RegExp(regex, "i"), path]);

/**
* Squash metrics with dynamic paths into one
*
* e.g. /postcodes/sw1a2aa -> /postcodes/:postcode
*/
const normalizePath = request => {
const url = request.url;
for (const [regex, path] of paths) {
if (regex.test(url)) return path;
if (regex.test(request.path)) return path;
}
return "other";
};
Expand Down
6 changes: 6 additions & 0 deletions tests/prometheus.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ describe("Prometheus /metrics endpoint", () => {
await testMetric(url, expectedMetric);
});

it("normalises /PLACES/:code", async () => {
const url = "/PLACES/foobar";
const expectedMetric = "/places/:code";
await testMetric(url, expectedMetric);
});

it("does not generate metrics for unexpected paths", async () => {
const url = "/bogus";
const expectedMetric = "other";
Expand Down

0 comments on commit 30d5c77

Please sign in to comment.