@@ -2,7 +2,7 @@ import { logger } from '@sentry/core';
2
2
import { getHubFromCarrier , Scope } from '@sentry/hub' ;
3
3
import { SentryEvent , Severity } from '@sentry/types' ;
4
4
import { forget } from '@sentry/utils/async' ;
5
- import { serialize } from '@sentry/utils/object' ;
5
+ import { clone , serialize } from '@sentry/utils/object' ;
6
6
import * as cookie from 'cookie' ;
7
7
import * as domain from 'domain' ;
8
8
import * as http from 'http' ;
@@ -201,6 +201,40 @@ function parseRequest(
201
201
return event ;
202
202
}
203
203
204
+ /** Inherit globally configured data from Scope, but make sure to not use references */
205
+ function inheritGlobalScope ( to : Scope , from : Scope ) : void {
206
+ const user = from . getUser ( ) ;
207
+ if ( user ) {
208
+ to . setUser ( clone ( user ) ) ;
209
+ }
210
+
211
+ const tags = from . getTags ( ) ;
212
+ if ( tags ) {
213
+ const clonedTags = clone ( tags ) ;
214
+ Object . keys ( clonedTags ) . forEach ( key => {
215
+ to . setTag ( key , clonedTags [ key ] ) ;
216
+ } ) ;
217
+ }
218
+
219
+ const extra = from . getExtra ( ) ;
220
+ if ( extra ) {
221
+ const clonedExtra = clone ( extra ) ;
222
+ Object . keys ( clonedExtra ) . forEach ( key => {
223
+ to . setExtra ( key , clonedExtra [ key ] ) ;
224
+ } ) ;
225
+ }
226
+
227
+ const fingerprint = from . getFingerprint ( ) ;
228
+ if ( fingerprint ) {
229
+ to . setFingerprint ( clone ( fingerprint ) ) ;
230
+ }
231
+
232
+ const level = from . getLevel ( ) ;
233
+ if ( level ) {
234
+ to . setLevel ( level ) ;
235
+ }
236
+ }
237
+
204
238
/** JSDoc */
205
239
export function requestHandler ( options ?: {
206
240
request ?: boolean ;
@@ -215,9 +249,14 @@ export function requestHandler(options?: {
215
249
next : ( ) => void ,
216
250
) : void {
217
251
const local = domain . create ( ) ;
252
+ const currentHub = getCurrentHub ( ) ;
253
+ const currentScope = currentHub . getScope ( ) ;
218
254
const hub = getHubFromCarrier ( req ) ;
219
- hub . bindClient ( getCurrentHub ( ) . getClient ( ) ) ;
255
+ hub . bindClient ( currentHub . getClient ( ) ) ;
220
256
hub . configureScope ( ( scope : Scope ) => {
257
+ if ( currentScope ) {
258
+ inheritGlobalScope ( scope , currentScope ) ;
259
+ }
221
260
scope . addEventProcessor ( async ( event : SentryEvent ) => parseRequest ( event , req , options ) ) ;
222
261
} ) ;
223
262
local . on ( 'error' , next ) ;
0 commit comments