From fe4cfa034f0d720d706e63e74758907c8e788cc6 Mon Sep 17 00:00:00 2001 From: Chris Wiggins Date: Tue, 12 Jul 2022 16:38:19 +1200 Subject: [PATCH 1/2] Adds request data to transaction first, to allow downstream changes --- packages/node/src/handlers.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/node/src/handlers.ts b/packages/node/src/handlers.ts index 417b60c4795f..cd7f236aaf96 100644 --- a/packages/node/src/handlers.ts +++ b/packages/node/src/handlers.ts @@ -56,6 +56,9 @@ export function tracingHandler(): ( scope.setSpan(transaction); }); + // Add the request data to the transaction first, so others can override downstream if necessary + addRequestDataToTransaction(transaction, req); + // We also set __sentry_transaction on the response so people can grab the transaction there to add // spans to it later. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access @@ -65,7 +68,6 @@ export function tracingHandler(): ( // Push `transaction.finish` to the next event loop so open spans have a chance to finish before the transaction // closes setImmediate(() => { - addRequestDataToTransaction(transaction, req); transaction.setHttpStatus(res.statusCode); transaction.finish(); }); From cddb34e8e266b93b5121ee6d964f07dd7f38a11f Mon Sep 17 00:00:00 2001 From: Chris Wiggins Date: Tue, 12 Jul 2022 16:58:42 +1200 Subject: [PATCH 2/2] Add tests --- packages/node/test/handlers.test.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/node/test/handlers.test.ts b/packages/node/test/handlers.test.ts index e06c1846d0c2..b05aa84937d9 100644 --- a/packages/node/test/handlers.test.ts +++ b/packages/node/test/handlers.test.ts @@ -305,6 +305,22 @@ describe('tracingHandler', () => { }); }); + it('allows downstream functions to override transaction values', done => { + const transaction = new Transaction({ name: 'mockTransaction' }); + jest.spyOn(sentryCore, 'startTransaction').mockReturnValue(transaction as Transaction); + const finishTransaction = jest.spyOn(transaction, 'finish'); + + sentryTracingMiddleware(req, res, next); + transaction.name = 'overriddenName'; + res.emit('finish'); + + setImmediate(() => { + expect(finishTransaction).toHaveBeenCalled(); + expect(transaction.name).toBe('overriddenName'); + done(); + }); + }); + it('strips query string from request path', () => { req.url = `${path}?${queryString}`;