Skip to content

Commit

Permalink
fix: honour 'usePathAsTransactionName' config var in Next.js instrume…
Browse files Browse the repository at this point in the history
…ntation

Fixes: #3072
  • Loading branch information
trentm committed Dec 15, 2022
1 parent 5655ccc commit 87499b8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<use-path-as-transaction-name>>
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
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion lib/instrumentation/modules/next/dist/server/next-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 87499b8

Please sign in to comment.