From b36c3bbf9d5856a8104c6d1672c7da4e33758ad4 Mon Sep 17 00:00:00 2001 From: Matt Loring Date: Sun, 29 Nov 2015 15:45:39 -0800 Subject: [PATCH] Support for redis 2.4 Fixes #185 --- lib/hooks/userspace/hook-redis.js | 34 ++++++++++++++++--- .../index.js | 0 .../package.json | 0 .../fixtures/{redis2 => redis2.3}/index.js | 0 .../{redis2 => redis2.3}/package.json | 0 test/hooks/fixtures/redis2.x/index.js | 1 + test/hooks/fixtures/redis2.x/package.json | 8 +++++ test/hooks/test-trace-redis.js | 5 +-- test/standalone/test-hooks-no-project-num.js | 2 +- test/standalone/test-hooks-sample-warning.js | 2 +- 10 files changed, 44 insertions(+), 8 deletions(-) rename test/hooks/fixtures/{redis2-hiredis0.4 => redis2.3-hiredis0.4}/index.js (100%) rename test/hooks/fixtures/{redis2-hiredis0.4 => redis2.3-hiredis0.4}/package.json (100%) rename test/hooks/fixtures/{redis2 => redis2.3}/index.js (100%) rename test/hooks/fixtures/{redis2 => redis2.3}/package.json (100%) create mode 100644 test/hooks/fixtures/redis2.x/index.js create mode 100644 test/hooks/fixtures/redis2.x/package.json diff --git a/lib/hooks/userspace/hook-redis.js b/lib/hooks/userspace/hook-redis.js index 94d2fca56..913b6a494 100644 --- a/lib/hooks/userspace/hook-redis.js +++ b/lib/hooks/userspace/hook-redis.js @@ -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() { @@ -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); @@ -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'); } diff --git a/test/hooks/fixtures/redis2-hiredis0.4/index.js b/test/hooks/fixtures/redis2.3-hiredis0.4/index.js similarity index 100% rename from test/hooks/fixtures/redis2-hiredis0.4/index.js rename to test/hooks/fixtures/redis2.3-hiredis0.4/index.js diff --git a/test/hooks/fixtures/redis2-hiredis0.4/package.json b/test/hooks/fixtures/redis2.3-hiredis0.4/package.json similarity index 100% rename from test/hooks/fixtures/redis2-hiredis0.4/package.json rename to test/hooks/fixtures/redis2.3-hiredis0.4/package.json diff --git a/test/hooks/fixtures/redis2/index.js b/test/hooks/fixtures/redis2.3/index.js similarity index 100% rename from test/hooks/fixtures/redis2/index.js rename to test/hooks/fixtures/redis2.3/index.js diff --git a/test/hooks/fixtures/redis2/package.json b/test/hooks/fixtures/redis2.3/package.json similarity index 100% rename from test/hooks/fixtures/redis2/package.json rename to test/hooks/fixtures/redis2.3/package.json diff --git a/test/hooks/fixtures/redis2.x/index.js b/test/hooks/fixtures/redis2.x/index.js new file mode 100644 index 000000000..24b909cb7 --- /dev/null +++ b/test/hooks/fixtures/redis2.x/index.js @@ -0,0 +1 @@ +module.exports = require('redis'); diff --git a/test/hooks/fixtures/redis2.x/package.json b/test/hooks/fixtures/redis2.x/package.json new file mode 100644 index 000000000..57351f4c1 --- /dev/null +++ b/test/hooks/fixtures/redis2.x/package.json @@ -0,0 +1,8 @@ +{ + "name": "redis2", + "version": "1.0.0", + "main": "index.js", + "dependencies": { + "redis": "^2.2.5" + } +} diff --git a/test/hooks/test-trace-redis.js b/test/hooks/test-trace-redis.js index 061d67c50..f15e541be 100644 --- a/test/hooks/test-trace-redis.js +++ b/test/hooks/test-trace-redis.js @@ -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; diff --git a/test/standalone/test-hooks-no-project-num.js b/test/standalone/test-hooks-no-project-num.js index 0de15a8da..c0da3726a 100644 --- a/test/standalone/test-hooks-no-project-num.js +++ b/test/standalone/test-hooks-no-project-num.js @@ -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() { diff --git a/test/standalone/test-hooks-sample-warning.js b/test/standalone/test-hooks-sample-warning.js index 2d9ed2c3b..76dc944af 100644 --- a/test/standalone/test-hooks-sample-warning.js +++ b/test/standalone/test-hooks-sample-warning.js @@ -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;