Skip to content

Commit 57eaded

Browse files
authored
[@sentry/tracing] Express route tracing (#2972)
1 parent 86ca835 commit 57eaded

File tree

1 file changed

+56
-10
lines changed

1 file changed

+56
-10
lines changed

packages/tracing/src/integrations/express.ts

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,35 @@ import { Integration, Transaction } from '@sentry/types';
33
import { logger } from '@sentry/utils';
44

55
// 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+
};
935

1036
type ErrorRequestHandler = (...args: any) => any;
1137
type RequestHandler = (...args: any) => any;
@@ -44,12 +70,14 @@ export class Express implements Integration {
4470
* Express App instance
4571
*/
4672
private readonly _app?: Application;
73+
private readonly _methods?: Method[];
4774

4875
/**
4976
* @inheritDoc
5077
*/
51-
public constructor(options: { app?: Application } = {}) {
78+
public constructor(options: { app?: Application; methods?: Method[] } = {}) {
5279
this._app = options.app;
80+
this._methods = options.methods;
5381
}
5482

5583
/**
@@ -61,6 +89,7 @@ export class Express implements Integration {
6189
return;
6290
}
6391
instrumentMiddlewares(this._app);
92+
routeMiddlewares(this._app, this._methods);
6493
}
6594
}
6695

@@ -180,14 +209,31 @@ function wrapUseArgs(args: IArguments): unknown[] {
180209
}
181210

182211
/**
183-
* Patches original app.use to utilize our tracing functionality
212+
* Patches original App to utilize our tracing functionality
184213
*/
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 {
189218
// eslint-disable-next-line prefer-rest-params
190-
return originalAppUse.apply(this, wrapUseArgs(arguments));
219+
return originalAppCallback.apply(this, wrapUseArgs(arguments));
191220
};
221+
192222
return app;
193223
}
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

Comments
 (0)