Skip to content
Edge Functions

使用 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

1. 创建 Supabase 函数 #

🌐 1. Create Supabase function

在本地创建一个新函数:

🌐 Create a new function locally:

1
supabase functions new sentryfied

2. 添加 Sentry Deno SDK #

🌐 2. Add the Sentry Deno SDK

在你的函数中处理异常并发送到 Sentry。

🌐 Handle exceptions within your function and send them to Sentry.

1
import * as Sentry from 'npm:@sentry/deno@^8'
2
import { withSupabase } from 'npm:@supabase/server@^1'
3
4
Sentry.init({
5
// https://docs.sentry.io/product/sentry-basics/concepts/dsn-explainer/#where-to-find-your-dsn
6
dsn: Deno.env.get('SENTRY_DSN'),
7
defaultIntegrations: false,
8
// Performance Monitoring
9
tracesSampleRate: 1.0,
10
// Set sampling rate for profiling - this is relative to tracesSampleRate
11
profilesSampleRate: 1.0,
12
})
13
14
// Set region and execution_id as custom tags
15
Sentry.setTag('region', Deno.env.get('SB_REGION'))
16
Sentry.setTag('execution_id', Deno.env.get('SB_EXECUTION_ID'))
17
18
// Open endpoint for testing. In production, implement an authorization layer in the handler or switch the auth mode.
19
export 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
}
27
28
return Response.json(data)
29
} catch (e) {
30
Sentry.captureException(e)
31
// Flush Sentry before the running process closes
32
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:

1
supabase start
2
supabase functions serve --no-verify-jwt

测试一下:http://localhost:54321/functions/v1/sentryfied

🌐 Test it: http://localhost:54321/functions/v1/sentryfied

将函数部署到 Supabase:

🌐 Deploy function to Supabase:

1
supabase functions deploy sentryfied --no-verify-jwt

4. 你自己试试 #

🌐 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 调用,或者直接传递上下文captureExceptioncaptureMessage 调用。

🌐 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.