|
| 1 | +import { EventProcessor, Hub, Integration } from '@sentry/types'; |
| 2 | +import { addRequestDataToEvent as utilsAddRequestDataToEvent, AddRequestDataToEventOptions } from '@sentry/utils'; |
| 3 | + |
| 4 | +type RequestDataOptions = { |
| 5 | + /** |
| 6 | + * Controls what data is pulled from the request and added to the event |
| 7 | + */ |
| 8 | + include?: AddRequestDataToEventOptions['include']; |
| 9 | + |
| 10 | + /** |
| 11 | + * Function for adding request data to event. Defaults to `addRequestDataToEvent` from `@sentry/utils`, but able to be |
| 12 | + * injected so that SDKs based on `@sentry/node` can use its version of `addRequestDataToEvent`, which itself contains |
| 13 | + * injected dependencies. |
| 14 | + * |
| 15 | + * @hidden |
| 16 | + */ |
| 17 | + _addReqDataCallback?: typeof utilsAddRequestDataToEvent; |
| 18 | +}; |
| 19 | + |
| 20 | +/** Add data about a request to an event. Primarily for use in Node-based SDKs, but included in `@sentry/integrations` |
| 21 | + * so it can be used in cross-platform SDKs like `@sentry/nextjs`. */ |
| 22 | +export class RequestData implements Integration { |
| 23 | + /** |
| 24 | + * @inheritDoc |
| 25 | + */ |
| 26 | + public static id: string = 'RequestData'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @inheritDoc |
| 30 | + */ |
| 31 | + public name: string = RequestData.id; |
| 32 | + |
| 33 | + private _options: RequestDataOptions; |
| 34 | + |
| 35 | + /** |
| 36 | + * @inheritDoc |
| 37 | + */ |
| 38 | + public constructor(options: RequestDataOptions = {}) { |
| 39 | + this._options = options; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @inheritDoc |
| 44 | + */ |
| 45 | + public setupOnce(addGlobalEventProcessor: (eventProcessor: EventProcessor) => void, getCurrentHub: () => Hub): void { |
| 46 | + const { include, _addReqDataCallback = utilsAddRequestDataToEvent } = this._options; |
| 47 | + |
| 48 | + addGlobalEventProcessor(event => { |
| 49 | + const self = getCurrentHub().getIntegration(RequestData); |
| 50 | + const req = event.sdkProcessingMetadata && event.sdkProcessingMetadata.request; |
| 51 | + |
| 52 | + // If the globally installed instance of this integration isn't associated with the current hub, `self` will be |
| 53 | + // undefined |
| 54 | + if (!self || !req) { |
| 55 | + return event; |
| 56 | + } |
| 57 | + |
| 58 | + return _addReqDataCallback(event, req, { include }); |
| 59 | + }); |
| 60 | + } |
| 61 | +} |
0 commit comments