Skip to content

Commit 83d59df

Browse files
feat(web): enable Sentry node and browser profiling
Adds @sentry/profiling-node and wires up profiling on both the server (nodeProfilingIntegration) and the browser (browserProfilingIntegration). Both use profileLifecycle: 'trace', so a profiler only runs while a sampled root span is active rather than continuously. Browser profiling additionally requires the document to opt into the JS Self-Profiling API, so a `Document-Policy: js-profiling` response header is added alongside the existing security headers. @sentry/profiling-node is pinned to ^10.40.0 to match @sentry/nextjs. It depends on @sentry/core and @sentry/node at exact versions, so a newer minor would pull a second copy of the SDK into the tree and the profiler would register against a different global. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 139b238 commit 83d59df

5 files changed

Lines changed: 24 additions & 8 deletions

File tree

packages/web/next.config.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ const nextConfig = {
104104
key: "Content-Security-Policy",
105105
value: "frame-ancestors 'self'",
106106
},
107+
// Opts the document into the JS Self-Profiling API, which Sentry's
108+
// browser profiling integration needs in order to start a profiler.
109+
// @see: https://docs.sentry.io/platforms/javascript/guides/nextjs/profiling/browser-profiling/
110+
{
111+
key: "Document-Policy",
112+
value: "js-profiling",
113+
},
107114
],
108115
},
109116
];

packages/web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
"@replit/codemirror-lang-svelte": "^6.0.0",
103103
"@replit/codemirror-vim": "^6.2.1",
104104
"@sentry/nextjs": "^10.40.0",
105+
"@sentry/profiling-node": "^10.40.0",
105106
"@shopify/lang-jsonc": "^1.0.0",
106107
"@sourcebot/codemirror-lang-tcl": "^1.0.13",
107108
"@sourcebot/db": "workspace:*",

packages/web/src/instrumentation-client.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ if (!!process.env.NEXT_PUBLIC_SENTRY_WEBAPP_DSN && !!process.env.NEXT_PUBLIC_SEN
1212
Sentry.init({
1313
dsn: process.env.NEXT_PUBLIC_SENTRY_WEBAPP_DSN,
1414
environment: process.env.NEXT_PUBLIC_SENTRY_ENVIRONMENT,
15-
15+
integrations: [
16+
Sentry.browserProfilingIntegration(),
17+
],
1618
tracesSampleRate: 1.0,
17-
18-
// Setting this option to true will print useful information to the console while you're setting up Sentry.
19-
debug: false,
19+
// Evaluated once per `Sentry.init()`, i.e. once per page load.
20+
profileSessionSampleRate: 1.0,
21+
// Profile only while a sampled root span is active, rather than continuously.
22+
profileLifecycle: 'trace',
2023
});
2124
} else {
2225
console.debug("[client] Sentry was not initialized");

packages/web/src/sentry.server.config.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
44

55
import * as Sentry from "@sentry/nextjs";
6+
import { nodeProfilingIntegration } from "@sentry/profiling-node";
67
import { createLogger } from "@sourcebot/shared";
78

89
const logger = createLogger('sentry-server-config');
@@ -11,11 +12,14 @@ if (!!process.env.NEXT_PUBLIC_SENTRY_WEBAPP_DSN && !!process.env.NEXT_PUBLIC_SEN
1112
Sentry.init({
1213
dsn: process.env.NEXT_PUBLIC_SENTRY_WEBAPP_DSN,
1314
environment: process.env.NEXT_PUBLIC_SENTRY_ENVIRONMENT,
14-
15+
integrations: [
16+
nodeProfilingIntegration(),
17+
],
1518
tracesSampleRate: 1.0,
16-
17-
// Setting this option to true will print useful information to the console while you're setting up Sentry.
18-
debug: false,
19+
// Evaluated once per `Sentry.init()`, i.e. once per server process.
20+
profileSessionSampleRate: 1.0,
21+
// Profile only while a sampled root span is active, rather than continuously.
22+
profileLifecycle: 'trace',
1923
});
2024
} else {
2125
logger.debug("[server] Sentry was not initialized");

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9380,6 +9380,7 @@ __metadata:
93809380
"@replit/codemirror-lang-svelte": "npm:^6.0.0"
93819381
"@replit/codemirror-vim": "npm:^6.2.1"
93829382
"@sentry/nextjs": "npm:^10.40.0"
9383+
"@sentry/profiling-node": "npm:^10.40.0"
93839384
"@shopify/lang-jsonc": "npm:^1.0.0"
93849385
"@sourcebot/codemirror-lang-tcl": "npm:^1.0.13"
93859386
"@sourcebot/db": "workspace:*"

0 commit comments

Comments
 (0)