-
Notifications
You must be signed in to change notification settings - Fork 230
/
Copy pathfastify.js
172 lines (146 loc) · 5.12 KB
/
fastify.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
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
/*
* 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';
// Instrumentation of fastify.
// https://www.fastify.io/docs/latest/LTS/
const semver = require('semver');
// Return the fastify route or request method, without hitting a Fastify
// deprecation warning.
// https://fastify.dev/docs/latest/Reference/Warnings/#FSTDEP018
function fastifyRouteMethod(req) {
if (req.routeOptions) {
// `.routeOptions` was added in [email protected].
return req.routeOptions.method;
} else {
return req.routerMethod || req.raw.method; // Fallback for fastify >3 <3.3.0
}
}
// Return the fastify route or request URL, without hitting a Fastify
// deprecation warning.
// https://fastify.dev/docs/latest/Reference/Warnings/#FSTDEP017
function fastifyRouteUrl(req, reply) {
if (req.routeOptions) {
// `.routeOptions` was added in [email protected].
// Note that `.routeOptions.url` might be undefined for a 404 route.
return req.routeOptions.url;
} else {
return req.routerPath || reply.context.config.url; // Fallback for fastify >3 <3.3.0
}
}
module.exports = function (
modExports,
agent,
{ version, enabled, isImportMod },
) {
if (!enabled) {
return modExports;
}
if (isImportMod && !semver.satisfies(version, '>=3.5.0')) {
// https://github.com/fastify/fastify/pull/2590
agent.logger.debug(
'ESM instrumentation of fastify requires fastify >=3.5.0, skipping instrumentation',
);
return modExports;
}
agent.setFramework({ name: 'fastify', version, overwrite: false });
agent.logger.debug('wrapping fastify build function');
const origFastify = isImportMod ? modExports.default : modExports;
var wrapper;
if (semver.gte(version, '3.0.0')) {
wrapper = wrappedFastify;
} else if (semver.gte(version, '2.0.0-rc')) {
wrapper = wrappedFastify2;
} else {
wrapper = wrappedFastify1;
}
// Assign all enumerable properties to the wrapper.
// - 'fastify' and 'default' are intentionally circular references to
// support various import/require styles. See:
// https://github.com/fastify/fastify/blob/v4.17.0/fastify.js#L814-L827
for (var prop in origFastify) {
switch (prop) {
case 'fastify':
case 'default':
wrapper[prop] = wrapper;
break;
default:
wrapper[prop] = origFastify[prop];
break;
}
}
if (isImportMod) {
modExports.fastify = wrapper;
modExports.default = wrapper;
return modExports;
} else {
return wrapper;
}
function wrappedFastify() {
const _fastify = origFastify.apply(null, arguments);
agent.logger.debug('adding onRequest hook to fastify');
_fastify.addHook('onRequest', (req, reply, next) => {
const method = fastifyRouteMethod(req);
const url = fastifyRouteUrl(req, reply);
const name = method + ' ' + url;
agent._instrumentation.setDefaultTransactionName(name);
next();
});
agent.logger.debug('adding preHandler hook to fastify');
_fastify.addHook('preHandler', (req, reply, next) => {
// Save the parsed req body to be picked up by getContextFromRequest().
req.raw.body = req.body;
next();
});
agent.logger.debug('adding onError hook to fastify');
_fastify.addHook('onError', (req, reply, err, next) => {
agent.captureError(err, { request: req.raw });
next();
});
return _fastify;
}
function wrappedFastify2() {
const _fastify = origFastify.apply(null, arguments);
agent.logger.debug('adding onRequest hook to fastify');
_fastify.addHook('onRequest', (req, reply, next) => {
const context = reply.context;
const name = req.raw.method + ' ' + context.config.url;
agent._instrumentation.setDefaultTransactionName(name);
next();
});
agent.logger.debug('adding preHandler hook to fastify');
_fastify.addHook('preHandler', (req, reply, next) => {
// Save the parsed req body to be picked up by getContextFromRequest().
req.raw.body = req.body;
next();
});
agent.logger.debug('adding onError hook to fastify');
_fastify.addHook('onError', (req, reply, err, next) => {
agent.captureError(err, { request: req.raw });
next();
});
return _fastify;
}
function wrappedFastify1() {
const _fastify = origFastify.apply(null, arguments);
agent.logger.debug('adding onRequest hook to fastify');
_fastify.addHook('onRequest', (req, reply, next) => {
const context = reply._context;
const name = req.method + ' ' + context.config.url;
agent._instrumentation.setDefaultTransactionName(name);
next();
});
agent.logger.debug('adding preHandler hook to fastify');
_fastify.addHook('preHandler', (req, reply, next) => {
// Save the parsed req body to be picked up by getContextFromRequest().
req.raw.body = req.body;
next();
});
agent.logger.warn(
'Elastic APM cannot automaticaly capture errors on this verison of Fastify. Upgrade to version 2.0.0 or later.',
);
return _fastify;
}
};