Skip to content

Commit

Permalink
Merge pull request #6 from bolgovr/redis
Browse files Browse the repository at this point in the history
Redis
  • Loading branch information
bolgovr committed May 8, 2012
2 parents d46d97f + 80d090d commit 82daa18
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 2 deletions.
File renamed without changes.
29 changes: 29 additions & 0 deletions example/redis_transport.js
@@ -0,0 +1,29 @@
var express = require('express');
var Stats = require('../index.js').statCollector;
var opts = {'transport': 'redis', 'statServers': ['stats']};
var st = new Stats(opts);
var counters = {
'ips': function (req, res) {
return req.connection.remoteAddress;
},
'urls': function (req, res) {
return req.originalUrl;
},
'client': function (req, res) {
return req.headers['user-agent'] + "::" + req.connection.remoteAddress;
}
};
var app = express.createServer();
app.use(st.useCounters(counters)); //use counters as middleware
st.attach(app); //expose stats object through req.stats

app.get('/', function (req, res) {
res.send('/');
});
app.get('/ok', function (req, res) {
res.send('/ok');
req.stats.counter('ppls', Math.random());
});

app.listen(8080);
console.log('example app listen on 8080 port, for getting stat subscribe to redis channel \'stats\'');
5 changes: 5 additions & 0 deletions example/redis_viewer.js
@@ -0,0 +1,5 @@
var client = require('redis').createClient();
client.subscribe('stats');
client.on('message', function (channel, message) {
console.log('got message from ' + channel + ' : ' + message);
});
70 changes: 70 additions & 0 deletions lib/transports/redis.js
@@ -0,0 +1,70 @@
var redis = require('redis');
var Evt = require('events').EventEmitter;

var RedisClient = function (opts) {
this.statChannels = {};
this.communicator = redis.createClient();
this.intervals = {};
this.collectingTime = 1000; //1 sec
var self = this;
if (opts.statServers) {
opts.statServers.forEach(function (statSrv) {
self.addStatChannel(statSrv);
});
}
};

RedisClient.prototype = new Evt();

RedisClient.prototype.addStatChannel = function (channel) {
this.statChannels[channel.toString()] = {};
this.intervals = setInterval(this.pubStats.bind(this), this.collectingTime);
};
RedisClient.prototype.broadcast = function (msg, callback) {
var reciever = null;
for (var channel in this.statChannels) {
reciever = this.statChannels[channel];
if (!reciever[msg.eventName]) {
reciever[msg.eventName] = {
'start': msg.start
};
}
reciever = reciever[msg.eventName];
reciever.end = Date.now();
delete msg.end;
delete msg.eventName;
delete msg.start;
for (var prop in msg) {
if (reciever[prop]) {
reciever[prop] += msg[prop];
} else {
reciever[prop] = msg[prop];
}
}
}
};
RedisClient.prototype._cleanStats = function (channel) {
if (this.statChannels[channel]) {
this.statChannels[channel] = {};
}
};
RedisClient.prototype.removeStatChannel = function (channel) {
delete this.statChannels[channel];
};

RedisClient.prototype.pubStats = function () {
var pub = function (msg, channel) {
try {
msg = JSON.stringify(msg);
} catch (e) {
console.dir(e);
}
this.communicator.publish(channel, msg);
this._cleanStats(channel);
};
for (var i in this.statChannels) {
pub.call(this, this.statChannels[i], i);
}
};

module.exports = RedisClient;
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -2,7 +2,7 @@
"author": "Bolgov Roman <bolgovr@gmail.com>",
"name": "express-stat",
"description": "Monitoring and stats for express application",
"version": "0.0.2",
"version": "0.0.3",
"repository": {
"type": "git",
"url": "git://github.com/bolgovr/express-stat.git"
Expand All @@ -14,7 +14,8 @@
"node": "~0.6.8"
},
"dependencies": {
"eventemitter2": "~0.4.9"
"eventemitter2": "~0.4.9",
"redis": "~0.7.2"
},
"devDependencies": {
"mocha": "~1.0.1",
Expand Down

0 comments on commit 82daa18

Please sign in to comment.