|
| 1 | +// TODO (v8 or v9): Whenever this becomes a default integration for `@sentry/browser`, move this to `@sentry/core`. For |
| 2 | +// now, we leave it in `@sentry/integrations` so that it doesn't contribute bytes to our CDN bundles. |
| 3 | + |
| 4 | +import { EventProcessor, Hub, Integration } from '@sentry/types'; |
| 5 | + |
| 6 | +import { |
| 7 | + addRequestDataToEvent, |
| 8 | + AddRequestDataToEventOptions, |
| 9 | + DEFAULT_USER_INCLUDES, |
| 10 | + TransactionNamingScheme, |
| 11 | +} from '../requestdata'; |
| 12 | + |
| 13 | +type RequestDataOptions = { |
| 14 | + /** |
| 15 | + * Controls what data is pulled from the request and added to the event |
| 16 | + */ |
| 17 | + include: { |
| 18 | + cookies?: boolean; |
| 19 | + data?: boolean; |
| 20 | + headers?: boolean; |
| 21 | + ip?: boolean; |
| 22 | + query_string?: boolean; |
| 23 | + url?: boolean; |
| 24 | + user?: boolean | Array<typeof DEFAULT_USER_INCLUDES[number]>; |
| 25 | + }; |
| 26 | + |
| 27 | + /** Whether to identify transactions by parameterized path, parameterized path with method, or handler name */ |
| 28 | + transactionNamingScheme: TransactionNamingScheme; |
| 29 | + |
| 30 | + /** |
| 31 | + * Function for adding request data to event. Defaults to `addRequestDataToEvent` from `@sentry/node` for now, but |
| 32 | + * left injectable so this integration can be moved to `@sentry/core` and used in browser-based SDKs in the future. |
| 33 | + * |
| 34 | + * @hidden |
| 35 | + */ |
| 36 | + addRequestData: typeof addRequestDataToEvent; |
| 37 | +}; |
| 38 | + |
| 39 | +const DEFAULT_OPTIONS = { |
| 40 | + addRequestData: addRequestDataToEvent, |
| 41 | + include: { |
| 42 | + cookies: true, |
| 43 | + data: true, |
| 44 | + headers: true, |
| 45 | + ip: false, |
| 46 | + query_string: true, |
| 47 | + url: true, |
| 48 | + user: DEFAULT_USER_INCLUDES, |
| 49 | + }, |
| 50 | + transactionNamingScheme: 'methodpath', |
| 51 | +}; |
| 52 | + |
| 53 | +/** Add data about a request to an event. Primarily for use in Node-based SDKs, but included in `@sentry/integrations` |
| 54 | + * so it can be used in cross-platform SDKs like `@sentry/nextjs`. */ |
| 55 | +export class RequestData implements Integration { |
| 56 | + /** |
| 57 | + * @inheritDoc |
| 58 | + */ |
| 59 | + public static id: string = 'RequestData'; |
| 60 | + |
| 61 | + /** |
| 62 | + * @inheritDoc |
| 63 | + */ |
| 64 | + public name: string = RequestData.id; |
| 65 | + |
| 66 | + private _options: RequestDataOptions; |
| 67 | + |
| 68 | + /** |
| 69 | + * @inheritDoc |
| 70 | + */ |
| 71 | + public constructor(options: Partial<RequestDataOptions> = {}) { |
| 72 | + this._options = { |
| 73 | + ...DEFAULT_OPTIONS, |
| 74 | + ...options, |
| 75 | + include: { |
| 76 | + // @ts-ignore It's mad because `method` isn't a known `include` key. (It's only here and not set by default in |
| 77 | + // `addRequestDataToEvent` for legacy reasons. TODO (v8): Change that.) |
| 78 | + method: true, |
| 79 | + ...DEFAULT_OPTIONS.include, |
| 80 | + ...options.include, |
| 81 | + }, |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @inheritDoc |
| 87 | + */ |
| 88 | + public setupOnce(addGlobalEventProcessor: (eventProcessor: EventProcessor) => void, getCurrentHub: () => Hub): void { |
| 89 | + const { include, addRequestData } = this._options; |
| 90 | + |
| 91 | + addGlobalEventProcessor(event => { |
| 92 | + const self = getCurrentHub().getIntegration(RequestData); |
| 93 | + const req = event.sdkProcessingMetadata && event.sdkProcessingMetadata.request; |
| 94 | + |
| 95 | + // If the globally installed instance of this integration isn't associated with the current hub, `self` will be |
| 96 | + // undefined |
| 97 | + if (!self || !req) { |
| 98 | + return event; |
| 99 | + } |
| 100 | + |
| 101 | + return addRequestData(event, req, { include: formatIncludeOption(include) }); |
| 102 | + }); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/** Convert `include` option to match what `addRequestDataToEvent` expects */ |
| 107 | +/** TODO: Can possibly be deleted once https://github.com/getsentry/sentry-javascript/issues/5718 is fixed */ |
| 108 | +function formatIncludeOption( |
| 109 | + integrationInclude: RequestDataOptions['include'] = {}, |
| 110 | +): AddRequestDataToEventOptions['include'] { |
| 111 | + const { ip, user, ...requestOptions } = integrationInclude; |
| 112 | + |
| 113 | + const requestIncludeKeys: string[] = []; |
| 114 | + for (const [key, value] of Object.entries(requestOptions)) { |
| 115 | + if (value) { |
| 116 | + requestIncludeKeys.push(key); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return { |
| 121 | + ip, |
| 122 | + user, |
| 123 | + request: requestIncludeKeys.length !== 0 ? requestIncludeKeys : undefined, |
| 124 | + }; |
| 125 | +} |
0 commit comments