forked from elastic/apm-agent-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkoa-router.js
63 lines (53 loc) · 1.73 KB
/
koa-router.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
60
61
62
63
/*
* 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 (Router, agent, { version, enabled }) {
if (!enabled) return Router;
if (!semver.satisfies(version, '>=5.2.0 <=12')) {
agent.logger.debug(
'koa-router version %s not supported - aborting...',
version,
);
return Router;
}
agent.logger.debug('shimming koa-router prototype.match function');
shimmer.wrap(Router.prototype, 'match', function (orig) {
return function (_, method) {
var matched = orig.apply(this, arguments);
if (typeof method !== 'string') {
agent.logger.debug(
'unexpected method type in koa-router prototype.match: %s',
typeof method,
);
return matched;
}
if (Array.isArray(matched && matched.pathAndMethod)) {
const layer = matched.pathAndMethod.find(function (layer) {
return layer && layer.opts && layer.opts.end === true;
});
var path = layer && layer.path;
if (typeof path === 'string') {
var name = method + ' ' + path;
agent._instrumentation.setDefaultTransactionName(name);
} else {
agent.logger.debug(
'unexpected path type in koa-router prototype.match: %s',
typeof path,
);
}
} else {
agent.logger.debug(
'unexpected match result in koa-router prototype.match: %s',
typeof matched,
);
}
return matched;
};
});
return Router;
};