Skip to content

Commit

Permalink
Adding metrics.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeal committed Sep 14, 2011
1 parent 38702ad commit 0591d97
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 74 deletions.
36 changes: 34 additions & 2 deletions node/handler/PadMessageHandler.js
Expand Up @@ -29,6 +29,7 @@ var securityManager = require("../db/SecurityManager");
var log4js = require('log4js');
var os = require("os");
var messageLogger = log4js.getLogger("message");
var metrics = require('../metrics')

/**
* A associative array that translates a session to a pad
Expand Down Expand Up @@ -151,6 +152,16 @@ exports.handleDisconnect = function(client)
* @param client the client that send this message
* @param message the message from the client
*/

var timers =
{ CLIENT_READY: metrics.timer('message.CLIENT_READY')
, notify_CLIENT_READY: metrics.timer('message.notify.CLIENT_READY')
, USER_CHANGES: metrics.timer('message.USER_CHANGES')
, USERINFO_UPDATE: metrics.timer('message.USERINFO_UPDATE')
, CHAT_MESSAGE: metrics.timer('message.CHAT_MESSAGE')
, CLIENT_MESSAGE: metrics.timer('message.CLIENT_MESSAGE')
}

exports.handleMessage = function(client, message)
{
if(message == null)
Expand All @@ -164,6 +175,13 @@ exports.handleMessage = function(client, message)
return;
}

client.starttime = new Date();
if (message.type === 'CLIENT_READY') {
client.timer = timers['CLIENT_READY']
} else if (message.data && message.data.type && timers[message.data.type]){
client.timer = timers[message.data.type]
}

//Check what type of message we get and delegate to the other methodes
if(message.type == "CLIENT_READY")
{
Expand All @@ -190,6 +208,7 @@ exports.handleMessage = function(client, message)
{
handleSuggestUserName(client, message);
}

//if the message type is unkown, throw an exception
else
{
Expand Down Expand Up @@ -252,7 +271,7 @@ function handleChatMessage(client, message)
{
socketio.sockets.sockets[pad2sessions[padId][i]].json.send(msg);
}

client.timer.update((new Date()) - client.starttime);
callback();
}
], function(err)
Expand Down Expand Up @@ -292,6 +311,7 @@ function handleSuggestUserName(client, message)
break;
}
}
client.timer.update((new Date()) - client.starttime)
}

/**
Expand Down Expand Up @@ -334,6 +354,7 @@ function handleUserInfoUpdate(client, message)
socketio.sockets.sockets[pad2sessions[padId][i]].json.send(message);
}
}
client.timer.update((new Date()) - client.starttime)
}

/**
Expand Down Expand Up @@ -401,6 +422,7 @@ function handleUserChanges(client, message)
{
console.warn("Can't apply USER_CHANGES "+changeset+", cause it faild checkRep");
client.json.send({disconnect:"badChangeset"});
client.timer.update((new Date()) - client.starttime)
return;
}

Expand Down Expand Up @@ -447,6 +469,7 @@ function handleUserChanges(client, message)
{
console.warn("Can't apply USER_CHANGES "+changeset+" with oldLen " + Changeset.oldLen(changeset) + " to document of length " + prevText.length);
client.json.send({disconnect:"badChangeset"});
client.timer.update((new Date()) - client.starttime)
callback();
return;
}
Expand All @@ -466,6 +489,7 @@ function handleUserChanges(client, message)
}

exports.updatePadClients(pad, callback);
client.timer.update((new Date()) - client.starttime)
}
], function(err)
{
Expand Down Expand Up @@ -813,6 +837,8 @@ function handleClientReady(client, message)
sessioninfos[client.id].rev = pad.getHeadRevisionNumber();
sessioninfos[client.id].author = author;

client.timer.update((new Date()) - client.starttime)

//prepare the notification for the other users on the pad, that this user joined
var messageToTheOtherUsers = {
"type": "COLLABROOM",
Expand All @@ -833,6 +859,8 @@ function handleClientReady(client, message)
messageToTheOtherUsers.data.userInfo.name = authorName;
}

var starttime = new Date()

//Run trough all sessions of this pad
async.forEach(pad2sessions[message.padId], function(sessionID, callback)
{
Expand Down Expand Up @@ -883,11 +911,15 @@ function handleClientReady(client, message)
}
}
};

client.json.send(messageToNotifyTheClientAboutTheOthers);
}
}
], callback);
}, callback);
}, function () {
callback()
timers['notify_CLIENT_READY'].update((new Date()) - starttime)
});
}
],function(err)
{
Expand Down
2 changes: 1 addition & 1 deletion node/handler/SocketIORouter.js
Expand Up @@ -127,7 +127,7 @@ exports.setSocketIO = function(_socket)
}
//drop message
else
{
{
messageLogger.warn("Droped message cause of bad permissions:" + stringifyWithoutPassword(message));
}
}
Expand Down
29 changes: 29 additions & 0 deletions node/metrics.js
@@ -0,0 +1,29 @@
var metrics = require('metrics')
;

var server = new metrics.Server()

exports.timer = function (ns) {
var x = new metrics.Timer()
server.addMetric(ns, x)
return x
}

exports.histogram = function (ns) {
var x = metrics.Histogram.createExponentialDecayHistogram(5000)
server.addMetric(ns, x)
return x
}

exports.listen = function () {
server.listen.apply(server, arguments)
}

var port = 9091

if (process.argv.indexOf('metrics') !== -1) {
if (parseInt(process.argv[process.argv.indexOf('metrics') + 1])) {
port = parseInt(process.argv[process.argv.indexOf('metrics') + 1])
}
exports.listen(port)
}

0 comments on commit 0591d97

Please sign in to comment.