Skip to content
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

feat(nextjs): Fork isolation scopes when incoming HTTP spans are created #11319

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/nextjs/package.json
Expand Up @@ -35,12 +35,14 @@
"access": "public"
},
"dependencies": {
"@opentelemetry/api": "1.7.0",
"@rollup/plugin-commonjs": "24.0.0",
"@sentry/core": "8.0.0-alpha.5",
"@sentry/node": "8.0.0-alpha.5",
"@sentry/react": "8.0.0-alpha.5",
"@sentry/types": "8.0.0-alpha.5",
"@sentry/utils": "8.0.0-alpha.5",
"@sentry/opentelemetry": "8.0.0-alpha.5",
"@sentry/vercel-edge": "8.0.0-alpha.5",
"@sentry/webpack-plugin": "2.16.0",
"chalk": "3.0.0",
Expand Down
29 changes: 29 additions & 0 deletions packages/nextjs/src/server/requestIsolationScopeIntegration.ts
@@ -0,0 +1,29 @@
import { SpanKind } from '@opentelemetry/api';
import { defineIntegration, spanToJSON } from '@sentry/core';
import { getSpanKind, getSpanScopes } from '@sentry/opentelemetry';

/**
* This integration is responsible for creating isolation scopes for incoming Http requests.
* We do so by waiting for http spans to be created and then forking the isolation scope.
*
* Normally the isolation scopes would be created by our Http instrumentation, however Next.js brings it's own Http
* instrumentation so we had to disable ours.
*/
export const requestIsolationScopeIntegration = defineIntegration(() => {
return {
name: 'RequestIsolationScope',
setup(client) {
client.on('spanStart', span => {
const spanJson = spanToJSON(span);

// 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();
}
}
});
},
};
});