
# 

Monitoring with Sentry

* * *

Add the [Sentry Deno SDK](https://docs.sentry.io/platforms/javascript/guides/deno/) to your Supabase Edge Functions to track exceptions and get notified of errors or performance issues.

### Prerequisites[#](#prerequisites)

*   [Create a Sentry account](https://sentry.io/signup/).
*   Make sure you have the latest version of the [Supabase CLI](../../../cli/index.md#installation) installed.

### 1\. Create Supabase function[#](#1-create-supabase-function)

Create a new function locally:

```
1supabase functions new sentryfied
```

### 2\. Add the Sentry Deno SDK[#](#2-add-the-sentry-deno-sdk)

Handle exceptions within your function and send them to Sentry.

```
1import * as Sentry from 'https://deno.land/x/sentry/index.mjs'23Sentry.init({4  // https://docs.sentry.io/product/sentry-basics/concepts/dsn-explainer/#where-to-find-your-dsn5  dsn: SENTRY_DSN,6  defaultIntegrations: false,7  // Performance Monitoring8  tracesSampleRate: 1.0,9  // Set sampling rate for profiling - this is relative to tracesSampleRate10  profilesSampleRate: 1.0,11})1213// Set region and execution_id as custom tags14Sentry.setTag('region', Deno.env.get('SB_REGION'))15Sentry.setTag('execution_id', Deno.env.get('SB_EXECUTION_ID'))1617Deno.serve(async (req) => {18  try {19    const { name } = await req.json()20    // This will throw, as `name` in our example call will be `undefined`21    const data = {22      message: `Hello ${name}!`,23    }2425    return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json' } })26  } catch (e) {27    Sentry.captureException(e)28    // Flush Sentry before the running process closes29    await Sentry.flush(2000)30    return new Response(JSON.stringify({ msg: 'error' }), {31      status: 500,32      headers: { 'Content-Type': 'application/json' },33    })34  }35})
```

### 3\. Deploy and test[#](#3-deploy-and-test)

Run function locally:

```
1supabase start2supabase functions serve --no-verify-jwt
```

Test it: [http://localhost:54321/functions/v1/sentryfied](http://localhost:54321/functions/v1/sentryfied)

Deploy function to Supabase:

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

### 4\. Try it yourself[#](#4-try-it-yourself)

Find the complete example on [GitHub](https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions/sentryfied/index.ts).

## Working with scopes[#](#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`](https://docs.sentry.io/platforms/javascript/enriching-events/scopes/#using-withscope) to encapsulate all Sentry SDK API calls, or [pass context directly](https://docs.sentry.io/platforms/javascript/enriching-events/context/#passing-context-directly) to the `captureException` or `captureMessage` calls.
