@@ -27,10 +27,11 @@ type Method =
2727 | 'subscribe'
2828 | 'trace'
2929 | 'unlock'
30- | 'unsubscribe' ;
30+ | 'unsubscribe'
31+ | 'use' ;
3132
3233type Application = {
33- [ method in Method | 'use' ] : ( ...args : any ) => any ;
34+ [ method in Method ] : ( ...args : any ) => any ;
3435} ;
3536
3637type ErrorRequestHandler = ( ...args : any ) => any ;
@@ -41,6 +42,16 @@ interface Response {
4142 once ( name : string , callback : ( ) => void ) : void ;
4243}
4344
45+ interface Request {
46+ route ?: {
47+ path : string ;
48+ } ;
49+ method : string ;
50+ originalUrl : string ;
51+ baseUrl : string ;
52+ query : string ;
53+ }
54+
4455/**
4556 * Internal helper for `__sentry_transaction`
4657 * @hidden
@@ -77,7 +88,7 @@ export class Express implements Integration {
7788 */
7889 public constructor ( options : { app ?: Application ; methods ?: Method [ ] } = { } ) {
7990 this . _app = options . app ;
80- this . _methods = options . methods ;
91+ this . _methods = ( Array . isArray ( options . methods ) ? options . methods : [ ] ) . concat ( 'use' ) ;
8192 }
8293
8394 /**
@@ -88,8 +99,7 @@ export class Express implements Integration {
8899 logger . error ( 'ExpressIntegration is missing an Express instance' ) ;
89100 return ;
90101 }
91- instrumentMiddlewares ( this . _app ) ;
92- routeMiddlewares ( this . _app , this . _methods ) ;
102+ instrumentMiddlewares ( this . _app , this . _methods ) ;
93103 }
94104}
95105
@@ -106,7 +116,7 @@ export class Express implements Integration {
106116 * app.use(function (err, req, res, next) { ... })
107117 */
108118// eslint-disable-next-line @typescript-eslint/ban-types
109- function wrap ( fn : Function ) : RequestHandler | ErrorRequestHandler {
119+ function wrap ( fn : Function , method : Method ) : RequestHandler | ErrorRequestHandler {
110120 const arity = fn . length ;
111121
112122 switch ( arity ) {
@@ -117,7 +127,7 @@ function wrap(fn: Function): RequestHandler | ErrorRequestHandler {
117127 if ( transaction ) {
118128 const span = transaction . startChild ( {
119129 description : fn . name ,
120- op : ' middleware' ,
130+ op : ` middleware. ${ method } ` ,
121131 } ) ;
122132 res . once ( 'finish' , ( ) => {
123133 span . finish ( ) ;
@@ -140,7 +150,7 @@ function wrap(fn: Function): RequestHandler | ErrorRequestHandler {
140150 transaction &&
141151 transaction . startChild ( {
142152 description : fn . name ,
143- op : ' middleware' ,
153+ op : ` middleware. ${ method } ` ,
144154 } ) ;
145155 fn . call ( this , req , res , function ( this : NodeJS . Global ) : any {
146156 if ( span ) {
@@ -165,7 +175,7 @@ function wrap(fn: Function): RequestHandler | ErrorRequestHandler {
165175 transaction &&
166176 transaction . startChild ( {
167177 description : fn . name ,
168- op : ' middleware' ,
178+ op : ` middleware. ${ method } ` ,
169179 } ) ;
170180 fn . call ( this , err , req , res , function ( this : NodeJS . Global ) : any {
171181 if ( span ) {
@@ -182,23 +192,6 @@ function wrap(fn: Function): RequestHandler | ErrorRequestHandler {
182192 }
183193}
184194
185- /**
186- * Set parameterized as transaction name e.g.: `GET /users/:id`
187- * Also adds more context data on the transaction from the request
188- */
189- function addExpressReqToTransaction ( transaction : Transaction | undefined , req : any ) : void {
190- /* eslint-disable @typescript-eslint/no-unsafe-member-access */
191- if ( transaction ) {
192- if ( req . route && req . route . path ) {
193- transaction . name = `${ req . method } ${ req . route . path } ` ;
194- }
195- transaction . setData ( 'url' , req . originalUrl ) ;
196- transaction . setData ( 'baseUrl' , req . baseUrl ) ;
197- transaction . setData ( 'query' , req . query ) ;
198- }
199- /* eslint-enable @typescript-eslint/no-unsafe-member-access */
200- }
201-
202195/**
203196 * Takes all the function arguments passed to the original `app.use` call
204197 * and wraps every function, as well as array of functions with a call to our `wrap` method.
@@ -209,16 +202,16 @@ function addExpressReqToTransaction(transaction: Transaction | undefined, req: a
209202 * app.use([<path>], <fn>, ...<fn>)
210203 * app.use([<path>], ...<fn>[])
211204 */
212- function wrapUseArgs ( args : IArguments ) : unknown [ ] {
213- return Array . from ( args ) . map ( ( arg : unknown ) => {
205+ function wrapMiddlewareArgs ( args : unknown [ ] , method : Method ) : unknown [ ] {
206+ return args . map ( ( arg : unknown ) => {
214207 if ( typeof arg === 'function' ) {
215- return wrap ( arg ) ;
208+ return wrap ( arg , method ) ;
216209 }
217210
218211 if ( Array . isArray ( arg ) ) {
219212 return arg . map ( ( a : unknown ) => {
220213 if ( typeof a === 'function' ) {
221- return wrap ( a ) ;
214+ return wrap ( a , method ) ;
222215 }
223216 return a ;
224217 } ) ;
@@ -231,29 +224,34 @@ function wrapUseArgs(args: IArguments): unknown[] {
231224/**
232225 * Patches original App to utilize our tracing functionality
233226 */
234- function patchMiddleware ( app : Application , method : Method | 'use' ) : Application {
227+ function patchMiddleware ( app : Application , method : Method ) : Application {
235228 const originalAppCallback = app [ method ] ;
236229
237- app [ method ] = function ( ) : any {
238- // eslint-disable-next-line prefer-rest-params
239- return originalAppCallback . apply ( this , wrapUseArgs ( arguments ) ) ;
230+ app [ method ] = function ( ...args : unknown [ ] ) : any {
231+ return originalAppCallback . apply ( this , wrapMiddlewareArgs ( args , method ) ) ;
240232 } ;
241233
242234 return app ;
243235}
244236
245237/**
246- * Patches original app.use
238+ * Patches original application methods
247239 */
248- function instrumentMiddlewares ( app : Application ) : void {
249- patchMiddleware ( app , 'use' ) ;
240+ function instrumentMiddlewares ( app : Application , methods : Method [ ] = [ ] ) : void {
241+ methods . forEach ( ( method : Method ) => patchMiddleware ( app , method ) ) ;
250242}
251243
252244/**
253- * Patches original app.METHOD
245+ * Set parameterized as transaction name e.g.: `GET /users/:id`
246+ * Also adds more context data on the transaction from the request
254247 */
255- function routeMiddlewares ( app : Application , methods : Method [ ] = [ ] ) : void {
256- methods . forEach ( function ( method : Method ) {
257- patchMiddleware ( app , method ) ;
258- } ) ;
248+ function addExpressReqToTransaction ( transaction : Transaction | undefined , req : Request ) : void {
249+ if ( transaction ) {
250+ if ( req . route && req . route . path ) {
251+ transaction . name = `${ req . method } ${ req . route . path } ` ;
252+ }
253+ transaction . setData ( 'url' , req . originalUrl ) ;
254+ transaction . setData ( 'baseUrl' , req . baseUrl ) ;
255+ transaction . setData ( 'query' , req . query ) ;
256+ }
259257}
0 commit comments