Skip to content

Commit

Permalink
Merge pull request #44 from Financial-Times/method
Browse files Browse the repository at this point in the history
splitting express metrics by request method
  • Loading branch information
wheresrhys committed Sep 2, 2015
2 parents 8dd33cb + 1b57d2a commit 79e1773
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/metrics/express/http-response-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ HttpResponse.prototype.measure = function (name, value) {
};

HttpResponse.prototype.instrument = function(res) {

if (!res._nextInstrumentedV2) {
res.writeHead = this._writeHead(res.writeHead, Date.now());
res.writeHead = this._writeHead(res.writeHead, res.req.method, Date.now());
res.render = (function() {
var _render = res.render.bind(res),
httpResponse = this;
Expand All @@ -33,7 +34,7 @@ HttpResponse.prototype.instrument = function(res) {
returnValue = _render(template, data, callback),
diff = process.hrtime(start);

httpResponse.measure('template_render.' + template, (diff[0] * 1e9) + diff[1]);
httpResponse.measure('express.default_route_' + res.req.method + '.res.template_render.' + template, (diff[0] * 1e9) + diff[1]);
return returnValue;
};

Expand All @@ -53,7 +54,7 @@ HttpResponse.prototype.reset = function() {
HttpResponse.prototype.counts = function() {
var metrics = {};
Object.keys(this.buckets).forEach(function (key) {
var safeKey = 'express.default_route.res.' + (key.indexOf('template' > -1) ? key.replace(/(!?:template_render)[\/\\\.]/g, '_') : key);
var safeKey = (key.indexOf('template' > -1) ? key.replace(/(!?:template_render)[\/\\\.]/g, '_') : key);
metrics[safeKey + '.count'] = this.buckets[key].counter.count;
metrics[safeKey + '.time.sum'] = this.buckets[key].histogram.sum;
metrics[safeKey + '.time.max'] = this.buckets[key].histogram.max;
Expand All @@ -73,13 +74,13 @@ HttpResponse.prototype.reporter = function() {


// proxy for res.writeHead - http://nodejs.org/api/http.html#http_response_writehead_statuscode_reasonphrase_headers
HttpResponse.prototype._writeHead = function(fn, dt) {
HttpResponse.prototype._writeHead = function(fn, method, dt) {
var self = this;
return scarlet
.intercept(fn)
.using(function(invocation, proceed) {
var statusCode = parseInt(('' + invocation.args[0]).toString().substr(0, 3));
self.measure('status.' + statusCode, Date.now() - dt);
self.measure('express.default_route_' + method + '.res.status.' + statusCode, Date.now() - dt);
proceed();
}).proxy();
};

0 comments on commit 79e1773

Please sign in to comment.