Skip to content

Adds request data to transaction first, to allow downstream changes #5410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
});
Expand Down
16 changes: 16 additions & 0 deletions packages/node/test/handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;

Expand Down