关于 monitoring
面向运维团队的 Vercel 应用监控技能,涵盖日志检查、分析指标、健康检查、工作流错误及 Cron 执行记录。
主要功能
- 01检查 Vercel 函数、构建和 Edge 日志
- 02跟踪 Web Vitals 与 Speed Insights
- 03测试 /api/health 端点
- 04排查工作流重试与失败
- 05监控定时 Cron 执行
适用场景
- 01调查生产部署中的错误
- 02检查应用健康状态与性能
- 03诊断工作流或 Cron 作业失败
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ motormetrics/ motormetrics --skill monitoring安装后在 Claude Code 中输入/monitoring确认已加载。
详细介绍
监控技能
监控 Vercel 部署、检查日志并跟踪错误。
查看日志
Vercel Dashboard
- 前往 Vercel Dashboard → Project → Logs
- 按以下条件筛选:
- 函数日志
- 构建日志
- Edge 函数日志
- 时间范围
Vercel CLI
# View recent logs
vercel logs <deployment-url>
# Stream logs in real-time
vercel logs <deployment-url> --follow
# Filter by status
vercel logs <deployment-url> --status error
Vercel Analytics
Web Vitals
在 Vercel Dashboard → Analytics 中监控:
- LCP(最大内容绘制)
- FID(首次输入延迟)
- CLS(累积布局偏移)
- TTFB(首字节时间)
Speed Insights
已集成到应用中:
// apps/web/src/app/layout.tsx
import { Analytics } from "@vercel/analytics/next";
import { SpeedInsights } from "@vercel/speed-insights/next";
const RootLayout = ({ children }) => (
<html>
<body>
{children}
<Analytics />
<SpeedInsights />
</body>
</html>
);
日志记录模式
代码库使用标准的 console.log/error/warn 进行日志记录:
// Workflow logging pattern
console.log("[WORKFLOW] Posts cache invalidated");
console.log("[COE]", coeResult);
// Error logging pattern
console.error("Error getting post view count:", error);
console.error("Download failed:", { url, status });
使用的日志前缀:
[WORKFLOW]- 工作流执行事件[COE]- COE 数据处理[CARS]- 汽车数据处理
健康检查
应用在 /api/health 提供健康检查端点:
// apps/web/src/app/api/health/route.ts
export const GET = () => {
return NextResponse.json(
{ status: "ok", timestamp: new Date().toISOString() },
{
status: 200,
headers: {
"Cache-Control": "public, s-maxage=60, stale-while-revalidate=300",
},
},
);
};
测试健康检查端点:
curl -f https://sgcarstrends.com/api/health || echo "Web unhealthy"
工作流错误处理
工作流使用 Vercel WDK 的内置错误处理:
// apps/web/src/workflows/cars/index.ts
import { FatalError, RetryableError } from "workflow";
// Retryable errors (will be retried automatically)
if (message.includes("429")) {
throw new RetryableError("AI rate limited", { retryAfter: "1m" });
}
// Fatal errors (will not be retried)
if (message.includes("401") || message.includes("403")) {
throw new FatalError("AI authentication failed");
}
错误类型:
RetryableError- 临时故障(速率限制、网络问题)FatalError- 永久故障(身份验证错误、配置无效)
调试生产环境问题
1. 检查 Vercel 日志
# Get deployment URL
vercel ls
# Check logs
vercel logs <url> --follow
2. 检查构建输出
- Vercel Dashboard → Deployments
- 点击部署
- 查看“Build Logs”
3. 测试端点
curl -v https://sgcarstrends.com/api/health
4. 检查函数运行时
Vercel Dashboard → Project → Settings → Functions
- 查看运行时版本
- 检查内存分配
- 查看超时设置
常见问题
| 问题 | 调查方法 | 解决方案 |
|---|---|---|
| 延迟过高 | 检查 Vercel Analytics 和慢查询 | 优化查询、添加缓存 |
| 构建失败 | 检查构建日志 | 修复构建错误、更新依赖项 |
| 函数超时 | 检查函数日志 | 优化代码、增加超时时间 |
| 冷启动 | 检查函数调用 | 考虑使用 Edge 函数 |
| 工作流失败 | 在 Vercel 日志中按工作流 ID 检查 | 查看错误类型、检查重试情况 |
Vercel 函数配置
// API route config
export const config = {
maxDuration: 60, // seconds (Pro plan allows up to 60s)
};
Cron 作业监控
工作流由 Vercel Cron 触发:
// apps/web/vercel.json
{
"crons": [
{ "path": "/api/workflows/cars", "schedule": "0 10 * * *" },
{ "path": "/api/workflows/coe", "schedule": "0 10 * * *" },
{ "path": "/api/workflows/deregistrations", "schedule": "0 10 * * *" }
]
}
监控 Cron 执行情况:
- Vercel Dashboard → Project → Cron Jobs
- 查看执行历史和状态
- 检查每次执行的日志
最佳实践
- 使用日志前缀:添加
[CONTEXT]前缀,便于筛选 - 记录有意义的数据:包含相关上下文(月、记录数)
- 不要记录机密信息:绝不记录密码、令牌、API 密钥
- 使用 Vercel Analytics:监控性能指标
- 检查 Cron 作业:监控计划的工作流执行情况
参考资料
- Vercel 日志:https://vercel.com/docs/observability/runtime-logs
- Vercel Analytics:https://vercel.com/docs/analytics
- Speed Insights:https://vercel.com/docs/speed-insights
- Vercel Cron Jobs:https://vercel.com/docs/cron-jobs
评论
支持 Markdown