Skip to content

Commit 3d6ee8e

Browse files
committed
feat: Allow requestHandler to be configured
1 parent 22c8706 commit 3d6ee8e

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

packages/node/src/handlers.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,32 +111,57 @@ function parseRequest(
111111
req: {
112112
[key: string]: any;
113113
},
114+
options?: {
115+
version?: boolean;
116+
request?: boolean;
117+
serverName?: boolean;
118+
user?: boolean;
119+
},
114120
): SentryEvent {
115-
const preparedEvent = {
116-
...event,
117-
extra: {
121+
// tslint:disable-next-line:no-parameter-reassignment
122+
options = {
123+
request: true,
124+
serverName: true,
125+
user: true,
126+
version: true,
127+
...options,
128+
};
129+
130+
if (options.version) {
131+
event.extra = {
118132
...event.extra,
119133
node: global.process.version,
120-
},
121-
request: {
134+
};
135+
}
136+
137+
if (options.request) {
138+
event.request = {
122139
...event.request,
123140
...extractRequestData(req),
124-
},
125-
server_name: global.process.env.SENTRY_NAME || os.hostname(),
126-
};
141+
};
142+
}
143+
144+
if (options.serverName) {
145+
event.server_name = global.process.env.SENTRY_NAME || os.hostname();
146+
}
127147

128-
if (req.user) {
129-
preparedEvent.user = {
148+
if (options.user && req.user) {
149+
event.user = {
130150
...event.user,
131151
...extractUserData(req),
132152
};
133153
}
134154

135-
return preparedEvent;
155+
return event;
136156
}
137157

138158
/** JSDoc */
139-
export function requestHandler(): (req: http.IncomingMessage, res: http.ServerResponse, next: () => void) => void {
159+
export function requestHandler(options?: {
160+
version?: boolean;
161+
request?: boolean;
162+
serverName?: boolean;
163+
user?: boolean;
164+
}): (req: http.IncomingMessage, res: http.ServerResponse, next: () => void) => void {
140165
return function sentryRequestMiddleware(
141166
req: http.IncomingMessage,
142167
_res: http.ServerResponse,
@@ -146,7 +171,7 @@ export function requestHandler(): (req: http.IncomingMessage, res: http.ServerRe
146171
const hub = getHubFromCarrier(req);
147172
hub.bindClient(getCurrentHub().getClient());
148173
hub.configureScope((scope: Scope) => {
149-
scope.addEventProcessor(async (event: SentryEvent) => parseRequest(event, req));
174+
scope.addEventProcessor(async (event: SentryEvent) => parseRequest(event, req, options));
150175
});
151176
local.on('error', next);
152177
local.run(next);

0 commit comments

Comments
 (0)