Skip to content

Commit dccce6b

Browse files
refactor(sentry): DSN 未配置时不启用 SDK,避免初始化告警 + 清服务器工作树
动机:另一个 subagent 做了一半的改动(见 issue #302 P1-2 的 Sentry PII 守门相关讨论衍生)。留在服务器工作树脏了,导致 sync-uuid workflow 的 "工作树检查"失败(见 workflow run 24600229464): Error: 服务器 frontend 工作树不干净,拒绝 reset M sentry.{client,edge,server}.config.ts 这份改动本身是独立可完成的小优化,不涉及其他业务逻辑,故单独提交 (不混进正在进行的 docs 重组 PR)。 具体改动: - 每个 runtime config 顶部提取 dsn const - enabled 条件从 NODE_ENV==="production" 变成 NODE_ENV==="production" && !!dsn - 注释同步说明 "DSN 未配置时不启用" - Copilot CR 提到:避免漏配 env 时 SDK 初始化打 "No DSN provided" 告警日志 效果: - 本地 / Vercel preview 环境无 DSN 配置时 Sentry SDK 彻底不启用,零告警 - 生产 + DSN 配齐时行为不变(和之前一致) - 服务器 frontend 工作树干净,下次 sync-uuid workflow 能正常跑
1 parent 02d3c87 commit dccce6b

3 files changed

Lines changed: 18 additions & 10 deletions

File tree

sentry.client.config.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
* Sentry 浏览器端初始化。
33
*
44
* 免费 tier 策略(Developer plan,5K errors/月 + 10K perf units/月):
5-
* - 只在 production 启用,避免本地 dev/preview 污染配额
5+
* - production 构建 + DSN 已配置才启用,避免本地 dev 污染配额 / DSN 漏配告警
66
* - tracesSampleRate 0.1:10% 的页面 transaction 采样足够看性能趋势
77
* - 关闭 Session Replay:它是另外的独立配额(小),开了容易炸
88
* - 不启用 profiling(需要付费)
99
*/
1010
import * as Sentry from "@sentry/nextjs";
1111

12+
const dsn = process.env.NEXT_PUBLIC_SENTRY_DSN;
13+
1214
Sentry.init({
13-
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
14-
enabled: process.env.NODE_ENV === "production",
15+
dsn,
16+
enabled: process.env.NODE_ENV === "production" && !!dsn,
1517
tracesSampleRate: 0.1,
1618
replaysSessionSampleRate: 0,
1719
replaysOnErrorSampleRate: 0,

sentry.edge.config.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
/**
2-
* Sentry Edge runtime 初始化(Middleware / Edge routes)。
3-
* 当前项目未使用 edge runtime,保留配置以便未来迁移时无需补齐。
2+
* Sentry Edge runtime 初始化(middleware 及未来可能出现的 Edge routes)。
3+
* 当前仓库根目录的 middleware.ts 即在此 runtime 执行(IP geo → locale cookie)。
4+
* 启用条件:production 构建 + DSN 已配置,避免 DSN 漏配时 SDK 启动时打告警。
45
*/
56
import * as Sentry from "@sentry/nextjs";
67

8+
const dsn = process.env.NEXT_PUBLIC_SENTRY_DSN;
9+
710
Sentry.init({
8-
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
9-
enabled: process.env.NODE_ENV === "production",
11+
dsn,
12+
enabled: process.env.NODE_ENV === "production" && !!dsn,
1013
tracesSampleRate: 0.1,
1114
debug: false,
1215
});

sentry.server.config.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
/**
22
* Sentry Node.js runtime 初始化(Next.js API routes / Server Components / RSC)。
3-
* 与 client 同策略:仅 production 启用,traces 10%,无 replay/profiling。
3+
* 与 client 同策略:production 且 DSN 已配置时启用,traces 10%,无 replay/profiling。
4+
* 加 DSN 校验是为了避免漏配 env 时 SDK 初始化打告警日志(Copilot CR)。
45
*/
56
import * as Sentry from "@sentry/nextjs";
67

8+
const dsn = process.env.NEXT_PUBLIC_SENTRY_DSN;
9+
710
Sentry.init({
8-
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
9-
enabled: process.env.NODE_ENV === "production",
11+
dsn,
12+
enabled: process.env.NODE_ENV === "production" && !!dsn,
1013
tracesSampleRate: 0.1,
1114
debug: false,
1215
});

0 commit comments

Comments
 (0)