Skip to content

Commit bc1004e

Browse files
committed
store express request handler options in scope metadata
1 parent e95527e commit bc1004e

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

packages/node/src/handlers.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
AddRequestDataToEventOptions,
66
addRequestDataToTransaction,
77
baggageHeaderToDynamicSamplingContext,
8+
dropUndefinedKeys,
89
extractPathForTransaction,
910
extractTraceparentData,
1011
isString,
@@ -109,13 +110,41 @@ export type RequestHandlerOptions =
109110
flushTimeout?: number;
110111
};
111112

113+
/**
114+
* Backwards compatibility shim which can be removed in v8. Forces the given options to follow the
115+
* `AddRequestDataToEventOptions` interface.
116+
*
117+
* TODO (v8): Get rid of this, and stop passing `requestDataOptionsFromExpressHandler` to `setSDKProcessingMetadata`.
118+
*/
119+
function convertReqHandlerOptsToAddReqDataOpts(
120+
reqHandlerOptions: RequestHandlerOptions = {},
121+
): AddRequestDataToEventOptions | undefined {
122+
let addRequestDataOptions: AddRequestDataToEventOptions | undefined;
123+
124+
if ('include' in reqHandlerOptions) {
125+
addRequestDataOptions = { include: reqHandlerOptions.include };
126+
} else {
127+
// eslint-disable-next-line deprecation/deprecation
128+
const { ip, request, transaction, user } = reqHandlerOptions as ParseRequestOptions;
129+
130+
if (ip || request || transaction || user) {
131+
addRequestDataOptions = { include: dropUndefinedKeys({ ip, request, transaction, user }) };
132+
}
133+
}
134+
135+
return addRequestDataOptions;
136+
}
137+
112138
/**
113139
* Express compatible request handler.
114140
* @see Exposed as `Handlers.requestHandler`
115141
*/
116142
export function requestHandler(
117143
options?: RequestHandlerOptions,
118144
): (req: http.IncomingMessage, res: http.ServerResponse, next: (error?: any) => void) => void {
145+
// TODO (v8): Get rid of this
146+
const requestDataOptions = convertReqHandlerOptsToAddReqDataOpts(options);
147+
119148
const currentHub = getCurrentHub();
120149
const client = currentHub.getClient<NodeClient>();
121150
// Initialise an instance of SessionFlusher on the client when `autoSessionTracking` is enabled and the
@@ -167,7 +196,11 @@ export function requestHandler(
167196

168197
currentHub.configureScope(scope => {
169198
scope.addEventProcessor(backwardsCompatibleEventProcessor);
170-
scope.setSDKProcessingMetadata({ request: req });
199+
scope.setSDKProcessingMetadata({
200+
request: req,
201+
// TODO (v8): Stop passing this
202+
requestDataOptionsFromExpressHandler: requestDataOptions,
203+
});
171204

172205
const client = currentHub.getClient<NodeClient>();
173206
if (isAutoSessionTrackingEnabled(client)) {

packages/types/src/transaction.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ export interface TransactionMetadata {
149149
/** For transactions tracing server-side request handling, the request being tracked. */
150150
request?: PolymorphicRequest;
151151

152+
/** Compatibility shim for transitioning to the `RequestData` integration. The options passed to our Express request
153+
* handler controlling what request data is added to the event.
154+
* TODO (v8): This should go away
155+
*/
156+
requestDataOptionsFromExpressHandler?: { [key: string]: unknown };
157+
152158
/** For transactions tracing server-side request handling, the path of the request being tracked. */
153159
/** TODO: If we rm -rf `instrumentServer`, this can go, too */
154160
requestPath?: string;

0 commit comments

Comments
 (0)