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