Permalink
Browse files

Added lightRequests option to stats()

basically just reports the ids so that you can map responses etc
but this is much more ideal for "realtime" reporting. Might be worth
implementing UDP for this as well down the road
  • Loading branch information...
tj committed Apr 18, 2011
1 parent bf1bc51 commit 1776dce9bd98ee2bae40c699daf6aea13fb164d9
Showing with 43 additions and 12 deletions.
  1. +26 −12 lib/plugins/stats.js
  2. +17 −0 lib/utils.js
View
@@ -27,8 +27,14 @@ try {
*
* Options:
*
- * - `connections` enable connection statistics
- * - `requests` enable request statistics
+ * - `connections` enable connection statistics
+ * - `requests` enable request statistics
+ * - `lightRequests` enable light-weight request statistics
+ *
+ * Real-time applications should utilize `lightRequests` for reporting
+ * when possible, although less data is available.
+ *
+ * TODO: UDP
*
* @param {Object} options
* @return {Function}
@@ -43,6 +49,7 @@ module.exports = function(options){
var server = master.server;
master.connectionStats = options.connections;
master.requestStats = options.requests;
+ master.lightRequestStats = options.lightRequests;
// worker stats
if (master.isWorker) {
@@ -59,16 +66,22 @@ module.exports = function(options){
});
}
+ // light-weight requests
+ if (options.lightRequests) {
+ utils.unshiftListener(server, 'request', function(req, res){
+ master.call('reportStats', 'light request', res.id = ++id);
+ var end = res.end;
+ res.end = function(str, encoding){
+ res.end = end;
+ res.end(str, encoding);
+ master.call('reportStats', 'light request complete', res.id);
+ };
+ });
+ }
+
// requests
if (options.requests) {
- // force cluster's callback first
- if (Array.isArray(server._events.request)) {
- server._events.request.unshift(request);
- } else {
- server._events.request = [request, server._events.request];
- }
-
- function request(req, res){
+ utils.unshiftListener(server, 'request', function(req, res){
var data = {
remoteAddress: req.socket.remoteAddress
, headers: req.headers
@@ -85,8 +98,8 @@ module.exports = function(options){
res.end = end;
res.end(str, encoding);
master.call('reportStats', 'request complete', data);
- }
- };
+ };
+ });
}
// master stats
} else {
@@ -114,6 +127,7 @@ module.exports = function(options){
case 'disconnection':
worker.stats.connectionsActive--;
break;
+ case 'light request':
case 'request':
worker.stats.requestsTotal++;
}
View
@@ -78,4 +78,21 @@ exports.formatDateRange = function(a, b) {
if (diff < hour) return unit('minute', (diff / minute).toFixed(0));
if (diff < day) return unit('hour', (diff / hour).toFixed(0));
return unit('day', (diff / day).toFixed(1));
+};
+
+/**
+ * Unshift a callback.
+ *
+ * @param {Object} obj
+ * @param {String} event
+ * @param {String} fn
+ * @api private
+ */
+
+exports.unshiftListener = function(obj, event, fn){
+ if (Array.isArray(obj._events[event])) {
+ obj._events[event].unshift(fn);
+ } else {
+ obj._events[event] = [fn, obj._events[event]];
+ }
};

0 comments on commit 1776dce

Please sign in to comment.