@@ -3,9 +3,35 @@ import { Integration, Transaction } from '@sentry/types';
3
3
import { logger } from '@sentry/utils' ;
4
4
5
5
// Have to manually set types because we are using package-alias
6
- interface Application {
7
- use ( ...args : any ) : any ;
8
- }
6
+ type Method =
7
+ | 'all'
8
+ | 'get'
9
+ | 'post'
10
+ | 'put'
11
+ | 'delete'
12
+ | 'patch'
13
+ | 'options'
14
+ | 'head'
15
+ | 'checkout'
16
+ | 'copy'
17
+ | 'lock'
18
+ | 'merge'
19
+ | 'mkactivity'
20
+ | 'mkcol'
21
+ | 'move'
22
+ | 'm-search'
23
+ | 'notify'
24
+ | 'purge'
25
+ | 'report'
26
+ | 'search'
27
+ | 'subscribe'
28
+ | 'trace'
29
+ | 'unlock'
30
+ | 'unsubscribe' ;
31
+
32
+ type Application = {
33
+ [ method in Method | 'use' ] : ( ...args : any ) => any ;
34
+ } ;
9
35
10
36
type ErrorRequestHandler = ( ...args : any ) => any ;
11
37
type RequestHandler = ( ...args : any ) => any ;
@@ -44,12 +70,14 @@ export class Express implements Integration {
44
70
* Express App instance
45
71
*/
46
72
private readonly _app ?: Application ;
73
+ private readonly _methods ?: Method [ ] ;
47
74
48
75
/**
49
76
* @inheritDoc
50
77
*/
51
- public constructor ( options : { app ?: Application } = { } ) {
78
+ public constructor ( options : { app ?: Application ; methods ?: Method [ ] } = { } ) {
52
79
this . _app = options . app ;
80
+ this . _methods = options . methods ;
53
81
}
54
82
55
83
/**
@@ -61,6 +89,7 @@ export class Express implements Integration {
61
89
return ;
62
90
}
63
91
instrumentMiddlewares ( this . _app ) ;
92
+ routeMiddlewares ( this . _app , this . _methods ) ;
64
93
}
65
94
}
66
95
@@ -180,14 +209,31 @@ function wrapUseArgs(args: IArguments): unknown[] {
180
209
}
181
210
182
211
/**
183
- * Patches original app.use to utilize our tracing functionality
212
+ * Patches original App to utilize our tracing functionality
184
213
*/
185
- function instrumentMiddlewares ( app : Application ) : Application {
186
- // eslint-disable-next-line @typescript-eslint/unbound- method
187
- const originalAppUse = app . use ;
188
- app . use = function ( ) : any {
214
+ function patchMiddleware ( app : Application , method : Method | 'use' ) : Application {
215
+ const originalAppCallback = app [ method ] ;
216
+
217
+ app [ method ] = function ( ) : any {
189
218
// eslint-disable-next-line prefer-rest-params
190
- return originalAppUse . apply ( this , wrapUseArgs ( arguments ) ) ;
219
+ return originalAppCallback . apply ( this , wrapUseArgs ( arguments ) ) ;
191
220
} ;
221
+
192
222
return app ;
193
223
}
224
+
225
+ /**
226
+ * Patches original app.use
227
+ */
228
+ function instrumentMiddlewares ( app : Application ) : void {
229
+ patchMiddleware ( app , 'use' ) ;
230
+ }
231
+
232
+ /**
233
+ * Patches original app.METHOD
234
+ */
235
+ function routeMiddlewares ( app : Application , methods : Method [ ] = [ ] ) : void {
236
+ methods . forEach ( function ( method : Method ) {
237
+ patchMiddleware ( app , method ) ;
238
+ } ) ;
239
+ }
0 commit comments