Skip to content

Commit

Permalink
added a url rewrite rule to standardise url requests. At the moment a…
Browse files Browse the repository at this point in the history
…ll requests get accounted by the metrics module, including static assets. If later we want to we can selectively ignore specific paths.
  • Loading branch information
nustiueudinastea committed Mar 16, 2017
1 parent 7dbf7db commit 00017da
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
12 changes: 5 additions & 7 deletions api/metrics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}
};

Expand Down
8 changes: 8 additions & 0 deletions helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 00017da

Please sign in to comment.