Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next.js instrumentation doesn't honour usePathAsTransactionName config var #3072

Open
trentm opened this issue Dec 15, 2022 · 1 comment · May be fixed by #3073
Open

Next.js instrumentation doesn't honour usePathAsTransactionName config var #3072

trentm opened this issue Dec 15, 2022 · 1 comment · May be fixed by #3073
Labels
8.8-candidate agent-nodejs Make available for APM Agents project planning.

Comments

@trentm
Copy link
Member

trentm commented Dec 15, 2022

As discussed at https://discuss.elastic.co/t/next-js-and-dynamic-routes/321302 the current Next.js instrumentation that sets the transaction.name for a Dynamic Route does not honour the usePathAsTransactionName: true config var. That means that a request GET /a-dynamic-route/42 to this dynamic route https://github.com/elastic/apm-agent-nodejs/blob/main/test/instrumentation/modules/next/a-nextjs-app/pages/a-dynamic-page/%5Bnum%5D.js results in transaction.name = 'GET /a-dynamic-route/[num] even if usePathAsTransactionName: true.

Note that there is also naming of the Next.js _data/route ... transactions for possibly dynamic routes -- e.g. "Next.js _next/data route /a-dynamic-page/[num]". However, I don't think those should honour usePathAsTransactionName. At least not without user feedback.

@github-actions github-actions bot added the agent-nodejs Make available for APM Agents project planning. label Dec 15, 2022
@trentm
Copy link
Member Author

trentm commented Dec 15, 2022

Note to self: a start at this is:

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 ab1e7052..d02b5da8 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 0431e260..428309fe 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
8.8-candidate agent-nodejs Make available for APM Agents project planning.
Projects
None yet
1 participant