From 59699b8412a1f124bb02d80b6a07620d0b187b1a Mon Sep 17 00:00:00 2001 From: Sergey Khomushin Date: Tue, 13 Oct 2020 23:01:06 +0300 Subject: [PATCH 1/5] express.ts: improve types --- packages/tracing/src/integrations/express.ts | 30 ++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/tracing/src/integrations/express.ts b/packages/tracing/src/integrations/express.ts index b216b360ce5e..85a08ba9d6eb 100644 --- a/packages/tracing/src/integrations/express.ts +++ b/packages/tracing/src/integrations/express.ts @@ -3,8 +3,34 @@ import { Integration, Transaction } from '@sentry/types'; import { logger } from '@sentry/utils'; // Have to manually set types because we are using package-alias -interface Application { - use(...args: any): any; +type Method = + | 'all' + | 'get' + | 'post' + | 'put' + | 'delete' + | 'patch' + | 'options' + | 'head' + | 'checkout' + | 'copy' + | 'lock' + | 'merge' + | 'mkactivity' + | 'mkcol' + | 'move' + | 'm-search' + | 'notify' + | 'purge' + | 'report' + | 'search' + | 'subscribe' + | 'trace' + | 'unlock' + | 'unsubscribe'; + +type Application = { + [method in (Method | 'use')]: (...args: any) => any; } type ErrorRequestHandler = (...args: any) => any; From d6879c26d4b3f14a993efae6bfb50fe72007403f Mon Sep 17 00:00:00 2001 From: Sergey Khomushin Date: Tue, 13 Oct 2020 23:01:50 +0300 Subject: [PATCH 2/5] express.ts: added methods list to tracing options --- packages/tracing/src/integrations/express.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/tracing/src/integrations/express.ts b/packages/tracing/src/integrations/express.ts index 85a08ba9d6eb..339205e7d491 100644 --- a/packages/tracing/src/integrations/express.ts +++ b/packages/tracing/src/integrations/express.ts @@ -70,12 +70,14 @@ export class Express implements Integration { * Express App instance */ private readonly _app?: Application; + private readonly _methods?: Method[]; /** * @inheritDoc */ - public constructor(options: { app?: Application } = {}) { + public constructor(options: { app?: Application, methods?: Method[] } = {}) { this._app = options.app; + this._methods = options.methods; } /** From ed13f69844fae3eb3badc6fea4fab2c3337534c9 Mon Sep 17 00:00:00 2001 From: Sergey Khomushin Date: Tue, 13 Oct 2020 23:02:33 +0300 Subject: [PATCH 3/5] express.ts: implement a patch for the route tracing --- packages/tracing/src/integrations/express.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/tracing/src/integrations/express.ts b/packages/tracing/src/integrations/express.ts index 339205e7d491..df4320163290 100644 --- a/packages/tracing/src/integrations/express.ts +++ b/packages/tracing/src/integrations/express.ts @@ -89,6 +89,7 @@ export class Express implements Integration { return; } instrumentMiddlewares(this._app); + routeMiddlewares(this._app, this._methods); } } @@ -219,3 +220,19 @@ function instrumentMiddlewares(app: Application): Application { }; return app; } + +/** + * Patches original app.METHOD to utilize our tracing functionality + */ +function routeMiddlewares(app: Application, methods: Method[] = []): Application { + methods.forEach(function(method: Method) { + const originalAppCallback = app[method]; + + app[method] = function(): any { + // eslint-disable-next-line prefer-rest-params + return originalAppCallback.apply(this, wrapUseArgs(arguments)); + }; + }); + + return app; +} From c6bef7044a0a8c2f7182ac8a81065d061725b0fb Mon Sep 17 00:00:00 2001 From: Sergey Khomushin Date: Tue, 13 Oct 2020 23:26:31 +0300 Subject: [PATCH 4/5] express.ts: prettier --- packages/tracing/src/integrations/express.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/tracing/src/integrations/express.ts b/packages/tracing/src/integrations/express.ts index df4320163290..2f35cebed542 100644 --- a/packages/tracing/src/integrations/express.ts +++ b/packages/tracing/src/integrations/express.ts @@ -30,8 +30,8 @@ type Method = | 'unsubscribe'; type Application = { - [method in (Method | 'use')]: (...args: any) => any; -} + [method in Method | 'use']: (...args: any) => any; +}; type ErrorRequestHandler = (...args: any) => any; type RequestHandler = (...args: any) => any; @@ -75,7 +75,7 @@ export class Express implements Integration { /** * @inheritDoc */ - public constructor(options: { app?: Application, methods?: Method[] } = {}) { + public constructor(options: { app?: Application; methods?: Method[] } = {}) { this._app = options.app; this._methods = options.methods; } From 668d9745af31165bc67c4f43d626bc6fefc12483 Mon Sep 17 00:00:00 2001 From: Sergey Khomushin Date: Sat, 17 Oct 2020 12:48:10 +0300 Subject: [PATCH 5/5] express.ts: use same logic for path --- packages/tracing/src/integrations/express.ts | 33 ++++++++++---------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/packages/tracing/src/integrations/express.ts b/packages/tracing/src/integrations/express.ts index 2f35cebed542..28fc6eb6f06b 100644 --- a/packages/tracing/src/integrations/express.ts +++ b/packages/tracing/src/integrations/express.ts @@ -209,30 +209,31 @@ function wrapUseArgs(args: IArguments): unknown[] { } /** - * Patches original app.use to utilize our tracing functionality + * Patches original App to utilize our tracing functionality */ -function instrumentMiddlewares(app: Application): Application { - // eslint-disable-next-line @typescript-eslint/unbound-method - const originalAppUse = app.use; - app.use = function(): any { +function patchMiddleware(app: Application, method: Method | 'use'): Application { + const originalAppCallback = app[method]; + + app[method] = function(): any { // eslint-disable-next-line prefer-rest-params - return originalAppUse.apply(this, wrapUseArgs(arguments)); + return originalAppCallback.apply(this, wrapUseArgs(arguments)); }; + return app; } /** - * Patches original app.METHOD to utilize our tracing functionality + * Patches original app.use */ -function routeMiddlewares(app: Application, methods: Method[] = []): Application { - methods.forEach(function(method: Method) { - const originalAppCallback = app[method]; +function instrumentMiddlewares(app: Application): void { + patchMiddleware(app, 'use'); +} - app[method] = function(): any { - // eslint-disable-next-line prefer-rest-params - return originalAppCallback.apply(this, wrapUseArgs(arguments)); - }; +/** + * Patches original app.METHOD + */ +function routeMiddlewares(app: Application, methods: Method[] = []): void { + methods.forEach(function(method: Method) { + patchMiddleware(app, method); }); - - return app; }