Skip to content

Commit f34d902

Browse files
committed
add RequestData integration
1 parent 889dc17 commit f34d902

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

packages/integrations/src/index.ts

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

0 commit comments

Comments
 (0)