22import { captureException , getCurrentHub , Hub } from '@sentry/node' ;
33import { getActiveTransaction , hasTracingEnabled } from '@sentry/tracing' ;
44import { 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' ;
615import * as domain from 'domain' ;
716
817import {
@@ -289,26 +298,38 @@ function matchServerRoutes(
289298 * @param pkg
290299 */
291300export function startRequestHandlerTransaction (
301+ hub : Hub ,
292302 url : URL ,
293- method : string ,
294303 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+ } ,
297312) : Transaction {
298313 const currentScope = hub . getScope ( ) ;
299314 const matches = matchServerRoutes ( routes , url . pathname , pkg ) ;
300315
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+
301320 const match = matches && getRequestMatch ( url , matches ) ;
302321 const name = match === null ? url . pathname : match . route . id ;
303322 const source = match === null ? 'url' : 'route' ;
304323 const transaction = hub . startTransaction ( {
305324 name,
306325 op : 'http.server' ,
307326 tags : {
308- method : method ,
327+ method : request . method ,
309328 } ,
329+ ...traceparentData ,
310330 metadata : {
311331 source,
332+ baggage,
312333 } ,
313334 } ) ;
314335
@@ -330,7 +351,13 @@ function wrapRequestHandler(origRequestHandler: RequestHandler, build: ServerBui
330351 }
331352
332353 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+ } ) ;
334361
335362 const res = ( await origRequestHandler . call ( this , request , loadContext ) ) as Response ;
336363
0 commit comments