# 监控、日志与可观测性 ## 目标 构建可追踪错误、性能瓶颈与用户关键路径的最小监控闭环。 ## Sentry(推荐) 1) 配置 `runtimeConfig.sentryDsn`; 2) 插件(示例,待引入依赖后启用): ```ts // plugins/sentry.client.ts export default defineNuxtPlugin(() => { const { public: pub, sentryDsn } = useRuntimeConfig() if (!sentryDsn) return // @ts-ignore: add SDK in deps later const Sentry = (window as any).Sentry if (Sentry) { Sentry.init({ dsn: sentryDsn, tracesSampleRate: 0.1 }) } }) ``` ## 日志与错误边界 - 服务器:使用 Nitro 默认日志(生产应接入集中式日志,如 CloudWatch/ELK)。 - 客户端:全局错误捕获(`app:error` 事件)上报到 Sentry/自建网关。 ## Web Vitals(可选) 收集 LCP/CLS/INP 等指标(示例): ```ts // plugins/web-vitals.client.ts import { onCLS, onINP, onLCP } from 'web-vitals' export default defineNuxtPlugin(() => { const send = (metric: any) => navigator.sendBeacon('/vitals', JSON.stringify(metric)) onLCP(send); onCLS(send); onINP(send) }) ``` ## Dashboard 路线 - 第一步:仅错误计数与关键事务。 - 第二步:接口慢查询、资源错误分布、版本追踪(commit SHA)。