|
5 | 5 | AddRequestDataToEventOptions,
|
6 | 6 | addRequestDataToTransaction,
|
7 | 7 | baggageHeaderToDynamicSamplingContext,
|
| 8 | + dropUndefinedKeys, |
8 | 9 | extractPathForTransaction,
|
9 | 10 | extractTraceparentData,
|
10 | 11 | isString,
|
@@ -109,13 +110,41 @@ export type RequestHandlerOptions =
|
109 | 110 | flushTimeout?: number;
|
110 | 111 | };
|
111 | 112 |
|
| 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 | + |
112 | 138 | /**
|
113 | 139 | * Express compatible request handler.
|
114 | 140 | * @see Exposed as `Handlers.requestHandler`
|
115 | 141 | */
|
116 | 142 | export function requestHandler(
|
117 | 143 | options?: RequestHandlerOptions,
|
118 | 144 | ): (req: http.IncomingMessage, res: http.ServerResponse, next: (error?: any) => void) => void {
|
| 145 | + // TODO (v8): Get rid of this |
| 146 | + const requestDataOptions = convertReqHandlerOptsToAddReqDataOpts(options); |
| 147 | + |
119 | 148 | const currentHub = getCurrentHub();
|
120 | 149 | const client = currentHub.getClient<NodeClient>();
|
121 | 150 | // Initialise an instance of SessionFlusher on the client when `autoSessionTracking` is enabled and the
|
@@ -167,7 +196,11 @@ export function requestHandler(
|
167 | 196 |
|
168 | 197 | currentHub.configureScope(scope => {
|
169 | 198 | scope.addEventProcessor(backwardsCompatibleEventProcessor);
|
170 |
| - scope.setSDKProcessingMetadata({ request: req }); |
| 199 | + scope.setSDKProcessingMetadata({ |
| 200 | + request: req, |
| 201 | + // TODO (v8): Stop passing this |
| 202 | + requestDataOptionsFromExpressHandler: requestDataOptions, |
| 203 | + }); |
171 | 204 |
|
172 | 205 | const client = currentHub.getClient<NodeClient>();
|
173 | 206 | if (isAutoSessionTrackingEnabled(client)) {
|
|
0 commit comments