-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathws.js
59 lines (49 loc) · 1.56 KB
/
ws.js
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
/*
* Copyright Elasticsearch B.V. and other contributors where applicable.
* Licensed under the BSD 2-Clause License; you may not use this file except in
* compliance with the BSD 2-Clause License.
*/
'use strict';
var semver = require('semver');
var shimmer = require('../shimmer');
module.exports = function (ws, agent, { version, enabled }) {
if (!enabled) return ws;
if (!semver.satisfies(version, '>=1 <8')) {
agent.logger.debug('ws version %s not supported - aborting...', version);
return ws;
}
const ins = agent._instrumentation;
agent.logger.debug('shimming ws.prototype.send function');
shimmer.wrap(ws.prototype, 'send', wrapSend);
return ws;
function wrapSend(orig) {
return function wrappedSend() {
agent.logger.debug('intercepted call to ws.prototype.send');
const span = ins.createSpan(
'Send WebSocket Message',
'websocket',
'send',
{ exitSpan: true },
);
if (!span) {
return orig.apply(this, arguments);
}
const args = Array.prototype.slice.call(arguments);
let cb = args[args.length - 1];
const onDone = function () {
span.end();
if (cb) {
cb.apply(this, arguments);
}
};
if (typeof cb === 'function') {
args[args.length - 1] = ins.bindFunction(onDone);
} else {
cb = null;
args.push(ins.bindFunction(onDone));
}
const spanRunContext = ins.currRunContext().enterSpan(span);
return ins.withRunContext(spanRunContext, orig, this, ...args);
};
}
};