Skip to content

Commit 472553b

Browse files
committed
overwrite transaction value after running addRequestDataToEvent
1 parent e8ba6b4 commit 472553b

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

packages/integrations/src/requestdata.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
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';
5-
import { addRequestDataToEvent as utilsAddRequestDataToEvent, AddRequestDataToEventOptions } from '@sentry/utils';
4+
import { EventProcessor, Hub, Integration, Transaction } from '@sentry/types';
5+
import {
6+
addRequestDataToEvent as utilsAddRequestDataToEvent,
7+
AddRequestDataToEventOptions,
8+
extractPathForTransaction,
9+
} from '@sentry/utils';
610

711
type RequestDataOptions = {
812
/**
@@ -46,9 +50,14 @@ export class RequestData implements Integration {
4650
* @inheritDoc
4751
*/
4852
public setupOnce(addGlobalEventProcessor: (eventProcessor: EventProcessor) => void, getCurrentHub: () => Hub): void {
49-
const { include, _addReqDataCallback = utilsAddRequestDataToEvent } = this._options;
53+
// Note: In the long run, most of the logic here should probably move into the request data utility functions. For
54+
// the moment it lives here, though, until https://github.com/getsentry/sentry-javascript/issues/5718 is addressed.
55+
// (TL;DR: Those functions touch many parts of the repo in many different ways, and need to be clened up. Once
56+
// that's happened, it will be easier to add this logic in without worrying about unexpected side effects.)
5057

5158
addGlobalEventProcessor(event => {
59+
const { include = {}, _addReqDataCallback = utilsAddRequestDataToEvent } = this._options;
60+
5261
const self = getCurrentHub().getIntegration(RequestData);
5362
const req = event.sdkProcessingMetadata && event.sdkProcessingMetadata.request;
5463

@@ -58,7 +67,35 @@ export class RequestData implements Integration {
5867
return event;
5968
}
6069

61-
return _addReqDataCallback(event, req, { include });
70+
const processedEvent = _addReqDataCallback(event, req, { include });
71+
72+
// In the cases where we either don't care about `transaction` value or already have the best data available, we're
73+
// done
74+
if (include.transaction === false || event.type === 'transaction' || include.transaction === 'handler') {
75+
return processedEvent;
76+
}
77+
78+
// In all other cases, use the request's associated transaction (if any) to overwrite the event's `transaction`
79+
// value with a higher-quality one
80+
const reqWithTransaction = req as { __sentry_transaction?: Transaction };
81+
const transaction = reqWithTransaction.__sentry_transaction;
82+
if (transaction) {
83+
// TODO (v8): Remove the nextjs check and just always include the method for nextjs events. (Doing so is breaking
84+
// because changing the names of transactions in Sentry has the potential to break things like alert rules.)
85+
const shouldIncludeMethodInTransactionName =
86+
event.sdk?.name === '@sentry/nextjs' ? transaction.name.startsWith('/api') : include.transaction !== 'path';
87+
88+
const [transactionValue] = extractPathForTransaction(req, {
89+
path: true,
90+
method: shouldIncludeMethodInTransactionName,
91+
customRoute: transaction.name,
92+
});
93+
processedEvent.transaction = transactionValue;
94+
// TODO: Find out if `source` is a thing it makes sense to add to error events
95+
// processedEvent.transaction_info = { ...processedEvent.transaction_info, source };
96+
}
97+
98+
return processedEvent;
6299
});
63100
}
64101
}

0 commit comments

Comments
 (0)