1
1
// TODO (v8 or v9): Whenever this becomes a default integration for `@sentry/browser`, move this to `@sentry/core`. For
2
2
// now, we leave it in `@sentry/integrations` so that it doesn't contribute bytes to our CDN bundles.
3
3
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' ;
6
10
7
11
type RequestDataOptions = {
8
12
/**
@@ -46,9 +50,14 @@ export class RequestData implements Integration {
46
50
* @inheritDoc
47
51
*/
48
52
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.)
50
57
51
58
addGlobalEventProcessor ( event => {
59
+ const { include = { } , _addReqDataCallback = utilsAddRequestDataToEvent } = this . _options ;
60
+
52
61
const self = getCurrentHub ( ) . getIntegration ( RequestData ) ;
53
62
const req = event . sdkProcessingMetadata && event . sdkProcessingMetadata . request ;
54
63
@@ -58,7 +67,35 @@ export class RequestData implements Integration {
58
67
return event ;
59
68
}
60
69
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 ;
62
99
} ) ;
63
100
}
64
101
}
0 commit comments