Skip to content

Commit cbe5170

Browse files
authored
Added Sentry example (#1435)
* Added sentry docs * Removed additionalPackages * Updated description * Copy updates
1 parent 3d7a6d8 commit cbe5170

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
title: "Track errors with Sentry"
3+
sidebarTitle: "Sentry error tracking"
4+
description: "This example demonstrates how to track errors with Sentry using Trigger.dev."
5+
---
6+
7+
## Overview
8+
9+
Automatically send errors and source maps to your Sentry project from your Trigger.dev tasks. Sending source maps to Sentry allows for more detailed stack traces when errors occur, as Sentry can map the minified code back to the original source code.
10+
11+
## Prerequisites
12+
13+
- A [Sentry](https://sentry.io) account and project
14+
- A [Trigger.dev](https://trigger.dev) account and project
15+
16+
## Build configuration
17+
18+
To send errors to Sentry when there are errors in your tasks, you'll need to add this build configuration to your `trigger.config.ts` file. This will then run every time you deploy your project.
19+
20+
<Note>
21+
You will need to set the `SENTRY_AUTH_TOKEN` and `SENTRY_DSN` environment variables. You can find
22+
the `SENTRY_AUTH_TOKEN` in your Sentry dashboard, in settings -> developer settings -> auth tokens
23+
and the `SENTRY_DSN` in your Sentry dashboard, in settings -> projects -> your project -> client
24+
keys (DSN). Add these to your `.env` file, and in your [Trigger.dev
25+
dashboard](https://cloud.trigger.dev), under environment variables in your project's sidebar.
26+
</Note>
27+
28+
```ts trigger.config.ts
29+
import { defineConfig } from "@trigger.dev/sdk/v3";
30+
import { esbuildPlugin } from "@trigger.dev/build/extensions";
31+
import { sentryEsbuildPlugin } from "@sentry/esbuild-plugin";
32+
import * as Sentry from "@sentry/node";
33+
34+
export default defineConfig({
35+
project: "<project ref>",
36+
// Your other config settings...
37+
build: {
38+
extensions: [
39+
esbuildPlugin(
40+
sentryEsbuildPlugin({
41+
org: "<your-sentry-org>",
42+
project: "<your-sentry-project>",
43+
// Find this auth token in settings -> developer settings -> auth tokens
44+
authToken: process.env.SENTRY_AUTH_TOKEN,
45+
}),
46+
{ placement: "last", target: "deploy" }
47+
),
48+
],
49+
},
50+
init: async () => {
51+
Sentry.init({
52+
// The Data Source Name (DSN) is a unique identifier for your Sentry project.
53+
dsn: process.env.SENTRY_DSN,
54+
// Update this to match the environment you want to track errors for
55+
environment: process.env.NODE_ENV === "production" ? "production" : "development",
56+
});
57+
},
58+
onFailure: async (payload, error, { ctx }) => {
59+
Sentry.captureException(error, {
60+
extra: {
61+
payload,
62+
ctx,
63+
},
64+
});
65+
},
66+
});
67+
```
68+
69+
<Note>
70+
[Build extensions](/config/config-file#extensions) allow you to hook into the build system and
71+
customize the build process or the resulting bundle and container image (in the case of
72+
deploying). You can use pre-built extensions or create your own.
73+
</Note>
74+
75+
## Testing that errors are being sent to Sentry
76+
77+
To test that errors are being sent to Sentry, you need to create a task that will fail.
78+
79+
This task takes no payload, and will throw an error.
80+
81+
```ts trigger/sentry-error-test.ts
82+
import { task } from "@trigger.dev/sdk/v3";
83+
84+
export const sentryErrorTest = task({
85+
id: "sentry-error-test",
86+
retry: {
87+
// Only retry once
88+
maxAttempts: 1,
89+
},
90+
run: async () => {
91+
const error = new Error("This is a custom error that Sentry will capture");
92+
error.cause = { additionalContext: "This is additional context" };
93+
throw error;
94+
},
95+
});
96+
```
97+
98+
After creating the task, deploy your project.
99+
100+
<CodeGroup>
101+
102+
```bash npm
103+
npx trigger.dev@latest deploy
104+
```
105+
106+
```bash pnpm
107+
pnpm dlx trigger.dev@latest deploy
108+
```
109+
110+
```bash yarn
111+
yarn dlx trigger.dev@latest deploy
112+
```
113+
114+
</CodeGroup>
115+
116+
Once deployed, navigate to the `test` page in the sidebar of your [Trigger.dev dashboard](https://cloud.trigger.dev), click on your `prod` environment, and select the `sentryErrorTest` task.
117+
118+
Run a test task with an empty payload by clicking the `Run test` button.
119+
120+
Your run should then fail, and if everything is set up correctly, you will see an error in the Sentry project dashboard shortly after.

docs/guides/introduction.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Tasks you can copy and paste to get started with Trigger.dev. They can all be ex
4747
| [React to PDF](/guides/examples/react-pdf) | Use `react-pdf` to generate a PDF and save it to Cloudflare R2. |
4848
| [Puppeteer](/guides/examples/puppeteer) | Use Puppeteer to generate a PDF or scrape a webpage. |
4949
| [Resend email sequence](/guides/examples/resend-email-sequence) | Send a sequence of emails over several days using Resend with Trigger.dev. |
50+
| [Sentry error tracking](/guides/examples/sentry-error-tracking) | Automatically send errors to Sentry from your tasks. |
5051
| [Sharp image processing](/guides/examples/sharp-image-processing) | Use Sharp to process an image and save it to Cloudflare R2. |
5152
| [Supabase database operations](/guides/examples/supabase-database-operations) | Run basic CRUD operations on a table in a Supabase database using Trigger.dev. |
5253
| [Supabase Storage upload](/guides/examples/supabase-storage-upload) | Download a video from a URL and upload it to Supabase Storage using S3. |

docs/mint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@
311311
"guides/examples/pdf-to-image",
312312
"guides/examples/puppeteer",
313313
"guides/examples/scrape-hacker-news",
314+
"guides/examples/sentry-error-tracking",
314315
"guides/examples/sharp-image-processing",
315316
"guides/examples/supabase-database-operations",
316317
"guides/examples/supabase-storage-upload",

0 commit comments

Comments
 (0)