Skip to content

Commit 8d162a2

Browse files
trentmPeterEinberger
authored andcommitted
fix(fastify): avoid the FSTDEP017 and FSTDEP018 deprecation warnings (elastic#3814)
Obsoletes: elastic#3789
1 parent 5b6194e commit 8d162a2

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

CHANGELOG.asciidoc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ Notes:
3434
See the <<upgrade-to-v4>> guide.
3535
3636
37+
==== Unreleased
38+
39+
[float]
40+
===== Breaking changes
41+
42+
[float]
43+
===== Features
44+
45+
[float]
46+
===== Bug fixes
47+
48+
* Improve Fastify instrumentation to no longer cause the https://fastify.dev/docs/latest/Reference/Warnings/#FSTDEP017[`FSTDEP017`]
49+
and https://fastify.dev/docs/latest/Reference/Warnings/#FSTDEP018[`FSTDEP018`]
50+
deprecation warnings. ({pull}3814[#3814])
51+
52+
[float]
53+
===== Chores
54+
55+
3756
[[release-notes-4.3.0]]
3857
==== 4.3.0 - 2023/12/05
3958

lib/instrumentation/modules/fastify.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,31 @@
1111

1212
const semver = require('semver');
1313

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+
1439
module.exports = function (
1540
modExports,
1641
agent,
@@ -70,8 +95,8 @@ module.exports = function (
7095

7196
agent.logger.debug('adding onRequest hook to fastify');
7297
_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);
75100
const name = method + ' ' + url;
76101
agent._instrumentation.setDefaultTransactionName(name);
77102
next();

0 commit comments

Comments
 (0)