Skip to content

Commit

Permalink
feat(nextjs): Set transaction names on scope for route handlers and g…
Browse files Browse the repository at this point in the history
…eneration functions (#11869)

for server components:
#11850
  • Loading branch information
s1gr1d authored May 2, 2024
1 parent ac59e7e commit 08a3b7c
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 90 deletions.
88 changes: 45 additions & 43 deletions packages/nextjs/src/common/wrapGenerationFunctionWithSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
SPAN_STATUS_OK,
captureException,
getClient,
getCurrentScope,
handleCallbackErrors,
startSpanManual,
withIsolationScope,
withScope,
} from '@sentry/core';
import type { WebFetchHeaders } from '@sentry/types';
import { propagationContextFromHeaders, winterCGHeadersToDict } from '@sentry/utils';
Expand Down Expand Up @@ -59,51 +59,53 @@ export function wrapGenerationFunctionWithSentry<F extends (...args: any[]) => a
const propagationContext = commonObjectToPropagationContext(headers, incomingPropagationContext);

return withIsolationScope(isolationScope, () => {
isolationScope.setTransactionName(`${componentType}.${generationFunctionIdentifier} (${componentRoute})`);
isolationScope.setSDKProcessingMetadata({
request: {
headers: headers ? winterCGHeadersToDict(headers) : undefined,
},
});
return withScope(scope => {
scope.setTransactionName(`${componentType}.${generationFunctionIdentifier} (${componentRoute})`);
isolationScope.setSDKProcessingMetadata({
request: {
headers: headers ? winterCGHeadersToDict(headers) : undefined,
},
});

getCurrentScope().setExtra('route_data', data);
getCurrentScope().setPropagationContext(propagationContext);
scope.setExtra('route_data', data);
scope.setPropagationContext(propagationContext);

return startSpanManual(
{
op: 'function.nextjs',
name: `${componentType}.${generationFunctionIdentifier} (${componentRoute})`,
forceTransaction: true,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nextjs',
},
},
span => {
return handleCallbackErrors(
() => originalFunction.apply(thisArg, args),
err => {
if (isNotFoundNavigationError(err)) {
// We don't want to report "not-found"s
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'not_found' });
} else if (isRedirectNavigationError(err)) {
// We don't want to report redirects
span.setStatus({ code: SPAN_STATUS_OK });
} else {
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
captureException(err, {
mechanism: {
handled: false,
},
});
}
},
() => {
span.end();
return startSpanManual(
{
op: 'function.nextjs',
name: `${componentType}.${generationFunctionIdentifier} (${componentRoute})`,
forceTransaction: true,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nextjs',
},
);
},
);
},
span => {
return handleCallbackErrors(
() => originalFunction.apply(thisArg, args),
err => {
if (isNotFoundNavigationError(err)) {
// We don't want to report "not-found"s
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'not_found' });
} else if (isRedirectNavigationError(err)) {
// We don't want to report redirects
span.setStatus({ code: SPAN_STATUS_OK });
} else {
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
captureException(err, {
mechanism: {
handled: false,
},
});
}
},
() => {
span.end();
},
);
},
);
});
});
});
},
Expand Down
96 changes: 49 additions & 47 deletions packages/nextjs/src/common/wrapRouteHandlerWithSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
SPAN_STATUS_ERROR,
captureException,
getCurrentScope,
handleCallbackErrors,
setHttpStatus,
startSpan,
withIsolationScope,
withScope,
} from '@sentry/core';
import { propagationContextFromHeaders, winterCGHeadersToDict } from '@sentry/utils';
import { isNotFoundNavigationError, isRedirectNavigationError } from './nextNavigationErrorUtils';
Expand Down Expand Up @@ -51,57 +51,59 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(

const propagationContext = commonObjectToPropagationContext(headers, incomingPropagationContext);

return withIsolationScope(isolationScope, async () => {
isolationScope.setTransactionName(`${method} ${parameterizedRoute}`);
getCurrentScope().setPropagationContext(propagationContext);
try {
return startSpan(
{
name: `${method} ${parameterizedRoute}`,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'http.server',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nextjs',
},
forceTransaction: true,
},
async span => {
const response: Response = await handleCallbackErrors(
() => originalFunction.apply(thisArg, args),
error => {
// Next.js throws errors when calling `redirect()`. We don't wanna report these.
if (isRedirectNavigationError(error)) {
// Don't do anything
} else if (isNotFoundNavigationError(error) && span) {
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'not_found' });
} else {
captureException(error, {
mechanism: {
handled: false,
},
});
}
return withIsolationScope(isolationScope, () => {
return withScope(async scope => {
scope.setTransactionName(`${method} ${parameterizedRoute}`);
scope.setPropagationContext(propagationContext);
try {
return startSpan(
{
name: `${method} ${parameterizedRoute}`,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'http.server',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nextjs',
},
);
forceTransaction: true,
},
async span => {
const response: Response = await handleCallbackErrors(
() => originalFunction.apply(thisArg, args),
error => {
// Next.js throws errors when calling `redirect()`. We don't wanna report these.
if (isRedirectNavigationError(error)) {
// Don't do anything
} else if (isNotFoundNavigationError(error) && span) {
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'not_found' });
} else {
captureException(error, {
mechanism: {
handled: false,
},
});
}
},
);

try {
if (span && response.status) {
setHttpStatus(span, response.status);
try {
if (span && response.status) {
setHttpStatus(span, response.status);
}
} catch {
// best effort - response may be undefined?
}
} catch {
// best effort - response may be undefined?
}

return response;
},
);
} finally {
if (!platformSupportsStreaming() || process.env.NEXT_RUNTIME === 'edge') {
// 1. Edge transport requires manual flushing
// 2. Lambdas require manual flushing to prevent execution freeze before the event is sent
await flushQueue();
return response;
},
);
} finally {
if (!platformSupportsStreaming() || process.env.NEXT_RUNTIME === 'edge') {
// 1. Edge transport requires manual flushing
// 2. Lambdas require manual flushing to prevent execution freeze before the event is sent
await flushQueue();
}
}
}
});
});
});
},
Expand Down

0 comments on commit 08a3b7c

Please sign in to comment.