@@ -3,16 +3,19 @@ import {
33 SPAN_STATUS_ERROR ,
44 SPAN_STATUS_OK ,
55 captureException ,
6+ getActiveSpan ,
67 getClient ,
78 handleCallbackErrors ,
89 startSpanManual ,
910 withIsolationScope ,
1011 withScope ,
1112} from '@sentry/core' ;
1213import type { WebFetchHeaders } from '@sentry/types' ;
13- import { propagationContextFromHeaders , winterCGHeadersToDict } from '@sentry/utils' ;
14+ import { propagationContextFromHeaders , uuid4 , winterCGHeadersToDict } from '@sentry/utils' ;
1415
16+ import { context as otelContext } from '@opentelemetry/api' ;
1517import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core' ;
18+ import { EXPERIMENTAL_SENTRY_REQUEST_SPAN_ID_SUGGESTION_CONTEXT_KEY } from '@sentry/opentelemetry' ;
1619import type { GenerationFunctionContext } from '../common/types' ;
1720import { isNotFoundNavigationError , isRedirectNavigationError } from './nextNavigationErrorUtils' ;
1821import {
@@ -32,6 +35,7 @@ export function wrapGenerationFunctionWithSentry<F extends (...args: any[]) => a
3235 const { requestAsyncStorage, componentRoute, componentType, generationFunctionIdentifier } = context ;
3336 return new Proxy ( generationFunction , {
3437 apply : ( originalFunction , thisArg , args ) => {
38+ const requestTraceId = getActiveSpan ( ) ?. spanContext ( ) . traceId ;
3539 return escapeNextjsTracing ( ( ) => {
3640 let headers : WebFetchHeaders | undefined = undefined ;
3741 // We try-catch here just in case anything goes wrong with the async storage here goes wrong since it is Next.js internal API
@@ -50,22 +54,32 @@ export function wrapGenerationFunctionWithSentry<F extends (...args: any[]) => a
5054 data = { params, searchParams } ;
5155 }
5256
53- const incomingPropagationContext = propagationContextFromHeaders (
54- headers ?. get ( 'sentry-trace' ) ?? undefined ,
55- headers ?. get ( 'baggage' ) ,
56- ) ;
57+ const completeHeadersDict : Record < string , string > = headers ? winterCGHeadersToDict ( headers ) : { } ;
5758
5859 const isolationScope = commonObjectToIsolationScope ( headers ) ;
59- const propagationContext = commonObjectToPropagationContext ( headers , incomingPropagationContext ) ;
60+
61+ isolationScope . setSDKProcessingMetadata ( {
62+ request : {
63+ headers : completeHeadersDict ,
64+ } ,
65+ } ) ;
6066
6167 return withIsolationScope ( isolationScope , ( ) => {
6268 return withScope ( scope => {
6369 scope . setTransactionName ( `${ componentType } .${ generationFunctionIdentifier } (${ componentRoute } )` ) ;
64- isolationScope . setSDKProcessingMetadata ( {
65- request : {
66- headers : headers ? winterCGHeadersToDict ( headers ) : undefined ,
67- } ,
68- } ) ;
70+
71+ const propagationContext = commonObjectToPropagationContext (
72+ headers ,
73+ completeHeadersDict [ 'sentry-trace' ]
74+ ? propagationContextFromHeaders ( completeHeadersDict [ 'sentry-trace' ] , completeHeadersDict [ 'baggage' ] )
75+ : {
76+ traceId : requestTraceId || uuid4 ( ) ,
77+ spanId : uuid4 ( ) . substring ( 16 ) ,
78+ parentSpanId : otelContext
79+ . active ( )
80+ . getValue ( EXPERIMENTAL_SENTRY_REQUEST_SPAN_ID_SUGGESTION_CONTEXT_KEY ) as string | undefined ,
81+ } ,
82+ ) ;
6983
7084 scope . setExtra ( 'route_data' , data ) ;
7185 scope . setPropagationContext ( propagationContext ) ;
0 commit comments