Skip to content

Commit

Permalink
fix(fastify): avoid the FSTDEP017 and FSTDEP018 deprecation warnings
Browse files Browse the repository at this point in the history
Obsoletes: #3789
  • Loading branch information
trentm committed Jan 2, 2024
1 parent 1dfcd8c commit 180f1d8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ Notes:
See the <<upgrade-to-v4>> guide.
==== Unreleased
[float]
===== Breaking changes
[float]
===== Features
[float]
===== Bug fixes
* Improve Fastify instrumentation to no longer cause the https://fastify.dev/docs/latest/Reference/Warnings/#FSTDEP017[`FSTDEP017`]
and https://fastify.dev/docs/latest/Reference/Warnings/#FSTDEP018[`FSTDEP018`]
deprecation warnings.
[float]
===== Chores
[[release-notes-4.3.0]]
==== 4.3.0 - 2023/12/05
Expand Down
29 changes: 27 additions & 2 deletions lib/instrumentation/modules/fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,31 @@

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 fastify@4.10.0.
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 fastify@4.10.0.
// 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,
Expand Down Expand Up @@ -70,8 +95,8 @@ module.exports = function (

agent.logger.debug('adding onRequest hook to fastify');
_fastify.addHook('onRequest', (req, reply, next) => {
const method = req.routerMethod || req.raw.method; // Fallback for fastify >3 <3.3.0
const url = req.routerPath || reply.context.config.url; // Fallback for fastify >3 <3.3.0
const method = fastifyRouteMethod(req);
const url = fastifyRouteUrl(req, reply);
const name = method + ' ' + url;
agent._instrumentation.setDefaultTransactionName(name);
next();
Expand Down

0 comments on commit 180f1d8

Please sign in to comment.