|
1 | 1 | import { getCurrentHub } from '@sentry/core'; |
2 | 2 | import { WINDOW } from '@sentry/react'; |
3 | | -import type { Primitive, TraceparentData, Transaction, TransactionContext, TransactionSource } from '@sentry/types'; |
4 | | -import { |
5 | | - baggageHeaderToDynamicSamplingContext, |
6 | | - extractTraceparentData, |
7 | | - logger, |
8 | | - stripUrlQueryAndFragment, |
9 | | -} from '@sentry/utils'; |
| 3 | +import type { Primitive, Transaction, TransactionContext, TransactionSource } from '@sentry/types'; |
| 4 | +import { logger, stripUrlQueryAndFragment, tracingContextFromHeaders } from '@sentry/utils'; |
10 | 5 | import type { NEXT_DATA as NextData } from 'next/dist/next-server/lib/utils'; |
11 | 6 | import { default as Router } from 'next/router'; |
12 | 7 | import type { ParsedUrlQuery } from 'querystring'; |
@@ -37,9 +32,9 @@ interface SentryEnhancedNextData extends NextData { |
37 | 32 |
|
38 | 33 | interface NextDataTagInfo { |
39 | 34 | route?: string; |
40 | | - traceParentData?: TraceparentData; |
41 | | - baggage?: string; |
42 | 35 | params?: ParsedUrlQuery; |
| 36 | + sentryTrace?: string; |
| 37 | + baggage?: string; |
43 | 38 | } |
44 | 39 |
|
45 | 40 | /** |
@@ -83,13 +78,8 @@ function extractNextDataTagInformation(): NextDataTagInfo { |
83 | 78 | nextDataTagInfo.params = query; |
84 | 79 |
|
85 | 80 | if (props && props.pageProps) { |
86 | | - if (props.pageProps._sentryBaggage) { |
87 | | - nextDataTagInfo.baggage = props.pageProps._sentryBaggage; |
88 | | - } |
89 | | - |
90 | | - if (props.pageProps._sentryTraceData) { |
91 | | - nextDataTagInfo.traceParentData = extractTraceparentData(props.pageProps._sentryTraceData); |
92 | | - } |
| 81 | + nextDataTagInfo.sentryTrace = props.pageProps._sentryTraceData; |
| 82 | + nextDataTagInfo.baggage = props.pageProps._sentryBaggage; |
93 | 83 | } |
94 | 84 |
|
95 | 85 | return nextDataTagInfo; |
@@ -121,22 +111,26 @@ export function nextRouterInstrumentation( |
121 | 111 | startTransactionOnPageLoad: boolean = true, |
122 | 112 | startTransactionOnLocationChange: boolean = true, |
123 | 113 | ): void { |
124 | | - const { route, traceParentData, baggage, params } = extractNextDataTagInformation(); |
| 114 | + const { route, params, sentryTrace, baggage } = extractNextDataTagInformation(); |
| 115 | + const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders( |
| 116 | + sentryTrace, |
| 117 | + baggage, |
| 118 | + ); |
| 119 | + |
| 120 | + getCurrentHub().getScope().setPropagationContext(propagationContext); |
125 | 121 | prevLocationName = route || globalObject.location.pathname; |
126 | 122 |
|
127 | 123 | if (startTransactionOnPageLoad) { |
128 | 124 | const source = route ? 'route' : 'url'; |
129 | | - const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(baggage); |
130 | | - |
131 | 125 | activeTransaction = startTransactionCb({ |
132 | 126 | name: prevLocationName, |
133 | 127 | op: 'pageload', |
134 | 128 | tags: DEFAULT_TAGS, |
135 | 129 | ...(params && client && client.getOptions().sendDefaultPii && { data: params }), |
136 | | - ...traceParentData, |
| 130 | + ...traceparentData, |
137 | 131 | metadata: { |
138 | 132 | source, |
139 | | - dynamicSamplingContext: traceParentData && !dynamicSamplingContext ? {} : dynamicSamplingContext, |
| 133 | + dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext, |
140 | 134 | }, |
141 | 135 | }); |
142 | 136 | } |
|
0 commit comments