-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathplugin-redis.ts
257 lines (236 loc) · 6.62 KB
/
plugin-redis.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright 2015 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as shimmer from 'shimmer';
function createCreateClientWrap(api) {
return function createClientWrap(createClient) {
return function createClientTrace() {
// eslint-disable-next-line prefer-rest-params
const client = createClient.apply(this, arguments);
api.wrapEmitter(client);
return client;
};
};
}
// Used for redis version > 2.3
function createCreateStreamWrap(api) {
return 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) {
api.wrapEmitter(val);
this._google_trace_stream = val;
},
});
}
// eslint-disable-next-line prefer-rest-params
return create_stream.apply(this, arguments);
};
};
}
// Used for redis version <= 2.3
function createStreamListenersWrap(api) {
return function streamListenersWrap(install_stream_listeners) {
return function install_stream_listeners_trace() {
api.wrapEmitter(this.stream);
// eslint-disable-next-line prefer-rest-params
return install_stream_listeners.apply(this, arguments);
};
};
}
function setupSpan(api, cmd, args, skipped_frames) {
const span = api.createChildSpan({
name: 'redis-' + cmd,
skipFrames: skipped_frames + 1,
});
span.addLabel('command', cmd);
if (api.enhancedDatabaseReportingEnabled()) {
span.addLabel('arguments', JSON.stringify(args));
}
return span;
}
function startSpanFromArguments(api, cmd, args, cb, send_command) {
// If the arguments cannot be processed in this plugin, let redis process
// them so that if they are incorrect, redis reports an error instead of
// this plugin.
if (
!cmd ||
!args ||
typeof cmd !== 'string' ||
!Array.isArray(args) ||
(cb && typeof cb !== 'function')
) {
return send_command(cmd, args, cb);
}
if (!cb) {
if (
typeof args[args.length - 1] === 'function' ||
typeof args[args.length - 1] === 'undefined'
) {
cb = args.pop();
}
}
const span = setupSpan(api, cmd, args, 1);
if (!api.isRealSpan(span)) {
return send_command(cmd, args, cb);
}
return send_command(cmd, args, wrapCallback(api, span, cb));
}
function createInternalSendCommandWrap(api) {
return function internalSendCommandWrap(internal_send_command) {
return function internal_send_command_trace(cmd, args, cb) {
if (arguments.length === 1 && typeof cmd === 'object') {
// New versions of redis (2.4+) use a single options object instead
// of separate named arguments.
const span = setupSpan(api, cmd.command, cmd.args, 0);
if (!api.isRealSpan(span)) {
return internal_send_command.call(this, cmd);
}
cmd.callback = wrapCallback(api, span, cmd.callback);
return internal_send_command.call(this, cmd);
}
return startSpanFromArguments(
api,
cmd,
args,
cb,
internal_send_command.bind(this)
);
};
};
}
function createSendCommandWrap(api) {
return function sendCommandWrap(send_command) {
return function send_command_trace(cmd, args, cb) {
return startSpanFromArguments(
api,
cmd,
args,
cb,
send_command.bind(this)
);
};
};
}
function wrapCallback(api, span, done) {
const fn = function (err, res) {
if (api.enhancedDatabaseReportingEnabled()) {
if (err) {
span.addLabel('error', err);
}
if (res) {
span.addLabel('result', res);
}
}
span.endSpan();
if (done) {
done(err, res);
}
};
return api.wrap(fn);
}
function wrapInternalSendCommand(redis, api) {
shimmer.wrap(
redis.RedisClient.prototype,
'internal_send_command',
createInternalSendCommandWrap(api)
);
}
function unwrapInternalSendCommand(redis) {
shimmer.unwrap(redis.RedisClient.prototype, 'internal_send_command');
}
function wrapCreateClient(redis, api) {
shimmer.wrap(redis, 'createClient', createCreateClientWrap(api));
}
function unwrapCreateClient(redis) {
shimmer.unwrap(redis, 'createClient');
}
function wrapCreateStream(redis, api) {
shimmer.wrap(
redis.RedisClient.prototype,
'create_stream',
createCreateStreamWrap(api)
);
}
function unwrapCreateStream(redis) {
shimmer.unwrap(redis.RedisClient.prototype, 'create_stream');
}
function wrapSendCommand(redis, api) {
shimmer.wrap(
redis.RedisClient.prototype,
'send_command',
createSendCommandWrap(api)
);
}
function unwrapSendCommand(redis) {
shimmer.unwrap(redis.RedisClient.prototype, 'send_command');
}
function wrapInstallStreamListeners(redis, api) {
shimmer.wrap(
redis.RedisClient.prototype,
'install_stream_listeners',
createStreamListenersWrap(api)
);
}
function unwrapInstallStreamListeners(redis) {
shimmer.unwrap(redis.RedisClient.prototype, 'install_stream_listeners');
}
module.exports = [
{
file: '',
versions: '>=2.6 <4.0',
patch: function (redis, api) {
wrapCreateStream(redis, api);
wrapInternalSendCommand(redis, api);
wrapCreateClient(redis, api);
},
unpatch: function (redis) {
unwrapCreateStream(redis);
unwrapInternalSendCommand(redis);
unwrapCreateClient(redis);
},
},
{
file: '',
versions: '>2.3 <2.6',
patch: function (redis, api) {
wrapSendCommand(redis, api);
wrapCreateStream(redis, api);
wrapCreateClient(redis, api);
},
unpatch: function (redis) {
unwrapSendCommand(redis);
unwrapCreateStream(redis);
unwrapCreateClient(redis);
},
},
{
file: '',
versions: '<=2.3',
patch: function (redis, api) {
wrapSendCommand(redis, api);
wrapInstallStreamListeners(redis, api);
wrapCreateClient(redis, api);
},
unpatch: function (redis) {
unwrapSendCommand(redis);
unwrapInstallStreamListeners(redis);
unwrapCreateClient(redis);
},
},
];
export default {};