Skip to content

Commit 237cf9f

Browse files
committed
use parameterized route, if available
1 parent d2ac2e6 commit 237cf9f

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

packages/nextjs/src/utils/withSentry.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,23 @@ export const withSentry = (origHandler: NextApiHandler): WrappedNextApiHandler =
7979
const baggageHeader = req.headers && req.headers.baggage;
8080
const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(baggageHeader);
8181

82-
const url = `${req.url}`;
83-
// pull off query string, if any
84-
let reqPath = stripUrlQueryAndFragment(url);
85-
// Replace with placeholder
86-
if (req.query) {
87-
// TODO get this from next if possible, to avoid accidentally replacing non-dynamic parts of the path if
88-
// they happen to match the values of any of the dynamic parts
89-
for (const [key, value] of Object.entries(req.query)) {
90-
reqPath = reqPath.replace(`${value}`, `[${key}]`);
82+
// prefer the parameterized route, if we have it (which we will if we've auto-wrapped the API route)
83+
let reqPath = origHandler.__sentry_route__ || this.__sentry_route__;
84+
85+
if (!reqPath) {
86+
const url = `${req.url}`;
87+
// pull off query string, if any
88+
reqPath = stripUrlQueryAndFragment(url);
89+
// Replace with placeholder
90+
if (req.query) {
91+
// TODO get this from next if possible, to avoid accidentally replacing non-dynamic parts of the path if
92+
// they happen to match the values of any of the dynamic parts
93+
for (const [key, value] of Object.entries(req.query)) {
94+
reqPath = reqPath.replace(`${value}`, `[${key}]`);
95+
}
9196
}
9297
}
98+
9399
const reqMethod = `${(req.method || 'GET').toUpperCase()} `;
94100

95101
const transaction = startTransaction(

0 commit comments

Comments
 (0)