@@ -82,17 +82,23 @@ export const withSentry = (origHandler: NextApiHandler): WrappedNextApiHandler =
8282 const baggageHeader = req . headers && req . headers . baggage ;
8383 const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext ( baggageHeader ) ;
8484
85- const url = `${ req . url } ` ;
86- // pull off query string, if any
87- let reqPath = stripUrlQueryAndFragment ( url ) ;
88- // Replace with placeholder
89- if ( req . query ) {
90- // TODO get this from next if possible, to avoid accidentally replacing non-dynamic parts of the path if
91- // they happen to match the values of any of the dynamic parts
92- for ( const [ key , value ] of Object . entries ( req . query ) ) {
93- reqPath = reqPath . replace ( `${ value } ` , `[${ key } ]` ) ;
85+ // prefer the parameterized route, if we have it (which we will if we've auto-wrapped the route handler)
86+ let reqPath = origHandler . __sentry_route__ || this . __sentry_route__ ;
87+
88+ // If not, fake it by just replacing parameter values with their names, hoping that none of them match either
89+ // each other or any hard-coded parts of the path
90+ if ( ! reqPath ) {
91+ const url = `${ req . url } ` ;
92+ // pull off query string, if any
93+ reqPath = stripUrlQueryAndFragment ( url ) ;
94+ // Replace with placeholder
95+ if ( req . query ) {
96+ for ( const [ key , value ] of Object . entries ( req . query ) ) {
97+ reqPath = reqPath . replace ( `${ value } ` , `[${ key } ]` ) ;
98+ }
9499 }
95100 }
101+
96102 const reqMethod = `${ ( req . method || 'GET' ) . toUpperCase ( ) } ` ;
97103
98104 const transaction = startTransaction (
0 commit comments