Skip to content

Monitoring Edge Function resource usage

了解如何跟踪你的 Edge Function 性能并找出潜在的资源问题。

🌐 Learn how to track your Edge Function's performance and identify potential resource issues.

正在查看指标 #

🌐 Accessing metrics

通过仪表板追踪你功能的表现:

🌐 Track your function's performance through the dashboard:

  1. 导航到 Edge Functions > [你的函数] > 指标
  2. 查看 CPU、内存和执行时间图表
  3. 找出资源消耗中可能出现的问题

关键指标监控 #

🌐 Key metrics to monitor

CPU 使用率 #

🌐 CPU usage

高 CPU 使用率表示你的函数正在进行高强度计算。可以考虑:

🌐 High CPU usage indicates your function is performing intensive computations. Consider:

  • 优化算法
  • 缓存计算结果
  • 将繁重的处理移到后台任务

内存使用 #

🌐 Memory usage

内存峰值可能会导致功能故障。注意:

🌐 Memory spikes can cause function failures. Watch for:

  • 大量数据集已加载到内存中
  • 未被回收的累积对象
  • 大型依赖包

执行时间 #

🌐 Execution time

长时间的执行可能会导致超时。注意监控以下内容:

🌐 Long execution times can lead to timeouts. Monitor for:

  • 外部 API 调用慢
  • 低效的数据库查询
  • 复杂的计算操作

资源限制 #

🌐 Resource limits

与传统服务器相比,边缘函数的资源有限。优化重点:

🌐 Edge Functions have limited resources compared to traditional servers. Optimize for:

  • 内存效率: 避免将大型数据集加载到内存中
  • CPU优化: 尽量减少复杂计算
  • 执行时间: 让函数保持在60秒以内

最佳实践 #

🌐 Best practices

对大数据使用流式处理 #

🌐 Use streaming for large data

不要把整个文件加载到内存里,而是要流式传输数据:

🌐 Instead of loading entire files into memory, stream data:

1
// Stream responses instead of buffering
2
return new Response(readableStream, {
3
headers: { 'Content-Type': 'application/json' },
4
})

分块处理数据 #

🌐 Process data in chunks

把大操作分成更小的批次:

🌐 Break large operations into smaller batches:

1
for (const batch of dataBatches) {
2
await processBatch(batch)
3
}

缓存高开销操作 #

🌐 Cache expensive operations

存储昂贵计算的结果以避免重复计算:

🌐 Store results of expensive computations to avoid recalculating:

1
const cachedResult = await cache.get(key)
2
if (cachedResult) {
3
return cachedResult
4
}

额外资源 #

🌐 Additional resources