Skip to content

Commit 2d198b5

Browse files
committed
fix(redis): support new internal methods
1 parent d0dbb46 commit 2d198b5

6 files changed

Lines changed: 201 additions & 143 deletions

File tree

lib/instrumentations/core/http/util.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// eslint-disable-next-line no-useless-escape
12
var DATA_URI_REGEX = /(data:([a-z]+\/[a-z0-9\-\+]+(;[a-z\-]+\=[a-z0-9\-]+)?)?(;base64)?,)([a-z0-9\!\$\&\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*)$/i
23

34
function formatDataUrl (text) {

lib/instrumentations/redis.js

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,57 @@
1+
var flatMap = require('lodash.flatmap')
2+
13
var Shimmer = require('../utils/shimmer')
24
var consts = require('../consts')
5+
var utils = require('./utils')
6+
var instrumentedCommands = require('./utils').redisTools.instrumentedCommands
37

4-
module.exports = function wrap (redis, agent) {
5-
Shimmer.wrap(redis.RedisClient.prototype, 'redis.RedisClient.prototype', 'send_command', function (original) {
6-
return function () {
7-
var spanId = agent.generateSpanId()
8-
var clientSendTime = agent.getMicrotime()
9-
var requestId = agent.getTransactionId()
10-
11-
var host = this.address
12-
13-
var args = Array.prototype.slice.apply(arguments)
14-
var command = args[0]
15-
var last = args[args.length - 1]
8+
module.exports = function wrap (redis, agent, pkg) {
9+
var _instrumentedCommands = flatMap(Object.keys(instrumentedCommands), function (key) {
10+
return instrumentedCommands[key]
11+
})
1612

17-
var wrappedCallback = function (original) {
18-
return function (err) {
19-
agent.clientReceive({
13+
Shimmer.wrap(redis.RedisClient.prototype, '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)
18+
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 host = this.address
27+
var commands = this.__trace
28+
return utils.wrapQuery.call(this, originalExec, args, agent, {
29+
protocol: consts.PROTOCOLS.REDIS,
30+
host: host,
31+
method: 'multi: ' + commands.join(', '),
32+
url: 'unknown'
33+
})
34+
}
35+
return multi
36+
} else {
37+
return utils.wrapQuery.call(this, original, args, agent, {
2038
protocol: consts.PROTOCOLS.REDIS,
21-
id: requestId,
22-
spanId: spanId,
2339
host: host,
24-
time: clientSendTime,
25-
url: 'unknown', // TODO(c/KRU7H3D1): add support for url parameter with redis key
26-
method: command,
27-
mustCollect: err ? consts.MUST_COLLECT.ERROR : undefined,
28-
responseTime: agent.getMicrotime() - clientSendTime,
29-
status: err ? consts.EDGE_STATUS.NOT_OK : consts.EDGE_STATUS.OK,
30-
statusCode: err ? err.code : 200
40+
method: name,
41+
url: 'unknown'
3142
})
32-
return original.apply(this, arguments)
3343
}
3444
}
35-
36-
if (last && typeof last === 'function') {
37-
args[args.length - 1] = wrappedCallback(last)
38-
} else if (Array.isArray(last) && typeof last[last.length - 1] === 'function') {
39-
last[last.length - 1] = wrappedCallback(last[last.length - 1])
40-
} else {
41-
args.push(wrappedCallback(function () { }))
45+
})
46+
47+
Shimmer.wrap(redis.Multi.prototype, 'redis.Multi.prototype', _instrumentedCommands,
48+
function (original, name) {
49+
return function () {
50+
var args = Array.prototype.slice.apply(arguments)
51+
this.__trace.push(name)
52+
return original.apply(this, args)
4253
}
43-
44-
agent.clientSend({
45-
id: requestId,
46-
spanId: spanId,
47-
host: host,
48-
time: clientSendTime,
49-
method: command,
50-
type: agent.CLIENT_SEND,
51-
url: 'unknown'
52-
})
53-
54-
return original.apply(this, args)
55-
}
56-
})
54+
})
5755

5856
return redis
5957
}

lib/instrumentations/redis.spec.e2e.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ describe('redis module wrapper', function () {
2323

2424
it('should instrument operation test #1', function (done) {
2525
var agent = fakeAgent(this.sandbox)
26-
var redis = wrap(require('redis'), agent)
26+
var redis = wrap(require('redis'), agent, {
27+
version: '2.0.0'
28+
})
2729
var client = redis.createClient(REDIS_URL)
2830

2931
client.sadd('x', 6, function () {
@@ -45,7 +47,9 @@ describe('redis module wrapper', function () {
4547

4648
it('should instrument operation test #2', function () {
4749
var agent = fakeAgent(this.sandbox)
48-
var redis = wrap(require('redis'), agent)
50+
var redis = wrap(require('redis'), agent, {
51+
version: '2.0.0'
52+
})
4953
var client = redis.createClient(REDIS_URL)
5054

5155
for (var i = 0; i < 9; ++i) {
@@ -57,18 +61,28 @@ describe('redis module wrapper', function () {
5761

5862
it('should work with multi', function (done) {
5963
var agent = fakeAgent(this.sandbox)
60-
var redis = wrap(require('redis'), agent)
64+
var redis = wrap(require('redis'), agent, {
65+
version: '2.0.0'
66+
})
6167

6268
var client = redis.createClient(REDIS_URL)
6369

64-
client.multi()
65-
.sadd('x', 6)
66-
.srem('x', 7)
67-
.exec(function () {
70+
var multi1 = client.multi().sadd('x', 6)
71+
var multi2 = client.multi().sadd('x', 5)
72+
73+
multi1.srem('x', 6)
74+
multi2.zadd('x', 5)
75+
76+
multi1.exec(function () {
77+
expect(agent.clientReceive).to.have.been.called
78+
expect(agent.clientReceive.args[0][0].method)
79+
.to.eql('multi: sadd, srem')
80+
multi2.exec(function () {
81+
expect(agent.clientReceive.args[1][0].method)
82+
.to.eql('multi: sadd, zadd')
6883
expect(agent.clientReceive).to.have.been.called
6984
done()
7085
})
71-
72-
expect(agent.clientSend).to.have.callCount(4)
86+
})
7387
})
7488
})

0 commit comments

Comments
 (0)