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

Commit 30f0673

Browse files
Merge pull request #81 from microservices-demo/fix-metrics-accounting
Improve metrics accounting
2 parents 7dbf7db + 7748f94 commit 30f0673

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

api/metrics/index.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,12 @@
1818
return (diff[0] * 1e9 + diff[1]) / 1000000000;
1919
}
2020

21-
function parse(path) {
22-
var clean_path = path;
23-
24-
if (path[path.length - 1] != '/') {
25-
if (!path.includes('.')) {
26-
clean_path = path.substr(0, path.lastIndexOf('/') + 1);
27-
}
28-
};
29-
30-
return clean_path;
31-
}
32-
3321
function observe(method, path, statusCode, start) {
3422
var path = path.toLowerCase();
3523
if (path !== '/metrics' && path !== '/metrics/') {
3624
var duration = s(start);
3725
var method = method.toLowerCase();
38-
var clean_path = parse(path);
39-
metric.http.requests.duration.labels(method, clean_path, statusCode).observe(duration);
26+
metric.http.requests.duration.labels(method, path, statusCode).observe(duration);
4027
}
4128
};
4229

helpers/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@
5151
res.end();
5252
}
5353

54+
/* Rewrites and redirects any url that doesn't end with a slash. */
55+
helpers.rewriteSlash = function(req, res, next) {
56+
if(req.url.substr(-1) == '/' && req.url.length > 1)
57+
res.redirect(301, req.url.slice(0, -1));
58+
else
59+
next();
60+
}
61+
5462
/* Public: performs an HTTP GET request to the given URL
5563
*
5664
* url - the URL where the external service can be reached out

server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ var request = require("request")
1515
, metrics = require("./api/metrics")
1616
, app = express()
1717

18-
app.use(express.static("public"));
18+
19+
app.use(helpers.rewriteSlash);
1920
app.use(metrics);
21+
app.use(express.static("public"));
2022
if(process.env.SESSION_REDIS) {
2123
console.log('Using the redis based session manager');
2224
app.use(session(config.session_redis));

0 commit comments

Comments
 (0)