1
1
/* eslint-disable @typescript-eslint/no-explicit-any */
2
2
3
- import { Primitive , Transaction , TransactionContext } from '@sentry/types' ;
4
- import { fill , getGlobalObject , logger , parseBaggageHeader , stripUrlQueryAndFragment } from '@sentry/utils' ;
3
+ import { Primitive , TraceparentData , Transaction , TransactionContext } from '@sentry/types' ;
4
+ import {
5
+ extractTraceparentData ,
6
+ fill ,
7
+ getGlobalObject ,
8
+ logger ,
9
+ parseBaggageHeader ,
10
+ stripUrlQueryAndFragment ,
11
+ } from '@sentry/utils' ;
5
12
import type { NEXT_DATA as NextData } from 'next/dist/next-server/lib/utils' ;
6
13
import { default as Router } from 'next/router' ;
7
14
import type { ParsedUrlQuery } from 'querystring' ;
@@ -16,10 +23,10 @@ type StartTransactionCb = (context: TransactionContext) => Transaction | undefin
16
23
interface SentryEnhancedNextData extends NextData {
17
24
// contains props returned by `getInitialProps` - except for `pageProps`, these are the props that got returned by `getServerSideProps` or `getStaticProps`
18
25
props : {
19
- _sentryGetInitialPropsTraceId ?: string ; // trace id, if injected by server-side `getInitialProps`
26
+ _sentryGetInitialPropsTraceData ?: string ; // trace id, if injected by server-side `getInitialProps`
20
27
_sentryGetInitialPropsBaggage ?: string ; // baggage, if injected by server-side `getInitialProps`
21
28
pageProps ?: {
22
- _sentryGetServerSidePropsTraceId ?: string ; // trace id, if injected by server-side `getServerSideProps`
29
+ _sentryGetServerSidePropsTraceData ?: string ; // trace id, if injected by server-side `getServerSideProps`
23
30
_sentryGetServerSidePropsBaggage ?: string ; // baggage, if injected by server-side `getServerSideProps`
24
31
} ;
25
32
} ;
@@ -38,7 +45,7 @@ interface SentryEnhancedNextData extends NextData {
38
45
*/
39
46
function extractNextDataTagInformation ( ) : {
40
47
route : string | undefined ;
41
- traceId : string | undefined ;
48
+ traceParentData : TraceparentData | undefined ;
42
49
baggage : string | undefined ;
43
50
params : ParsedUrlQuery | undefined ;
44
51
} {
@@ -57,20 +64,24 @@ function extractNextDataTagInformation(): {
57
64
58
65
// `nextData.page` always contains the parameterized route
59
66
const route = ( nextData || { } ) . page ;
67
+ const params = ( nextData || { } ) . query ;
60
68
61
- const getServerSidePropsTraceId = ( ( ( nextData || { } ) . props || { } ) . pageProps || { } ) . _sentryGetServerSidePropsTraceId ;
62
- const getInitialPropsTraceId = ( ( nextData || { } ) . props || { } ) . _sentryGetInitialPropsTraceId ;
63
- const getServerSidePropsBaggage = ( ( ( nextData || { } ) . props || { } ) . pageProps || { } ) . _sentryGetServerSidePropsBaggage ;
64
69
const getInitialPropsBaggage = ( ( nextData || { } ) . props || { } ) . _sentryGetInitialPropsBaggage ;
70
+ const getServerSidePropsBaggage = ( ( ( nextData || { } ) . props || { } ) . pageProps || { } ) . _sentryGetServerSidePropsBaggage ;
65
71
66
- const params = ( nextData || { } ) . query ;
72
+ const getInitialPropsTraceData = ( ( nextData || { } ) . props || { } ) . _sentryGetInitialPropsTraceData ;
73
+ const getServerSidePropsTraceData = ( ( ( nextData || { } ) . props || { } ) . pageProps || { } )
74
+ . _sentryGetServerSidePropsTraceData ;
75
+
76
+ // Ordering of the following shouldn't matter but `getInitialProps` generally runs before `getServerSideProps` so we give it priority.
77
+ const baggage = getInitialPropsBaggage || getServerSidePropsBaggage ;
78
+ const traceData = getInitialPropsTraceData || getServerSidePropsTraceData ;
67
79
68
80
return {
69
81
route,
70
82
params,
71
- // Ordering of the following shouldn't matter but `getInitialProps` generally runs before `getServerSideProps` so we give it priority.
72
- traceId : getInitialPropsTraceId || getServerSidePropsTraceId ,
73
- baggage : getInitialPropsBaggage || getServerSidePropsBaggage ,
83
+ traceParentData : traceData ? extractTraceparentData ( traceData ) : undefined ,
84
+ baggage,
74
85
} ;
75
86
}
76
87
@@ -98,17 +109,17 @@ export function nextRouterInstrumentation(
98
109
startTransaction = startTransactionCb ;
99
110
100
111
if ( startTransactionOnPageLoad ) {
101
- const { route, traceId , baggage, params } = extractNextDataTagInformation ( ) ;
112
+ const { route, traceParentData , baggage, params } = extractNextDataTagInformation ( ) ;
102
113
103
114
prevTransactionName = route || global . document . location . pathname ;
104
115
const source = route ? 'route' : 'url' ;
105
116
106
117
activeTransaction = startTransactionCb ( {
107
118
name : prevTransactionName ,
108
- traceId,
109
119
op : 'pageload' ,
110
120
tags : DEFAULT_TAGS ,
111
121
...( params && { data : params } ) ,
122
+ ...traceParentData ,
112
123
metadata : {
113
124
source,
114
125
...( baggage && { baggage : parseBaggageHeader ( baggage ) } ) ,
0 commit comments