|
11 | 11 |
|
12 | 12 | const semver = require('semver'); |
13 | 13 |
|
| 14 | +// Return the fastify route or request method, without hitting a Fastify |
| 15 | +// deprecation warning. |
| 16 | +// https://fastify.dev/docs/latest/Reference/Warnings/#FSTDEP018 |
| 17 | +function fastifyRouteMethod(req) { |
| 18 | + if (req.routeOptions) { |
| 19 | + // `.routeOptions` was added in [email protected]. |
| 20 | + return req.routeOptions.method; |
| 21 | + } else { |
| 22 | + return req.routerMethod || req.raw.method; // Fallback for fastify >3 <3.3.0 |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// Return the fastify route or request URL, without hitting a Fastify |
| 27 | +// deprecation warning. |
| 28 | +// https://fastify.dev/docs/latest/Reference/Warnings/#FSTDEP017 |
| 29 | +function fastifyRouteUrl(req, reply) { |
| 30 | + if (req.routeOptions) { |
| 31 | + // `.routeOptions` was added in [email protected]. |
| 32 | + // Note that `.routeOptions.url` might be undefined for a 404 route. |
| 33 | + return req.routeOptions.url; |
| 34 | + } else { |
| 35 | + return req.routerPath || reply.context.config.url; // Fallback for fastify >3 <3.3.0 |
| 36 | + } |
| 37 | +} |
| 38 | + |
14 | 39 | module.exports = function ( |
15 | 40 | modExports, |
16 | 41 | agent, |
@@ -70,8 +95,8 @@ module.exports = function ( |
70 | 95 |
|
71 | 96 | agent.logger.debug('adding onRequest hook to fastify'); |
72 | 97 | _fastify.addHook('onRequest', (req, reply, next) => { |
73 | | - const method = req.routerMethod || req.raw.method; // Fallback for fastify >3 <3.3.0 |
74 | | - const url = req.routerPath || reply.context.config.url; // Fallback for fastify >3 <3.3.0 |
| 98 | + const method = fastifyRouteMethod(req); |
| 99 | + const url = fastifyRouteUrl(req, reply); |
75 | 100 | const name = method + ' ' + url; |
76 | 101 | agent._instrumentation.setDefaultTransactionName(name); |
77 | 102 | next(); |
|
0 commit comments