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

Commit

Permalink
Merge pull request #81 from microservices-demo/fix-metrics-accounting
Browse files Browse the repository at this point in the history
Improve metrics accounting
  • Loading branch information
nustiueudinastea authored Mar 16, 2017
2 parents 7dbf7db + 7748f94 commit 30f0673
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
15 changes: 1 addition & 14 deletions api/metrics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,12 @@
return (diff[0] * 1e9 + diff[1]) / 1000000000;
}

function parse(path) {
var clean_path = path;

if (path[path.length - 1] != '/') {
if (!path.includes('.')) {
clean_path = path.substr(0, path.lastIndexOf('/') + 1);
}
};

return clean_path;
}

function observe(method, path, statusCode, start) {
var path = path.toLowerCase();
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 30f0673

Please sign in to comment.