Skip to content

Commit 06cbe6e

Browse files
authored
chore(docs): upgrade sentry guide to use new global init and global hooks (#2799)
1 parent 3875bb2 commit 06cbe6e

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

docs/guides/examples/sentry-error-tracking.mdx

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ Automatically send errors and source maps to your Sentry project from your Trigg
1313
- A [Sentry](https://sentry.io) account and project
1414
- A [Trigger.dev](https://trigger.dev) account and project
1515

16-
## Build configuration
16+
## Setup
1717

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.
18+
This setup involves two files:
19+
20+
1. **`trigger.config.ts`** - Configures the build to upload source maps to Sentry during deployment
21+
2. **`trigger/init.ts`** - Initializes Sentry and registers the error tracking hook at runtime
1922

2023
<Note>
2124
You will need to set the `SENTRY_AUTH_TOKEN` and `SENTRY_DSN` environment variables. You can find
@@ -25,11 +28,14 @@ To send errors to Sentry when there are errors in your tasks, you'll need to add
2528
dashboard](https://cloud.trigger.dev), under environment variables in your project's sidebar.
2629
</Note>
2730

31+
### Build configuration
32+
33+
Add this build configuration to your `trigger.config.ts` file. This uses the Sentry esbuild plugin to upload source maps every time you deploy your project.
34+
2835
```ts trigger.config.ts
2936
import { defineConfig } from "@trigger.dev/sdk";
3037
import { esbuildPlugin } from "@trigger.dev/build/extensions";
3138
import { sentryEsbuildPlugin } from "@sentry/esbuild-plugin";
32-
import * as Sentry from "@sentry/node";
3339

3440
export default defineConfig({
3541
project: "<project ref>",
@@ -47,23 +53,6 @@ export default defineConfig({
4753
),
4854
],
4955
},
50-
init: async () => {
51-
Sentry.init({
52-
defaultIntegrations: false,
53-
// The Data Source Name (DSN) is a unique identifier for your Sentry project.
54-
dsn: process.env.SENTRY_DSN,
55-
// Update this to match the environment you want to track errors for
56-
environment: process.env.NODE_ENV === "production" ? "production" : "development",
57-
});
58-
},
59-
onFailure: async ({ payload, error, ctx }) => {
60-
Sentry.captureException(error, {
61-
extra: {
62-
payload,
63-
ctx,
64-
},
65-
});
66-
},
6756
});
6857
```
6958

@@ -73,6 +62,39 @@ export default defineConfig({
7362
deploying). You can use pre-built extensions or create your own.
7463
</Note>
7564

65+
### Runtime initialization
66+
67+
Create a `trigger/init.ts` file to initialize Sentry and register the global `onFailure` hook. This file is automatically loaded when your tasks execute.
68+
69+
```ts trigger/init.ts
70+
import { tasks } from "@trigger.dev/sdk";
71+
import * as Sentry from "@sentry/node";
72+
73+
// Initialize Sentry
74+
Sentry.init({
75+
defaultIntegrations: false,
76+
// The Data Source Name (DSN) is a unique identifier for your Sentry project.
77+
dsn: process.env.SENTRY_DSN,
78+
// Update this to match the environment you want to track errors for
79+
environment: process.env.NODE_ENV === "production" ? "production" : "development",
80+
});
81+
82+
// Register a global onFailure hook to capture errors
83+
tasks.onFailure(({ payload, error, ctx }) => {
84+
Sentry.captureException(error, {
85+
extra: {
86+
payload,
87+
ctx,
88+
},
89+
});
90+
});
91+
```
92+
93+
<Note>
94+
Learn more about [global lifecycle hooks](/tasks/overview#global-lifecycle-hooks) and the
95+
[`init.ts` file](/tasks/overview#init-ts).
96+
</Note>
97+
7698
## Testing that errors are being sent to Sentry
7799

78100
To test that errors are being sent to Sentry, you need to create a task that will fail.

0 commit comments

Comments
 (0)