forked from elastic/apm-agent-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestify.js
52 lines (44 loc) · 1.5 KB
/
restify.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
/*
* 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';
const semver = require('semver');
const shimmer = require('../shimmer');
module.exports = function (restify, agent, { version, enabled }) {
if (!enabled) {
return restify;
}
if (!semver.satisfies(version, '>=5.2.0 <12.0.0')) {
agent.logger.debug('restify version %s not supported, skipping', version);
return restify;
}
agent.setFramework({ name: 'restify', version, overwrite: false });
function patchServer(server) {
if (semver.gte(version, '7.0.0')) {
shimmer.wrap(server, '_onHandlerError', function (orig) {
return function _wrappedOnHandlerError(err, req, res, isUncaught) {
if (err)
agent.captureError(err, { request: req, handled: !isUncaught });
return orig.apply(this, arguments);
};
});
} else {
shimmer.wrap(server, '_emitErrorEvents', function (orig) {
return function _wrappedOnHandlerError(req, res, route, err, cb) {
if (err) agent.captureError(err, { request: req });
return orig.apply(this, arguments);
};
});
}
}
shimmer.wrap(restify, 'createServer', function (fn) {
return function wrappedCreateServer() {
const server = fn.apply(this, arguments);
patchServer(server);
return server;
};
});
return restify;
};