Skip to content

Commit 17e4cb8

Browse files
authored
feat(nextjs): Add status to data-fetcher spans (#5777)
1 parent 9a6f98e commit 17e4cb8

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

packages/nextjs/src/config/wrappers/wrapperUtils.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export function callTracedServerSideDataFetcher<F extends (...args: any[]) => Pr
9898
op: 'nextjs.data.server',
9999
name: options.requestedRouteName,
100100
...traceparentData,
101+
status: 'ok',
101102
metadata: {
102103
source: 'route',
103104
dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext,
@@ -116,6 +117,7 @@ export function callTracedServerSideDataFetcher<F extends (...args: any[]) => Pr
116117
const dataFetcherSpan = requestTransaction.startChild({
117118
op: 'nextjs.data.server',
118119
description: `${options.dataFetchingMethodName} (${options.dataFetcherRouteName})`,
120+
status: 'ok',
119121
});
120122

121123
const currentScope = getCurrentHub().getScope();
@@ -137,6 +139,17 @@ export function callTracedServerSideDataFetcher<F extends (...args: any[]) => Pr
137139

138140
try {
139141
return await origFunction(...origFunctionArguments);
142+
} catch (e) {
143+
// Since we finish the span before the error can bubble up and trigger the handlers in `registerErrorInstrumentation`
144+
// that set the transaction status, we need to manually set the status of the span & transaction
145+
dataFetcherSpan.setStatus('internal_error');
146+
147+
const transaction = dataFetcherSpan.transaction;
148+
if (transaction) {
149+
transaction.setStatus('internal_error');
150+
}
151+
152+
throw e;
140153
} finally {
141154
dataFetcherSpan.finish();
142155
}
@@ -178,14 +191,17 @@ export async function callDataFetcherTraced<F extends (...args: any[]) => Promis
178191
const span = transaction.startChild({
179192
op: 'nextjs.data.server',
180193
description: `${dataFetchingMethodName} (${parameterizedRoute})`,
194+
status: 'ok',
181195
});
182196

183197
try {
184198
return await origFunction(...origFunctionArgs);
185199
} catch (err) {
186-
if (span) {
187-
span.finish();
188-
}
200+
// Since we finish the span before the error can bubble up and trigger the handlers in `registerErrorInstrumentation`
201+
// that set the transaction status, we need to manually set the status of the span & transaction
202+
transaction.setStatus('internal_error');
203+
span.setStatus('internal_error');
204+
span.finish();
189205

190206
// TODO Copy more robust error handling over from `withSentry`
191207
captureException(err);

packages/nextjs/test/integration/test/server/errorServerSideProps.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const assert = require('assert');
22

33
const { sleep } = require('../utils/common');
4-
const { getAsync, interceptEventRequest } = require('../utils/server');
4+
const { getAsync, interceptEventRequest, interceptTracingRequest } = require('../utils/server');
55

66
module.exports = async ({ url: urlBase, argv }) => {
77
const url = `${urlBase}/withErrorServerSideProps`;
@@ -28,8 +28,32 @@ module.exports = async ({ url: urlBase, argv }) => {
2828
'errorServerSideProps',
2929
);
3030

31+
const capturedTransactionRequest = interceptTracingRequest(
32+
{
33+
contexts: {
34+
trace: {
35+
op: 'nextjs.data.server',
36+
status: 'internal_error',
37+
},
38+
},
39+
transaction: '/withErrorServerSideProps',
40+
transaction_info: {
41+
source: 'route',
42+
changes: [],
43+
propagations: 0,
44+
},
45+
type: 'transaction',
46+
request: {
47+
url,
48+
},
49+
},
50+
argv,
51+
'errorServerSideProps',
52+
);
53+
3154
await getAsync(url);
3255
await sleep(250);
3356

3457
assert.ok(capturedRequest.isDone(), 'Did not intercept expected request');
58+
assert.ok(capturedTransactionRequest.isDone(), 'Did not intercept expected transaction request');
3559
};

packages/nextjs/test/integration/test/server/tracingServerGetInitialProps.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = async ({ url: urlBase, argv }) => {
1111
contexts: {
1212
trace: {
1313
op: 'nextjs.data.server',
14+
status: 'ok',
1415
},
1516
},
1617
transaction: '/[id]/withInitialProps',

packages/nextjs/test/integration/test/server/tracingServerGetServerSideProps.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = async ({ url: urlBase, argv }) => {
1111
contexts: {
1212
trace: {
1313
op: 'nextjs.data.server',
14+
status: 'ok',
1415
},
1516
},
1617
transaction: '/[id]/withServerSideProps',

0 commit comments

Comments
 (0)