Skip to content

Commit

Permalink
Adding some more stats
Browse files Browse the repository at this point in the history
Following now show in /info command
* Configuration
* Current Masters / Slaves
* Total commands, ReadOnly/Mutating,
* Number of times redis server went up/down
* Number of errors from server
  • Loading branch information
sreeix committed Sep 17, 2012
1 parent 7eea294 commit faf34fe
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 33 deletions.
2 changes: 1 addition & 1 deletion lib/redis_proxy.js
Expand Up @@ -95,7 +95,7 @@ Object.defineProperty(RedisProxy.prototype, 'active', {
// balancing strategies
RedisProxy.prototype.readsToSlaves = function(command, id, callback) {
stats.incr("commands");
stats.incr("commands:"+command);
// stats.incr("commands:"+command);
if(redisCommand.readOnly(command)) {
stats.incr("commands:read");
logger.debug('Read only command sending to the slave');
Expand Down
5 changes: 5 additions & 0 deletions lib/server.js
Expand Up @@ -6,6 +6,7 @@ var Pool = require('connection_pool');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var logger = require('winston');
var stats = require('./stats');


var Server = module.exports = function(serverInfo){
Expand Down Expand Up @@ -99,6 +100,8 @@ Server.prototype._createConnections = function(){
Server.prototype.slave = function(server){
var self = this;
logger.info('Marking '+ this.host + ':' + this.port + ' as slave of '+ server.host+': '+ server.port);
stats.incr("slave:"+ this.host + ':' + this.port);
stats.push("slaves", this.host + ':' + this.port);
this.client.slaveof(server.client.host, server.client.port, function(err, message){
if(err){
return logger.error(err);
Expand All @@ -111,6 +114,8 @@ Server.prototype.slave = function(server){
Server.prototype._master = function (){
var self = this;
logger.info(this.options.host+":"+this.options.port+ " is slave of no one");
stats.incr("master:"+ this.host + ':' + this.port);
stats.set("master", this.host + ':' + this.port);
this.client.slaveof('no', 'one', function(err, message){
if(err){
return logger.error(err);
Expand Down
10 changes: 10 additions & 0 deletions lib/stats.js
@@ -1,19 +1,29 @@
var _ = require('underscore');

var data = {};
module.exports = Stats = {
incr: function(item){
if(!data[item]) data[item] = 0;
return ++data[item];
},

decr: function(item){
if(!data[item]) data[item] = 0;
return --data[item];
},

value: function(item){
return data[item] || 0;
},

set: function(item, value){
data[item] = value;
},
push : function(item, value){
if(!data[item]) data[item] = []
data[item].push(value);
},

all: function(){
return data;
}
Expand Down
64 changes: 33 additions & 31 deletions lib/webui.js
@@ -1,39 +1,41 @@
var express = require('express');
var stats = require('./stats');
var app = express();
module.exports = function(config){
var express = require('express');
var stats = require('./stats');
var app = express();

app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);

app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(logErrors);
app.use(clientErrorHandler);
app.use(errorHandler);
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(logErrors);
app.use(clientErrorHandler);
app.use(errorHandler);

function logErrors(err, req, res, next) {
console.error(err.stack);
next(err);
}

function clientErrorHandler(err, req, res, next) {
if (req.xhr) {
res.send(500, { error: 'Something blew up!' });
} else {
function logErrors(err, req, res, next) {
console.error(err.stack);
next(err);
}
}

function errorHandler(err, req, res, next) {
res.status(500);
res.render('error', { error: err });
}
function clientErrorHandler(err, req, res, next) {
if (req.xhr) {
res.send(500, { error: 'Something blew up!' });
} else {
next(err);
}
}

function errorHandler(err, req, res, next) {
res.status(500);
res.render('error', { error: err });
}

app.get('/info', function(req, res){
res.send(stats.all());
});
app.get('/info', function(req, res){
res.send({stats: stats.all(), config: config});
});

app.listen(3000);
console.log('Listening on port 3000');
app.listen(3000);
console.log('Listening on port 3000');
};
2 changes: 1 addition & 1 deletion server.js
Expand Up @@ -49,7 +49,7 @@ redis_proxy.watch();

if(config.show_web_ui){
logger.info("Starting the web UI.");
require('./lib/webui');
require('./lib/webui')(config);
}

server.listen(listenPort, bindAddress);
Expand Down

0 comments on commit faf34fe

Please sign in to comment.