Skip to content

Commit

Permalink
custom start, stop timer added
Browse files Browse the repository at this point in the history
  • Loading branch information
Prasanna-sr committed Jul 17, 2015
1 parent 3e1fff0 commit 33db9da
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 24 deletions.
43 changes: 28 additions & 15 deletions examples/express4.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,36 @@ var instrumentation = require('./../index.js');


instrumentation(app, {
responseTime: instrumentation.httpResponseTime(500)
responseTime: instrumentation.httpResponseTime(200)
}, function(req, res) {
//log data here
logData(req, res);
});

function logData(req, res) {
console.log('notify callback called !');
console.log('/***********************************************************/');
console.log('/********************** REQUEST HEADERS *********************/');
console.log('/************************************************************/');
console.log(req.headers);
console.log(req.timers);
});
console.log('URL route :' + req.originalUrl);
console.log('\n');
console.log('/***************************************************************/');
console.log('/********************* MIDDLEWARE TIMERS ***********************/');
console.log('/***************************************************************/');
console.log(req.timers.value);

}

app.use(function prkApp1(req, res, next) {
next();
});

app.use(function prkApp2(req, res, next) {
setTimeout(function (){
setTimeout(function() {
next();
}, 111);

});
app.use(router);

Expand All @@ -36,19 +48,20 @@ router.use(function prkRouter1(req, res, next) {

});

// function customFn() {
// req.timer.startTime();
// //
// req.timer.EndTime();
function customFunction(req, cb) {
req.timers.start('customFunctioN');

// }

router.get('/test', function test(req, res, next) {
setTimeout(function() {
res.status(200).send('test dfsd');
next();
}, 111);
req.timers.stop();
cb();
}, 500);
}

router.get('/test', function test(req, res, next) {
customFunction(req, function() {
res.status(200).send('test dfsd');
next();
});
});


Expand Down
50 changes: 42 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var expressSendMethods = ['send', 'json', 'jsonp', 'redirect', 'sendStatus', 'render', 'sendfile','sendFile'];
var expressSendMethods = ['send', 'json', 'jsonp', 'redirect', 'sendStatus', 'render', 'sendfile', 'sendFile'];
var httpResponseTime = require('./rules/httpResponseTime');

module.exports = instrument;
Expand All @@ -7,24 +7,56 @@ instrument.httpResponseTime = httpResponseTime;
function instrument(app, rulesObj, notifyCallback) {

app.use(function(req, res, next) {
req.timers = [];
req.timers = function() {};
req.timers.value = [];
var startTime = new Date().getTime();
req.timers.start = function(name) {
for (var i = req.timers.value.length - 1; i >= 0; i--) {
var tempObj = req.timers.value[i];
var key = Object.keys(tempObj)[0];
var value = tempObj[key];
if (value === -1) {
key = key + ' -> ' + name;
var timerObj = {};
timerObj[key] = {};
timerObj[key]['time'] = new Date().getTime();
timerObj[key]['init'] = -1;
req.timers.value.push(timerObj);
break;
}
}
}

req.timers.stop = function() {
for (var i = req.timers.value.length - 1; i >= 0; i--) {
var tempObj = req.timers.value[i];
var key = Object.keys(tempObj)[0];
var value = tempObj[key]['init'];
if (value === -1) {
var timerObj = {};
timerObj[key] = new Date().getTime() - req.timers.value[i][key]['time'];
req.timers.value[i] = timerObj;
break;
}

}
}
overrideMethods(res, expressSendMethods, responseSend);

function responseSend(responseFn) {
return function() {
req.timers.push({
req.timers.value.push({
'$finalTimer': (new Date().getTime() - startTime)
});
var keys = Object.keys(rulesObj);
var shouldNotify = keys.some(function(key) {
var fn = rulesObj[key];
return fn(req, res);
});
if(shouldNotify) {
notifyCallback(req, res);
if (shouldNotify) {
notifyCallback(req, res);
}

return responseFn.apply(this, arguments);
}
}
Expand Down Expand Up @@ -92,9 +124,11 @@ function instrument(app, rulesObj, notifyCallback) {
} else {
$obj.name = 'anonymous';
}
$obj["timerObj"][$obj.name] = -1;
$obj.req.timers.value.push($obj["timerObj"]);
arguments[2] = function() {
$obj["timerObj"][$obj.name] = new Date().getTime() - $obj.counter;
$obj.req.timers.push($obj["timerObj"]);
$obj.req.timers.value.push($obj["timerObj"]);
nextFn.apply(this, arguments);
}
return middlewareFn.apply(this, arguments);
Expand All @@ -111,4 +145,4 @@ function instrument(app, rulesObj, notifyCallback) {
function overrideMethod(object, methodName, callback) {
object[methodName] = callback(object[methodName]);
}
}
}
2 changes: 1 addition & 1 deletion rules/httpResponseTime.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = function(milliseconds) {
return function(req, res) {
return req.timers.some(function(timer) {
return req.timers.value.some(function(timer) {
if (timer.$finalTimer > milliseconds) {
return true;
}
Expand Down

0 comments on commit 33db9da

Please sign in to comment.