From 87499b8b733cda2023b47cc9a8f468a675c76d6d Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Thu, 15 Dec 2022 11:21:01 -0800 Subject: [PATCH] fix: honour 'usePathAsTransactionName' config var in Next.js instrumentation Fixes: #3072 --- CHANGELOG.asciidoc | 20 +++++++++++++++++++ examples/nextjs/package.json | 2 +- .../next/dist/server/dev/next-dev-server.js | 6 +++++- .../modules/next/dist/server/next-server.js | 6 +++++- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 10c3a9b3d0..4b26a35cce 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -32,6 +32,26 @@ Notes: === Node.js Agent version 3.x +==== Unreleased + +[float] +===== Breaking changes + +[float] +===== Features + +[float] +===== Bug fixes + +* Fix Next.js instrumentation to honour the <> + config setting. When set to true the APM transaction name for a request to a + Next.js Dynamic Route will use the request path, e.g. `GET /about-us`, rather + than the dynamic route name, e.g. `GET /[...page]`. ({issues}3072[#3072]) + +[float] +===== Chores + + [[release-notes-3.41.0]] ==== 3.41.0 2022/12/12 diff --git a/examples/nextjs/package.json b/examples/nextjs/package.json index 45168c859e..d132ab79e6 100644 --- a/examples/nextjs/package.json +++ b/examples/nextjs/package.json @@ -9,7 +9,7 @@ "lint": "next lint" }, "dependencies": { - "elastic-apm-node": "elastic/apm-agent-nodejs#main", + "elastic-apm-node": "^3.40.1", "next": "12.3.1", "react": "18.2.0", "react-dom": "18.2.0" diff --git a/lib/instrumentation/modules/next/dist/server/dev/next-dev-server.js b/lib/instrumentation/modules/next/dist/server/dev/next-dev-server.js index ab1e70523c..d02b5da865 100644 --- a/lib/instrumentation/modules/next/dist/server/dev/next-dev-server.js +++ b/lib/instrumentation/modules/next/dist/server/dev/next-dev-server.js @@ -143,7 +143,11 @@ module.exports = function (mod, agent, { version, enabled }) { // specific route wrapper hasn't already done so. if (trans && !trans[kSetTransNameFn]) { trans[kSetTransNameFn] = (req, pathname) => { - trans.setDefaultName(`${req.method} ${pathname}`) + if (agent._conf.usePathAsTransactionName) { + trans.setDefaultName(`${req.method} ${req.url}`) + } else { + trans.setDefaultName(`${req.method} ${pathname}`) + } // Ensure only the first `findPageComponents` result sets the trans // name, otherwise a loaded `/_error` for page error handling could // incorrectly override. diff --git a/lib/instrumentation/modules/next/dist/server/next-server.js b/lib/instrumentation/modules/next/dist/server/next-server.js index 0431e260d0..428309fe9e 100644 --- a/lib/instrumentation/modules/next/dist/server/next-server.js +++ b/lib/instrumentation/modules/next/dist/server/next-server.js @@ -140,7 +140,11 @@ module.exports = function (mod, agent, { version, enabled }) { // specific route wrapper hasn't already done so. if (trans && !trans[kSetTransNameFn]) { trans[kSetTransNameFn] = (req, pathname) => { - trans.setDefaultName(`${req.method} ${pathname}`) + if (agent._conf.usePathAsTransactionName) { + trans.setDefaultName(`${req.method} ${req.url}`) + } else { + trans.setDefaultName(`${req.method} ${pathname}`) + } // Ensure only the first `findPageComponents` result sets the trans // name, otherwise a loaded `/_error` for page error handling could // incorrectly override.