Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from bolgovr/redis
Redis
- Loading branch information
Showing
5 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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\''); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters