Edge Functions

Monitoring with Sentry

Add the Sentry Deno SDK to your Supabase Edge Functions to easily track exceptions and get notified of errors or performance issues.

Prerequisites

1. Create Supabase function

Create a new function locally:


_10
supabase functions new sentryfied

2. Add the Sentry Deno SDK

Handle exceptions within your function and send them to Sentry.


_33
import * as Sentry from 'https://deno.land/x/sentry/index.mjs'
_33
_33
Sentry.init({
_33
// https://docs.sentry.io/product/sentry-basics/concepts/dsn-explainer/#where-to-find-your-dsn
_33
dsn: SENTRY_DSN,
_33
defaultIntegrations: false,
_33
// Performance Monitoring
_33
tracesSampleRate: 1.0,
_33
// Set sampling rate for profiling - this is relative to tracesSampleRate
_33
profilesSampleRate: 1.0,
_33
})
_33
_33
// Set region and execution_id as custom tags
_33
Sentry.setTag('region', Deno.env.get('SB_REGION'))
_33
Sentry.setTag('execution_id', Deno.env.get('SB_EXECUTION_ID'))
_33
_33
Deno.serve(async (req) => {
_33
try {
_33
const { name } = await req.json()
_33
// This will throw, as `name` in our example call will be `undefined`
_33
const data = {
_33
message: `Hello ${name}!`,
_33
}
_33
_33
return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json' } })
_33
} catch (e) {
_33
Sentry.captureException(e)
_33
return new Response(JSON.stringify({ msg: 'error' }), {
_33
status: 500,
_33
headers: { 'Content-Type': 'application/json' },
_33
})
_33
}
_33
})

3. Deploy and test

Run function locally:


_10
supabase start
_10
supabase functions serve --no-verify-jwt

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

Deploy function to Supabase:


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

4. Try it yourself

Find the complete example on GitHub.

Working With Scopes

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.