Skip to content

Commit a974f86

Browse files
committed
overwrite transaction value after running addRequestDataToEvent
1 parent 124fd6d commit a974f86

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

packages/node/src/integrations/requestdata.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// TODO (v8 or v9): Whenever this becomes a default integration for `@sentry/browser`, move this to `@sentry/core`. For
22
// now, we leave it in `@sentry/integrations` so that it doesn't contribute bytes to our CDN bundles.
33

4-
import { EventProcessor, Hub, Integration } from '@sentry/types';
4+
import { EventProcessor, Hub, Integration, Transaction } from '@sentry/types';
5+
import { extractPathForTransaction } from '@sentry/utils';
56

67
import {
78
addRequestDataToEvent,
@@ -86,7 +87,11 @@ export class RequestData implements Integration {
8687
* @inheritDoc
8788
*/
8889
public setupOnce(addGlobalEventProcessor: (eventProcessor: EventProcessor) => void, getCurrentHub: () => Hub): void {
89-
const { include, addRequestData } = this._options;
90+
// Note: In the long run, most of the logic here should probably move into the request data utility functions. For
91+
// the moment it lives here, though, until https://github.com/getsentry/sentry-javascript/issues/5718 is addressed.
92+
// (TL;DR: Those functions touch many parts of the repo in many different ways, and need to be clened up. Once
93+
// that's happened, it will be easier to add this logic in without worrying about unexpected side effects.)
94+
const { include, addRequestData, transactionNamingScheme } = this._options;
9095

9196
addGlobalEventProcessor(event => {
9297
const self = getCurrentHub().getIntegration(RequestData);
@@ -98,7 +103,36 @@ export class RequestData implements Integration {
98103
return event;
99104
}
100105

101-
return addRequestData(event, req, { include: formatIncludeOption(include) });
106+
const processedEvent = addRequestData(event, req, { include: formatIncludeOption(include) });
107+
108+
// Transaction events already have the right `transaction` value
109+
if (event.type === 'transaction' || transactionNamingScheme === 'handler') {
110+
return processedEvent;
111+
}
112+
113+
// In all other cases, use the request's associated transaction (if any) to overwrite the event's `transaction`
114+
// value with a high-quality one
115+
const reqWithTransaction = req as { __sentry_transaction?: Transaction };
116+
const transaction = reqWithTransaction.__sentry_transaction;
117+
if (transaction) {
118+
// TODO (v8): Remove the nextjs check and just base it on `transactionNamingScheme` for all SDKs. (We have to
119+
// keep it the way it is for the moment, because changing the names of transactions in Sentry has the potential
120+
// to break things like alert rules.)
121+
const shouldIncludeMethodInTransactionName =
122+
event.sdk?.name === '@sentry/nextjs'
123+
? transaction.name.startsWith('/api')
124+
: transactionNamingScheme !== 'path';
125+
126+
const [transactionValue] = extractPathForTransaction(req, {
127+
path: true,
128+
method: shouldIncludeMethodInTransactionName,
129+
customRoute: transaction.name,
130+
});
131+
132+
processedEvent.transaction = transactionValue;
133+
}
134+
135+
return processedEvent;
102136
});
103137
}
104138
}

0 commit comments

Comments
 (0)