Skip to content

Commit

Permalink
feat(nextjs): Add status to data-fetcher spans (#5777)
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst committed Sep 20, 2022
1 parent 9a6f98e commit 17e4cb8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
22 changes: 19 additions & 3 deletions packages/nextjs/src/config/wrappers/wrapperUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export function callTracedServerSideDataFetcher<F extends (...args: any[]) => Pr
op: 'nextjs.data.server',
name: options.requestedRouteName,
...traceparentData,
status: 'ok',
metadata: {
source: 'route',
dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext,
Expand All @@ -116,6 +117,7 @@ export function callTracedServerSideDataFetcher<F extends (...args: any[]) => Pr
const dataFetcherSpan = requestTransaction.startChild({
op: 'nextjs.data.server',
description: `${options.dataFetchingMethodName} (${options.dataFetcherRouteName})`,
status: 'ok',
});

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

try {
return await origFunction(...origFunctionArguments);
} catch (e) {
// Since we finish the span before the error can bubble up and trigger the handlers in `registerErrorInstrumentation`
// that set the transaction status, we need to manually set the status of the span & transaction
dataFetcherSpan.setStatus('internal_error');

const transaction = dataFetcherSpan.transaction;
if (transaction) {
transaction.setStatus('internal_error');
}

throw e;
} finally {
dataFetcherSpan.finish();
}
Expand Down Expand Up @@ -178,14 +191,17 @@ export async function callDataFetcherTraced<F extends (...args: any[]) => Promis
const span = transaction.startChild({
op: 'nextjs.data.server',
description: `${dataFetchingMethodName} (${parameterizedRoute})`,
status: 'ok',
});

try {
return await origFunction(...origFunctionArgs);
} catch (err) {
if (span) {
span.finish();
}
// Since we finish the span before the error can bubble up and trigger the handlers in `registerErrorInstrumentation`
// that set the transaction status, we need to manually set the status of the span & transaction
transaction.setStatus('internal_error');
span.setStatus('internal_error');
span.finish();

// TODO Copy more robust error handling over from `withSentry`
captureException(err);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const assert = require('assert');

const { sleep } = require('../utils/common');
const { getAsync, interceptEventRequest } = require('../utils/server');
const { getAsync, interceptEventRequest, interceptTracingRequest } = require('../utils/server');

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

const capturedTransactionRequest = interceptTracingRequest(
{
contexts: {
trace: {
op: 'nextjs.data.server',
status: 'internal_error',
},
},
transaction: '/withErrorServerSideProps',
transaction_info: {
source: 'route',
changes: [],
propagations: 0,
},
type: 'transaction',
request: {
url,
},
},
argv,
'errorServerSideProps',
);

await getAsync(url);
await sleep(250);

assert.ok(capturedRequest.isDone(), 'Did not intercept expected request');
assert.ok(capturedTransactionRequest.isDone(), 'Did not intercept expected transaction request');
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = async ({ url: urlBase, argv }) => {
contexts: {
trace: {
op: 'nextjs.data.server',
status: 'ok',
},
},
transaction: '/[id]/withInitialProps',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = async ({ url: urlBase, argv }) => {
contexts: {
trace: {
op: 'nextjs.data.server',
status: 'ok',
},
},
transaction: '/[id]/withServerSideProps',
Expand Down

0 comments on commit 17e4cb8

Please sign in to comment.