@@ -27,10 +27,11 @@ type Method =
27
27
| 'subscribe'
28
28
| 'trace'
29
29
| 'unlock'
30
- | 'unsubscribe' ;
30
+ | 'unsubscribe'
31
+ | 'use' ;
31
32
32
33
type Application = {
33
- [ method in Method | 'use' ] : ( ...args : any ) => any ;
34
+ [ method in Method ] : ( ...args : any ) => any ;
34
35
} ;
35
36
36
37
type ErrorRequestHandler = ( ...args : any ) => any ;
@@ -41,6 +42,16 @@ interface Response {
41
42
once ( name : string , callback : ( ) => void ) : void ;
42
43
}
43
44
45
+ interface Request {
46
+ route ?: {
47
+ path : string ;
48
+ } ;
49
+ method : string ;
50
+ originalUrl : string ;
51
+ baseUrl : string ;
52
+ query : string ;
53
+ }
54
+
44
55
/**
45
56
* Internal helper for `__sentry_transaction`
46
57
* @hidden
@@ -77,7 +88,7 @@ export class Express implements Integration {
77
88
*/
78
89
public constructor ( options : { app ?: Application ; methods ?: Method [ ] } = { } ) {
79
90
this . _app = options . app ;
80
- this . _methods = options . methods ;
91
+ this . _methods = ( Array . isArray ( options . methods ) ? options . methods : [ ] ) . concat ( 'use' ) ;
81
92
}
82
93
83
94
/**
@@ -88,8 +99,7 @@ export class Express implements Integration {
88
99
logger . error ( 'ExpressIntegration is missing an Express instance' ) ;
89
100
return ;
90
101
}
91
- instrumentMiddlewares ( this . _app ) ;
92
- routeMiddlewares ( this . _app , this . _methods ) ;
102
+ instrumentMiddlewares ( this . _app , this . _methods ) ;
93
103
}
94
104
}
95
105
@@ -106,7 +116,7 @@ export class Express implements Integration {
106
116
* app.use(function (err, req, res, next) { ... })
107
117
*/
108
118
// eslint-disable-next-line @typescript-eslint/ban-types
109
- function wrap ( fn : Function ) : RequestHandler | ErrorRequestHandler {
119
+ function wrap ( fn : Function , method : Method ) : RequestHandler | ErrorRequestHandler {
110
120
const arity = fn . length ;
111
121
112
122
switch ( arity ) {
@@ -117,7 +127,7 @@ function wrap(fn: Function): RequestHandler | ErrorRequestHandler {
117
127
if ( transaction ) {
118
128
const span = transaction . startChild ( {
119
129
description : fn . name ,
120
- op : ' middleware' ,
130
+ op : ` middleware. ${ method } ` ,
121
131
} ) ;
122
132
res . once ( 'finish' , ( ) => {
123
133
span . finish ( ) ;
@@ -140,7 +150,7 @@ function wrap(fn: Function): RequestHandler | ErrorRequestHandler {
140
150
transaction &&
141
151
transaction . startChild ( {
142
152
description : fn . name ,
143
- op : ' middleware' ,
153
+ op : ` middleware. ${ method } ` ,
144
154
} ) ;
145
155
fn . call ( this , req , res , function ( this : NodeJS . Global ) : any {
146
156
if ( span ) {
@@ -165,7 +175,7 @@ function wrap(fn: Function): RequestHandler | ErrorRequestHandler {
165
175
transaction &&
166
176
transaction . startChild ( {
167
177
description : fn . name ,
168
- op : ' middleware' ,
178
+ op : ` middleware. ${ method } ` ,
169
179
} ) ;
170
180
fn . call ( this , err , req , res , function ( this : NodeJS . Global ) : any {
171
181
if ( span ) {
@@ -182,23 +192,6 @@ function wrap(fn: Function): RequestHandler | ErrorRequestHandler {
182
192
}
183
193
}
184
194
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
-
202
195
/**
203
196
* Takes all the function arguments passed to the original `app.use` call
204
197
* 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
209
202
* app.use([<path>], <fn>, ...<fn>)
210
203
* app.use([<path>], ...<fn>[])
211
204
*/
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 ) => {
214
207
if ( typeof arg === 'function' ) {
215
- return wrap ( arg ) ;
208
+ return wrap ( arg , method ) ;
216
209
}
217
210
218
211
if ( Array . isArray ( arg ) ) {
219
212
return arg . map ( ( a : unknown ) => {
220
213
if ( typeof a === 'function' ) {
221
- return wrap ( a ) ;
214
+ return wrap ( a , method ) ;
222
215
}
223
216
return a ;
224
217
} ) ;
@@ -231,29 +224,34 @@ function wrapUseArgs(args: IArguments): unknown[] {
231
224
/**
232
225
* Patches original App to utilize our tracing functionality
233
226
*/
234
- function patchMiddleware ( app : Application , method : Method | 'use' ) : Application {
227
+ function patchMiddleware ( app : Application , method : Method ) : Application {
235
228
const originalAppCallback = app [ method ] ;
236
229
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 ) ) ;
240
232
} ;
241
233
242
234
return app ;
243
235
}
244
236
245
237
/**
246
- * Patches original app.use
238
+ * Patches original application methods
247
239
*/
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 ) ) ;
250
242
}
251
243
252
244
/**
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
254
247
*/
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
+ }
259
257
}
0 commit comments