使用 Sentry 进行监控
将 Sentry Deno SDK 添加到你的 Supabase Edge Functions 中,以跟踪异常并在出现错误或性能问题时收到通知。
🌐 Add the Sentry Deno SDK to your Supabase Edge Functions to track exceptions and get notified of errors or performance issues.
先决条件 #
🌐 Prerequisites
- 创建一个 Sentry 账户。
- 确保你已经安装了最新版本的 Supabase CLI。
1. 创建 Supabase 函数 #
🌐 1. Create Supabase function
在本地创建一个新函数:
🌐 Create a new function locally:
1supabase functions new sentryfied2. 添加 Sentry Deno SDK #
🌐 2. Add the Sentry Deno SDK
在你的函数中处理异常并发送到 Sentry。
🌐 Handle exceptions within your function and send them to Sentry.
1import * as Sentry from 'npm:@sentry/deno@^8'2import { withSupabase } from 'npm:@supabase/server@^1'34Sentry.init({5 // https://docs.sentry.io/product/sentry-basics/concepts/dsn-explainer/#where-to-find-your-dsn6 dsn: Deno.env.get('SENTRY_DSN'),7 defaultIntegrations: false,8 // Performance Monitoring9 tracesSampleRate: 1.0,10 // Set sampling rate for profiling - this is relative to tracesSampleRate11 profilesSampleRate: 1.0,12})1314// Set region and execution_id as custom tags15Sentry.setTag('region', Deno.env.get('SB_REGION'))16Sentry.setTag('execution_id', Deno.env.get('SB_EXECUTION_ID'))1718// Open endpoint for testing. In production, implement an authorization layer in the handler or switch the auth mode.19export default {20 fetch: withSupabase({ auth: 'none' }, async (req, ctx) => {21 try {22 const { name } = await req.json()23 // This will throw, as `name` in our example call will be `undefined`24 const data = {25 message: `Hello ${name}!`,26 }2728 return Response.json(data)29 } catch (e) {30 Sentry.captureException(e)31 // Flush Sentry before the running process closes32 await Sentry.flush(2000)33 return Response.json({ error: 'Internal Server Error' }, { status: 500 })34 }35 }),36}3. 部署和测试 #
🌐 3. Deploy and test
在本地运行函数:
🌐 Run function locally:
1supabase start2supabase functions serve --no-verify-jwt测试一下:http://localhost:54321/functions/v1/sentryfied
🌐 Test it: http://localhost:54321/functions/v1/sentryfied
将函数部署到 Supabase:
🌐 Deploy function to Supabase:
1supabase functions deploy sentryfied --no-verify-jwt4. 你自己试试 #
🌐 4. Try it yourself
在 GitHub 上查看完整示例。
🌐 Find the complete example on GitHub.
使用作用域 #
🌐 Working with scopes
Sentry Deno SDK 目前不支持 Deno.serve 仪表,这意味着请求之间没有作用域隔离。因此,当 Edge Functions 运行时在多个请求之间重用时,所有全局捕获的面包屑和上下文数据都会被共享,这不是理想的行为。为了解决这个问题,上面示例代码中的所有默认集成都被禁用了,你应该依靠 withScope 来封装所有 Sentry SDK API 调用,或者直接传递上下文给 captureException 或 captureMessage 调用。
🌐 Sentry Deno SDK currently do not support Deno.serve instrumentation, which means that there is no scope separation between requests. Because of that, when the Edge Functions runtime is reused between multiple requests, all globally captured breadcrumbs and contextual data will be shared, which is not the desired behavior. To work around this, all default integrations in the example code above are disabled, and you should be relying on withScope to encapsulate all Sentry SDK API calls, or pass context directly to the captureException or captureMessage calls.