Last active
August 29, 2015 14:07
-
-
Save Unitech/91f4a10ca661eb7862ad to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var redisWrapper = require('./redisWrapper.js'); | |
var events = require('events'); | |
var redis = require('redis'); | |
var client = redis.createClient(); | |
/** | |
* Global event emitter | |
*/ | |
global._ev = new events.EventEmitter(); | |
/** | |
* Wrap redis | |
*/ | |
redisWrapper(redis); | |
global._ev.on('query', function(dt) { | |
console.log(dt); | |
}); | |
/** | |
* Do queries | |
*/ | |
setInterval(function() { | |
client.set("string key", "string val", redis.print); | |
client.hset(["hash key", "hashtest 2", "some other value"], redis.print); | |
}, 3000); |
This file contains hidden or 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
var debug = require('debug')('proxy'); | |
// var cls = require('continuation-local-storage'); | |
// var ns = cls.createNamespace('namespace'); | |
var Proxy = module.exports = { | |
wrap : function(object, methods, hook) { | |
var self = this; | |
if (!Array.isArray(methods)) methods = [methods]; | |
methods.forEach(function(method) { | |
var original = object[method]; | |
if (!original) return debug('Method %s unknown', method); | |
if (original.__axm_original) return debug('Already wrapped', object); | |
var hooked = hook(original); | |
hooked.__axm_original = original; | |
object[method] = hooked; | |
debug('Method proxified'); | |
}); | |
} | |
}; |
This file contains hidden or 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
var Proxy = require('./proxy.js'); | |
var RedisWrap = module.exports = function(redis) { | |
Proxy.wrap(redis.RedisClient.prototype, 'send_command', function(send_command) { | |
return function() { | |
var cmd = arguments[0]; | |
var fn_args = arguments[arguments.length - 1]; | |
var cb = fn_args[fn_args.length - 1]; | |
var args = arguments; | |
var begin = Date.now(); | |
function end() { | |
global._ev.emit('query', { | |
time : Date.now() - begin, | |
type : 'redis', | |
at : Date.now(), | |
query : fn_args, | |
cmd : cmd | |
}); | |
} | |
if (typeof cb === 'function') | |
cb = end(cb); | |
else | |
end(); | |
send_command.apply(this, arguments); | |
}; | |
}); | |
return redis; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment