|
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 type { Event, EventProcessor, Hub, Integration, PolymorphicRequest, Transaction } from '@sentry/types'; |
| 4 | +import type { Client, Event, EventHint, Integration, PolymorphicRequest, Transaction } from '@sentry/types'; |
5 | 5 | import { extractPathForTransaction } from '@sentry/utils'; |
6 | 6 |
|
7 | 7 | import type { AddRequestDataToEventOptions, TransactionNamingScheme } from '../requestdata'; |
@@ -99,65 +99,66 @@ export class RequestData implements Integration { |
99 | 99 | /** |
100 | 100 | * @inheritDoc |
101 | 101 | */ |
102 | | - public setupOnce(addGlobalEventProcessor: (eventProcessor: EventProcessor) => void, getCurrentHub: () => Hub): void { |
| 102 | + public setupOnce(_addGlobaleventProcessor: unknown, _getCurrentHub: unknown): void { |
| 103 | + // noop |
| 104 | + } |
| 105 | + |
| 106 | + /** @inheritDoc */ |
| 107 | + public processEvent(event: Event, hint: EventHint | undefined, client: Client): Event { |
103 | 108 | // Note: In the long run, most of the logic here should probably move into the request data utility functions. For |
104 | 109 | // the moment it lives here, though, until https://github.com/getsentry/sentry-javascript/issues/5718 is addressed. |
105 | 110 | // (TL;DR: Those functions touch many parts of the repo in many different ways, and need to be clened up. Once |
106 | 111 | // that's happened, it will be easier to add this logic in without worrying about unexpected side effects.) |
107 | | - const { transactionNamingScheme } = this._options; |
108 | 112 |
|
109 | | - addGlobalEventProcessor(event => { |
110 | | - const hub = getCurrentHub(); |
111 | | - const self = hub.getIntegration(RequestData); |
| 113 | + const { transactionNamingScheme } = this._options; |
112 | 114 |
|
113 | | - const { sdkProcessingMetadata = {} } = event; |
114 | | - const req = sdkProcessingMetadata.request; |
| 115 | + const { sdkProcessingMetadata = {} } = event; |
| 116 | + const req = sdkProcessingMetadata.request; |
115 | 117 |
|
116 | | - // If the globally installed instance of this integration isn't associated with the current hub, `self` will be |
117 | | - // undefined |
118 | | - if (!self || !req) { |
119 | | - return event; |
120 | | - } |
| 118 | + // If the globally installed instance of this integration isn't associated with the current hub, `self` will be |
| 119 | + // undefined |
| 120 | + if (!req) { |
| 121 | + return event; |
| 122 | + } |
121 | 123 |
|
122 | | - // The Express request handler takes a similar `include` option to that which can be passed to this integration. |
123 | | - // If passed there, we store it in `sdkProcessingMetadata`. TODO(v8): Force express and GCP people to use this |
124 | | - // integration, so that all of this passing and conversion isn't necessary |
125 | | - const addRequestDataOptions = |
126 | | - sdkProcessingMetadata.requestDataOptionsFromExpressHandler || |
127 | | - sdkProcessingMetadata.requestDataOptionsFromGCPWrapper || |
128 | | - convertReqDataIntegrationOptsToAddReqDataOpts(this._options); |
| 124 | + // The Express request handler takes a similar `include` option to that which can be passed to this integration. |
| 125 | + // If passed there, we store it in `sdkProcessingMetadata`. TODO(v8): Force express and GCP people to use this |
| 126 | + // integration, so that all of this passing and conversion isn't necessary |
| 127 | + const addRequestDataOptions = |
| 128 | + sdkProcessingMetadata.requestDataOptionsFromExpressHandler || |
| 129 | + sdkProcessingMetadata.requestDataOptionsFromGCPWrapper || |
| 130 | + convertReqDataIntegrationOptsToAddReqDataOpts(this._options); |
129 | 131 |
|
130 | | - const processedEvent = this._addRequestData(event, req, addRequestDataOptions); |
| 132 | + const processedEvent = this._addRequestData(event, req, addRequestDataOptions); |
131 | 133 |
|
132 | | - // Transaction events already have the right `transaction` value |
133 | | - if (event.type === 'transaction' || transactionNamingScheme === 'handler') { |
134 | | - return processedEvent; |
135 | | - } |
| 134 | + // Transaction events already have the right `transaction` value |
| 135 | + if (event.type === 'transaction' || transactionNamingScheme === 'handler') { |
| 136 | + return processedEvent; |
| 137 | + } |
136 | 138 |
|
137 | | - // In all other cases, use the request's associated transaction (if any) to overwrite the event's `transaction` |
138 | | - // value with a high-quality one |
139 | | - const reqWithTransaction = req as { _sentryTransaction?: Transaction }; |
140 | | - const transaction = reqWithTransaction._sentryTransaction; |
141 | | - if (transaction) { |
142 | | - // TODO (v8): Remove the nextjs check and just base it on `transactionNamingScheme` for all SDKs. (We have to |
143 | | - // keep it the way it is for the moment, because changing the names of transactions in Sentry has the potential |
144 | | - // to break things like alert rules.) |
145 | | - const shouldIncludeMethodInTransactionName = |
146 | | - getSDKName(hub) === 'sentry.javascript.nextjs' |
147 | | - ? transaction.name.startsWith('/api') |
148 | | - : transactionNamingScheme !== 'path'; |
149 | | - |
150 | | - const [transactionValue] = extractPathForTransaction(req, { |
151 | | - path: true, |
152 | | - method: shouldIncludeMethodInTransactionName, |
153 | | - customRoute: transaction.name, |
154 | | - }); |
155 | | - |
156 | | - processedEvent.transaction = transactionValue; |
157 | | - } |
| 139 | + // In all other cases, use the request's associated transaction (if any) to overwrite the event's `transaction` |
| 140 | + // value with a high-quality one |
| 141 | + const reqWithTransaction = req as { _sentryTransaction?: Transaction }; |
| 142 | + const transaction = reqWithTransaction._sentryTransaction; |
| 143 | + if (transaction) { |
| 144 | + // TODO (v8): Remove the nextjs check and just base it on `transactionNamingScheme` for all SDKs. (We have to |
| 145 | + // keep it the way it is for the moment, because changing the names of transactions in Sentry has the potential |
| 146 | + // to break things like alert rules.) |
| 147 | + const shouldIncludeMethodInTransactionName = |
| 148 | + getSDKName(client) === 'sentry.javascript.nextjs' |
| 149 | + ? transaction.name.startsWith('/api') |
| 150 | + : transactionNamingScheme !== 'path'; |
| 151 | + |
| 152 | + const [transactionValue] = extractPathForTransaction(req, { |
| 153 | + path: true, |
| 154 | + method: shouldIncludeMethodInTransactionName, |
| 155 | + customRoute: transaction.name, |
| 156 | + }); |
| 157 | + |
| 158 | + processedEvent.transaction = transactionValue; |
| 159 | + } |
158 | 160 |
|
159 | | - return processedEvent; |
160 | | - }); |
| 161 | + return processedEvent; |
161 | 162 | } |
162 | 163 | } |
163 | 164 |
|
@@ -203,12 +204,12 @@ function convertReqDataIntegrationOptsToAddReqDataOpts( |
203 | 204 | }; |
204 | 205 | } |
205 | 206 |
|
206 | | -function getSDKName(hub: Hub): string | undefined { |
| 207 | +function getSDKName(client: Client): string | undefined { |
207 | 208 | try { |
208 | 209 | // For a long chain like this, it's fewer bytes to combine a try-catch with assuming everything is there than to |
209 | 210 | // write out a long chain of `a && a.b && a.b.c && ...` |
210 | 211 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
211 | | - return hub.getClient()!.getOptions()!._metadata!.sdk!.name; |
| 212 | + return client.getOptions()!._metadata!.sdk!.name; |
212 | 213 | } catch (err) { |
213 | 214 | // In theory we should never get here |
214 | 215 | return undefined; |
|
0 commit comments