2
2
import { captureException , getCurrentHub , Hub } from '@sentry/node' ;
3
3
import { getActiveTransaction , hasTracingEnabled } from '@sentry/tracing' ;
4
4
import { Transaction , WrappedFunction } from '@sentry/types' ;
5
- import { addExceptionMechanism , fill , isNodeEnv , loadModule , logger , serializeBaggage } from '@sentry/utils' ;
5
+ import {
6
+ addExceptionMechanism ,
7
+ extractTraceparentData ,
8
+ fill ,
9
+ isNodeEnv ,
10
+ loadModule ,
11
+ logger ,
12
+ parseBaggageSetMutability ,
13
+ serializeBaggage ,
14
+ } from '@sentry/utils' ;
6
15
import * as domain from 'domain' ;
7
16
8
17
import {
@@ -289,26 +298,38 @@ function matchServerRoutes(
289
298
* @param pkg
290
299
*/
291
300
export function startRequestHandlerTransaction (
301
+ hub : Hub ,
292
302
url : URL ,
293
- method : string ,
294
303
routes : ServerRoute [ ] ,
295
- hub : Hub ,
296
- pkg ?: ReactRouterDomPkg ,
304
+ pkg : ReactRouterDomPkg | undefined ,
305
+ request : {
306
+ headers : {
307
+ 'sentry-trace' : string ;
308
+ baggage : string ;
309
+ } ;
310
+ method : string ;
311
+ } ,
297
312
) : Transaction {
298
313
const currentScope = hub . getScope ( ) ;
299
314
const matches = matchServerRoutes ( routes , url . pathname , pkg ) ;
300
315
316
+ // If there is a trace header set, we extract the data from it (parentSpanId, traceId, and sampling decision)
317
+ const traceparentData = extractTraceparentData ( request . headers [ 'sentry-trace' ] ) ;
318
+ const baggage = parseBaggageSetMutability ( request . headers . baggage , traceparentData ) ;
319
+
301
320
const match = matches && getRequestMatch ( url , matches ) ;
302
321
const name = match === null ? url . pathname : match . route . id ;
303
322
const source = match === null ? 'url' : 'route' ;
304
323
const transaction = hub . startTransaction ( {
305
324
name,
306
325
op : 'http.server' ,
307
326
tags : {
308
- method : method ,
327
+ method : request . method ,
309
328
} ,
329
+ ...traceparentData ,
310
330
metadata : {
311
331
source,
332
+ baggage,
312
333
} ,
313
334
} ) ;
314
335
@@ -330,7 +351,13 @@ function wrapRequestHandler(origRequestHandler: RequestHandler, build: ServerBui
330
351
}
331
352
332
353
const url = new URL ( request . url ) ;
333
- const transaction = startRequestHandlerTransaction ( url , request . method , routes , hub , pkg ) ;
354
+ const transaction = startRequestHandlerTransaction ( hub , url , routes , pkg , {
355
+ headers : {
356
+ 'sentry-trace' : request . headers . get ( 'sentry-trace' ) || '' ,
357
+ baggage : request . headers . get ( 'baggage' ) || '' ,
358
+ } ,
359
+ method : request . method ,
360
+ } ) ;
334
361
335
362
const res = ( await origRequestHandler . call ( this , request , loadContext ) ) as Response ;
336
363
0 commit comments