@@ -79,17 +79,23 @@ export const withSentry = (origHandler: NextApiHandler): WrappedNextApiHandler =
79
79
const baggageHeader = req . headers && req . headers . baggage ;
80
80
const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext ( baggageHeader ) ;
81
81
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
+ }
91
96
}
92
97
}
98
+
93
99
const reqMethod = `${ ( req . method || 'GET' ) . toUpperCase ( ) } ` ;
94
100
95
101
const transaction = startTransaction (
0 commit comments