Skip to content

Commit 227cd4d

Browse files
author
Luca Forstner
committed
Traceparent data instead of traceId only
1 parent 936b42e commit 227cd4d

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

packages/nextjs/src/performance/client.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22

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';
512
import type { NEXT_DATA as NextData } from 'next/dist/next-server/lib/utils';
613
import { default as Router } from 'next/router';
714
import type { ParsedUrlQuery } from 'querystring';
@@ -16,10 +23,10 @@ type StartTransactionCb = (context: TransactionContext) => Transaction | undefin
1623
interface SentryEnhancedNextData extends NextData {
1724
// contains props returned by `getInitialProps` - except for `pageProps`, these are the props that got returned by `getServerSideProps` or `getStaticProps`
1825
props: {
19-
_sentryGetInitialPropsTraceId?: string; // trace id, if injected by server-side `getInitialProps`
26+
_sentryGetInitialPropsTraceData?: string; // trace id, if injected by server-side `getInitialProps`
2027
_sentryGetInitialPropsBaggage?: string; // baggage, if injected by server-side `getInitialProps`
2128
pageProps?: {
22-
_sentryGetServerSidePropsTraceId?: string; // trace id, if injected by server-side `getServerSideProps`
29+
_sentryGetServerSidePropsTraceData?: string; // trace id, if injected by server-side `getServerSideProps`
2330
_sentryGetServerSidePropsBaggage?: string; // baggage, if injected by server-side `getServerSideProps`
2431
};
2532
};
@@ -38,7 +45,7 @@ interface SentryEnhancedNextData extends NextData {
3845
*/
3946
function extractNextDataTagInformation(): {
4047
route: string | undefined;
41-
traceId: string | undefined;
48+
traceParentData: TraceparentData | undefined;
4249
baggage: string | undefined;
4350
params: ParsedUrlQuery | undefined;
4451
} {
@@ -57,20 +64,24 @@ function extractNextDataTagInformation(): {
5764

5865
// `nextData.page` always contains the parameterized route
5966
const route = (nextData || {}).page;
67+
const params = (nextData || {}).query;
6068

61-
const getServerSidePropsTraceId = (((nextData || {}).props || {}).pageProps || {})._sentryGetServerSidePropsTraceId;
62-
const getInitialPropsTraceId = ((nextData || {}).props || {})._sentryGetInitialPropsTraceId;
63-
const getServerSidePropsBaggage = (((nextData || {}).props || {}).pageProps || {})._sentryGetServerSidePropsBaggage;
6469
const getInitialPropsBaggage = ((nextData || {}).props || {})._sentryGetInitialPropsBaggage;
70+
const getServerSidePropsBaggage = (((nextData || {}).props || {}).pageProps || {})._sentryGetServerSidePropsBaggage;
6571

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;
6779

6880
return {
6981
route,
7082
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,
7485
};
7586
}
7687

@@ -98,17 +109,17 @@ export function nextRouterInstrumentation(
98109
startTransaction = startTransactionCb;
99110

100111
if (startTransactionOnPageLoad) {
101-
const { route, traceId, baggage, params } = extractNextDataTagInformation();
112+
const { route, traceParentData, baggage, params } = extractNextDataTagInformation();
102113

103114
prevTransactionName = route || global.document.location.pathname;
104115
const source = route ? 'route' : 'url';
105116

106117
activeTransaction = startTransactionCb({
107118
name: prevTransactionName,
108-
traceId,
109119
op: 'pageload',
110120
tags: DEFAULT_TAGS,
111121
...(params && { data: params }),
122+
...traceParentData,
112123
metadata: {
113124
source,
114125
...(baggage && { baggage: parseBaggageHeader(baggage) }),

packages/nextjs/test/performance/client.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe('client', () => {
8686
'/[user]/posts/[id]',
8787
{ user: 'lforst', id: '1337', q: '42' },
8888
{
89-
_sentryGetInitialPropsTraceId: 'SOME_TRACE_ID',
89+
_sentryGetInitialPropsTraceData: 'c82b8554881b4d28ad977de04a4fb40a-a755953cd3394d5f-1',
9090
_sentryGetInitialPropsBaggage:
9191
'other=vendor,foo=bar,third=party,last=item,sentry-release=2.1.0,sentry-environment=myEnv',
9292
},
@@ -106,7 +106,9 @@ describe('client', () => {
106106
source: 'route',
107107
baggage: [{ environment: 'myEnv', release: '2.1.0' }, '', true],
108108
},
109-
traceId: 'SOME_TRACE_ID',
109+
traceId: 'c82b8554881b4d28ad977de04a4fb40a',
110+
parentSpanId: 'a755953cd3394d5f',
111+
parentSampled: true,
110112
},
111113
],
112114
[
@@ -115,7 +117,7 @@ describe('client', () => {
115117
{},
116118
{
117119
pageProps: {
118-
_sentryGetServerSidePropsTraceId: 'SOME_TRACE_ID',
120+
_sentryGetServerSidePropsTraceData: 'c82b8554881b4d28ad977de04a4fb40a-a755953cd3394d5f-1',
119121
_sentryGetServerSidePropsBaggage:
120122
'other=vendor,foo=bar,third=party,last=item,sentry-release=2.1.0,sentry-environment=myEnv',
121123
},
@@ -132,7 +134,9 @@ describe('client', () => {
132134
source: 'route',
133135
baggage: [{ environment: 'myEnv', release: '2.1.0' }, '', true],
134136
},
135-
traceId: 'SOME_TRACE_ID',
137+
traceId: 'c82b8554881b4d28ad977de04a4fb40a',
138+
parentSpanId: 'a755953cd3394d5f',
139+
parentSampled: true,
136140
},
137141
],
138142
[

0 commit comments

Comments
 (0)