Skip to content

Commit 703db85

Browse files
Luca Forstnermydea
Luca Forstner
authored andcommitted
feat(nextjs): Fork isolation scopes when incoming HTTP spans are created
1 parent 8a03f27 commit 703db85

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

packages/nextjs/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@
6565
"access": "public"
6666
},
6767
"dependencies": {
68+
"@opentelemetry/api": "1.7.0",
6869
"@rollup/plugin-commonjs": "24.0.0",
6970
"@sentry/core": "8.0.0-alpha.9",
7071
"@sentry/node": "8.0.0-alpha.9",
7172
"@sentry/react": "8.0.0-alpha.9",
7273
"@sentry/types": "8.0.0-alpha.9",
7374
"@sentry/utils": "8.0.0-alpha.9",
75+
"@sentry/opentelemetry": "8.0.0-alpha.9",
7476
"@sentry/vercel-edge": "8.0.0-alpha.9",
7577
"@sentry/webpack-plugin": "2.16.0",
7678
"chalk": "3.0.0",
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { SpanKind } from '@opentelemetry/api';
2+
import { defineIntegration, spanToJSON } from '@sentry/core';
3+
import { getSpanKind, getSpanScopes } from '@sentry/opentelemetry';
4+
5+
/**
6+
* This integration is responsible for creating isolation scopes for incoming Http requests.
7+
* We do so by waiting for http spans to be created and then forking the isolation scope.
8+
*
9+
* Normally the isolation scopes would be created by our Http instrumentation, however Next.js brings it's own Http
10+
* instrumentation so we had to disable ours.
11+
*/
12+
export const requestIsolationScopeIntegration = defineIntegration(() => {
13+
return {
14+
name: 'RequestIsolationScope',
15+
setup(client) {
16+
client.on('spanStart', span => {
17+
const spanJson = spanToJSON(span);
18+
19+
// The following check is a heuristic to determine whether the started span is a span that tracks an incoming HTTP request
20+
if (getSpanKind(span) === SpanKind.SERVER && spanJson.data && 'http.method' in spanJson.data) {
21+
const scopes = getSpanScopes(span);
22+
if (scopes) {
23+
scopes.isolationScope = scopes.isolationScope.clone();
24+
}
25+
}
26+
});
27+
},
28+
};
29+
});

0 commit comments

Comments
 (0)