Skip to content

Commit 9814453

Browse files
committed
add RequestData integration
1 parent 71dc9ee commit 9814453

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-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';
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)