Skip to content

Commit 5e05126

Browse files
committed
fix(instrumentation): redis multi typeerror
1 parent 7e29040 commit 5e05126

File tree

15 files changed

+390
-341
lines changed

15 files changed

+390
-341
lines changed

circle.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
machine:
2+
services:
3+
- redis
4+
- mysql
5+
- mongodb
6+
- postgresql
27
node:
38
version: 6.9.5
49
environment:

lib/instrumentations/trace-instrumentation-redis.js

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,74 @@ var flatMap = require('lodash.flatmap')
33
var Shimmer = require('../utils/shimmer')
44
var consts = require('../consts')
55
var utils = require('./utils')
6-
var instrumentedCommands = require('./utils').redisTools.instrumentedCommands
6+
var Module = require('module')
7+
var redisCommands = require('./utils').redisTools.instrumentedCommands
78

8-
module.exports = function wrap (redis, agent, pkg) {
9-
var _instrumentedCommands = flatMap(Object.keys(instrumentedCommands), function (key) {
10-
return instrumentedCommands[key]
11-
})
9+
var instrumentedCommands = flatMap(Object.keys(redisCommands), function (key) {
10+
return redisCommands[key]
11+
})
1212

13-
Shimmer.wrap(redis.RedisClient.prototype,
14-
_instrumentedCommands.concat(['multi']), function (original, name) {
15-
return function () {
16-
var host = this.address
17-
var args = Array.prototype.slice.apply(arguments)
13+
function wrapRedisClient (redis, agent, pkg) {
14+
Shimmer.wrap(redis.RedisClient.prototype, instrumentedCommands, function (original, name) {
15+
return function () {
16+
var host = this.address
17+
var args = Array.prototype.slice.apply(arguments)
18+
return utils.wrapQuery.call(this, original, args, agent, {
19+
continuationMethod: 'callback',
20+
protocol: consts.PROTOCOLS.REDIS,
21+
host: host || 'unknown',
22+
method: name,
23+
url: 'unknown'
24+
})
25+
}
26+
})
27+
return redis
28+
}
1829

19-
if (name === 'multi') {
20-
// start a multi
21-
var multi = original.apply(this, args)
22-
multi.__trace = []
23-
var originalExec = multi.exec
24-
multi.exec = function () {
25-
var args = Array.prototype.slice.apply(arguments)
26-
var commands = this.__trace
27-
return utils.wrapQuery.call(this, originalExec, args, agent, {
28-
continuationMethod: 'callback',
29-
protocol: consts.PROTOCOLS.REDIS,
30-
host: host || 'unknown',
31-
method: 'multi: ' + commands.join(', '),
32-
url: 'unknown'
33-
})
34-
}
35-
return multi
36-
} else {
37-
return utils.wrapQuery.call(this, original, args, agent, {
38-
continuationMethod: 'callback',
39-
protocol: consts.PROTOCOLS.REDIS,
40-
host: host || 'unknown',
41-
method: name,
42-
url: 'unknown'
43-
})
44-
}
30+
function wrapMulti (Multi, agent, pkg) {
31+
Shimmer.wrap(Multi.prototype, ['exec', 'EXEC', 'exec_transaction'], function (original, name) {
32+
return function () {
33+
var args = Array.prototype.slice.apply(arguments)
34+
var host = this._client && this._client.address
35+
var commands = this.__trace || []
36+
var methodString
37+
if (commands.length) {
38+
methodString = 'multi: ' + commands.join(', ')
39+
} else {
40+
methodString = 'multi'
4541
}
46-
})
42+
return utils.wrapQuery.call(this, original, args, agent, {
43+
continuationMethod: 'callback',
44+
protocol: consts.PROTOCOLS.REDIS,
45+
host: host || 'unknown',
46+
method: methodString,
47+
url: 'unknown'
48+
})
49+
}
50+
})
4751

48-
Shimmer.wrap(redis.Multi.prototype, _instrumentedCommands,
52+
Shimmer.wrap(Multi.prototype, instrumentedCommands,
4953
function (original, name) {
5054
return function () {
5155
var args = Array.prototype.slice.apply(arguments)
56+
this.__trace = this.__trace || []
5257
this.__trace.push(name)
5358
return original.apply(this, args)
5459
}
5560
})
61+
return Multi
62+
}
5663

57-
return redis
64+
module.exports = {
65+
instrumentations: [{
66+
path: 'redis',
67+
pre: function () {
68+
Module._load('redis/lib/multi', arguments[3])
69+
return Array.prototype.slice.call(arguments, 2)
70+
},
71+
post: wrapRedisClient
72+
}, {
73+
path: 'redis/lib/multi',
74+
post: wrapMulti
75+
}]
5876
}

0 commit comments

Comments
 (0)