From 00017da764a5c5a101308b2a915ae809bffcf1b5 Mon Sep 17 00:00:00 2001 From: Alexandru Giurgiu Date: Thu, 16 Mar 2017 14:57:42 +0200 Subject: [PATCH 1/2] added a url rewrite rule to standardise url requests. At the moment all requests get accounted by the metrics module, including static assets. If later we want to we can selectively ignore specific paths. --- api/metrics/index.js | 12 +++++------- helpers/index.js | 8 ++++++++ server.js | 4 +++- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/api/metrics/index.js b/api/metrics/index.js index fc37552df..e61650f40 100644 --- a/api/metrics/index.js +++ b/api/metrics/index.js @@ -20,12 +20,11 @@ function parse(path) { var clean_path = path; + var ignore_list = ['css', 'img', 'js']; - if (path[path.length - 1] != '/') { - if (!path.includes('.')) { - clean_path = path.substr(0, path.lastIndexOf('/') + 1); - } - }; + if (ignore_list.indexOf(path.split('/')[1]) != -1) { + clean_path = '/' + path.split('/')[1] + '/'; + } return clean_path; } @@ -35,8 +34,7 @@ if (path !== '/metrics' && path !== '/metrics/') { var duration = s(start); var method = method.toLowerCase(); - var clean_path = parse(path); - metric.http.requests.duration.labels(method, clean_path, statusCode).observe(duration); + metric.http.requests.duration.labels(method, path, statusCode).observe(duration); } }; diff --git a/helpers/index.js b/helpers/index.js index e9292594c..348e785db 100644 --- a/helpers/index.js +++ b/helpers/index.js @@ -51,6 +51,14 @@ res.end(); } + /* Rewrites and redirects any url that doesn't end with a slash. */ + helpers.rewriteSlash = function(req, res, next) { + if(req.url.substr(-1) == '/' && req.url.length > 1) + res.redirect(301, req.url.slice(0, -1)); + else + next(); + } + /* Public: performs an HTTP GET request to the given URL * * url - the URL where the external service can be reached out diff --git a/server.js b/server.js index 45141e49c..929f7a403 100644 --- a/server.js +++ b/server.js @@ -15,8 +15,10 @@ var request = require("request") , metrics = require("./api/metrics") , app = express() -app.use(express.static("public")); + +app.use(helpers.rewriteSlash); app.use(metrics); +app.use(express.static("public")); if(process.env.SESSION_REDIS) { console.log('Using the redis based session manager'); app.use(session(config.session_redis)); From 7748f944135a570f43923c31001fa7c6f2fb6a89 Mon Sep 17 00:00:00 2001 From: Alexandru Giurgiu Date: Thu, 16 Mar 2017 15:14:04 +0200 Subject: [PATCH 2/2] remove parse method, only confuses people and it's not used --- api/metrics/index.js | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/api/metrics/index.js b/api/metrics/index.js index e61650f40..7ec7deffb 100644 --- a/api/metrics/index.js +++ b/api/metrics/index.js @@ -18,17 +18,6 @@ return (diff[0] * 1e9 + diff[1]) / 1000000000; } - function parse(path) { - var clean_path = path; - var ignore_list = ['css', 'img', 'js']; - - if (ignore_list.indexOf(path.split('/')[1]) != -1) { - clean_path = '/' + path.split('/')[1] + '/'; - } - - return clean_path; -} - function observe(method, path, statusCode, start) { var path = path.toLowerCase(); if (path !== '/metrics' && path !== '/metrics/') {