Skip to content

fix nextjs PR #11450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions packages/nextjs/src/common/wrapRouteHandlerWithSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
handleCallbackErrors,
setHttpStatus,
startSpan,
withIsolationScope,
} from '@sentry/core';
import type { Span } from '@sentry/types';
import { winterCGHeadersToDict } from '@sentry/utils';
Expand All @@ -22,10 +21,7 @@ import { withIsolationScopeOrReuseFromRootSpan } from './utils/withIsolationScop

/** As our own HTTP integration is disabled (src/server/index.ts) the rootSpan comes from Next.js.
* In case there is not root span, we start a new span. */
function startOrUpdateSpan(
spanName: string,
handleResponseErrors: (rootSpan: Span) => Promise<Response>,
): Promise<Response> {
function startOrUpdateSpan(spanName: string, cb: (rootSpan: Span) => Promise<Response>): Promise<Response> {
const activeSpan = getActiveSpan();
const rootSpan = activeSpan && getRootSpan(activeSpan);

Expand All @@ -35,7 +31,7 @@ function startOrUpdateSpan(
rootSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'http.server');
rootSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.function.nextjs');

return handleResponseErrors(rootSpan);
return cb(rootSpan);
} else {
return startSpan(
{
Expand All @@ -48,7 +44,7 @@ function startOrUpdateSpan(
},
},
(span: Span) => {
return handleResponseErrors(span);
return cb(span);
},
);
}
Expand All @@ -68,7 +64,7 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(

return new Proxy(routeHandler, {
apply: (originalFunction, thisArg, args) => {
return withIsolationScope(async isolationScope => {
return withIsolationScopeOrReuseFromRootSpan(async isolationScope => {
isolationScope.setSDKProcessingMetadata({
request: {
headers: headers ? winterCGHeadersToDict(headers) : undefined,
Expand Down
25 changes: 18 additions & 7 deletions packages/nextjs/src/server/requestIsolationScopeIntegration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { SpanKind } from '@opentelemetry/api';
import { defineIntegration, spanToJSON } from '@sentry/core';
import { getSpanKind, getSpanScopes } from '@sentry/opentelemetry';
import {
defineIntegration,
getCapturedScopesOnSpan,
getCurrentScope,
getIsolationScope,
setCapturedScopesOnSpan,
spanToJSON,
} from '@sentry/core';
import { getSpanKind } from '@sentry/opentelemetry';

/**
* This integration is responsible for creating isolation scopes for incoming Http requests.
Expand All @@ -15,13 +22,17 @@ export const requestIsolationScopeIntegration = defineIntegration(() => {
setup(client) {
client.on('spanStart', span => {
const spanJson = spanToJSON(span);
const data = spanJson.data || {};

// The following check is a heuristic to determine whether the started span is a span that tracks an incoming HTTP request
if (getSpanKind(span) === SpanKind.SERVER && spanJson.data && 'http.method' in spanJson.data) {
const scopes = getSpanScopes(span);
if (scopes) {
scopes.isolationScope = scopes.isolationScope.clone();
}
if ((getSpanKind(span) === SpanKind.SERVER && data['http.method']) || data['next.route']) {
const scopes = getCapturedScopesOnSpan(span);

// Update the isolation scope, isolate this request
const isolationScope = (scopes.isolationScope || getIsolationScope()).clone();
const scope = scopes.scope || getCurrentScope();

setCapturedScopesOnSpan(span, scope, isolationScope);
}
});
},
Expand Down