Skip to content

Commit

Permalink
Support for redis 2.4
Browse files Browse the repository at this point in the history
Fixes #185
  • Loading branch information
Matt Loring committed Dec 18, 2015
1 parent d12c7d2 commit b36c3bb
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 8 deletions.
34 changes: 30 additions & 4 deletions lib/hooks/userspace/hook-redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var semver = require('semver');
var SpanData = require('../../span-data.js');
var agent;

var SUPPORTED_VERSIONS = '<=2.3.x';
var SUPPORTED_VERSIONS = '<=2.x';

function createClientWrap(createClient) {
return function createClientTrace() {
Expand All @@ -32,6 +32,23 @@ function createClientWrap(createClient) {
};
}

// Used for redis version > 2.3
function createStreamWrap(create_stream) {
return function create_stream_trace() {
if (!this.stream) {
Object.defineProperty(this, 'stream', {
get: function () { return this._google_trace_stream; },
set: function (val) {
cls.getNamespace().bindEmitter(val);
this._google_trace_stream = val;
}
});
}
return create_stream.apply(this, arguments);
};
}

// Used for redis version <= 2.3
function streamListenersWrap(install_stream_listeners) {
return function install_stream_listeners_trace() {
cls.getNamespace().bindEmitter(this.stream);
Expand Down Expand Up @@ -97,13 +114,22 @@ module.exports = function(version_, agent_) {
patch: function(redis) {
agent = agent_;
shimmer.wrap(redis.RedisClient.prototype, 'send_command', sendCommandWrap);
shimmer.wrap(redis.RedisClient.prototype, 'install_stream_listeners',
streamListenersWrap);
if (semver.satisfies(version_, '<=2.3.x')) {
shimmer.wrap(redis.RedisClient.prototype, 'install_stream_listeners',
streamListenersWrap);
} else {
shimmer.wrap(redis.RedisClient.prototype, 'create_stream',
createStreamWrap);
}
shimmer.wrap(redis, 'createClient', createClientWrap);
},
unpatch: function(redis) {
shimmer.unwrap(redis.RedisClient.prototype, 'send_command');
shimmer.unwrap(redis.RedisClient.prototype, 'install_stream_listeners');
if (semver.satisfies(version_, '<=2.3.x')) {
shimmer.unwrap(redis.RedisClient.prototype, 'install_stream_listeners');
} else {
shimmer.unwrap(redis.RedisClient.prototype, 'create_stream');
}
shimmer.unwrap(redis, 'createClient');
agent_.logger.info('Redis: unpatched');
}
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/hooks/fixtures/redis2.x/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('redis');
8 changes: 8 additions & 0 deletions test/hooks/fixtures/redis2.x/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "redis2",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"redis": "^2.2.5"
}
}
5 changes: 3 additions & 2 deletions test/hooks/test-trace-redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ var common = require('./common.js');
var assert = require('assert');
var versions = {
redis0: require('./fixtures/redis0.12'),
redis2: require('./fixtures/redis2'),
redisHiredis: require('./fixtures/redis2-hiredis0.4')
redis2dot3: require('./fixtures/redis2.3'),
redis2dotx: require('./fixtures/redis2.x'),
redisHiredis: require('./fixtures/redis2.3-hiredis0.4')
};

var client;
Expand Down
2 changes: 1 addition & 1 deletion test/standalone/test-hooks-no-project-num.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('should not break without project num', function() {

it('redis', function(done) {
var agent = require('../..').start();
var redis = require('../hooks/fixtures/redis2');
var redis = require('../hooks/fixtures/redis2.3');
var client = redis.createClient();
client.set('i', 1, function() {
client.quit(function() {
Expand Down
2 changes: 1 addition & 1 deletion test/standalone/test-hooks-sample-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var assert = require('assert');
var express = require('../hooks/fixtures/express4');
var http = require('http');
var mongoose = require('../hooks/fixtures/mongoose4');
var redis = require('../hooks/fixtures/redis2');
var redis = require('../hooks/fixtures/redis2.3');
var mysql = require('../hooks/fixtures/mysql2');
var debugCount = 0;
var oldDebug = agent.logger.debug;
Expand Down

0 comments on commit b36c3bb

Please sign in to comment.