|
| 1 | +--- |
| 2 | +title: Elysia |
| 3 | +description: Learn how to set up Sentry in your Elysia app to capture errors and monitor performance. |
| 4 | +sdk: sentry.javascript.elysia |
| 5 | +fallbackGuide: javascript.bun |
| 6 | +categories: |
| 7 | + - javascript |
| 8 | + - server |
| 9 | + - server-node |
| 10 | +--- |
| 11 | + |
| 12 | +<Alert> |
| 13 | + |
| 14 | +This SDK is currently in **ALPHA**. Alpha features are still in progress, may have bugs, and might include breaking changes. |
| 15 | +Please reach out on [GitHub](https://github.com/getsentry/sentry-javascript/issues/new/choose) if you have any feedback or concerns. |
| 16 | + |
| 17 | +</Alert> |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +You need: |
| 22 | + |
| 23 | +- A Sentry [account](https://sentry.io/signup/) and [project](/product/projects/) |
| 24 | +- An Elysia application (v1.4.0+) |
| 25 | +- Bun or Node.js 18+ (with [@elysiajs/node](https://elysiajs.com/integrations/node) adapter) |
| 26 | + |
| 27 | +## Step 1: Install |
| 28 | + |
| 29 | +Choose the features you want to configure, and this guide will show you how: |
| 30 | + |
| 31 | +<OnboardingOptionButtons |
| 32 | + options={["error-monitoring", "performance", "logs"]} |
| 33 | +/> |
| 34 | + |
| 35 | +<Include name="quick-start-features-expandable" /> |
| 36 | + |
| 37 | +### Install the Sentry SDK |
| 38 | + |
| 39 | +Run the command for your preferred package manager to add the Sentry SDK to your application: |
| 40 | + |
| 41 | +```bash {tabTitle:Bun} |
| 42 | +bun add @sentry/elysia |
| 43 | +``` |
| 44 | + |
| 45 | +```bash {tabTitle:Node.js} |
| 46 | +npm install @sentry/elysia @elysiajs/node |
| 47 | +# or |
| 48 | +yarn add @sentry/elysia @elysiajs/node |
| 49 | +# or |
| 50 | +pnpm add @sentry/elysia @elysiajs/node |
| 51 | +``` |
| 52 | + |
| 53 | +<Alert> |
| 54 | + You do **not** need `@elysiajs/opentelemetry`. The `@sentry/elysia` SDK handles all instrumentation natively. |
| 55 | +</Alert> |
| 56 | + |
| 57 | +## Step 2: Configure |
| 58 | + |
| 59 | +Call `Sentry.init()` before creating your Elysia app, then wrap the app with `Sentry.withElysia()` before defining routes. |
| 60 | + |
| 61 | +```javascript {tabTitle:Bun} {filename: index.ts} |
| 62 | +import * as Sentry from "@sentry/elysia"; |
| 63 | +import { Elysia } from "elysia"; |
| 64 | + |
| 65 | +Sentry.init({ |
| 66 | + dsn: "___PUBLIC_DSN___", |
| 67 | + // Adds request headers and IP for users, for more info visit: |
| 68 | + // https://docs.sentry.io/platforms/javascript/guides/elysia/configuration/options/#sendDefaultPii |
| 69 | + sendDefaultPii: true, |
| 70 | + // ___PRODUCT_OPTION_START___ performance |
| 71 | + |
| 72 | + // Set tracesSampleRate to 1.0 to capture 100% |
| 73 | + // of transactions for tracing. |
| 74 | + // We recommend adjusting this value in production |
| 75 | + // Learn more at |
| 76 | + // https://docs.sentry.io/platforms/javascript/guides/elysia/configuration/options/#tracesSampleRate |
| 77 | + tracesSampleRate: 1.0, |
| 78 | + // ___PRODUCT_OPTION_END___ performance |
| 79 | + // ___PRODUCT_OPTION_START___ logs |
| 80 | + |
| 81 | + // Enable logs to be sent to Sentry |
| 82 | + enableLogs: true, |
| 83 | + // ___PRODUCT_OPTION_END___ logs |
| 84 | +}); |
| 85 | + |
| 86 | +// withElysia returns the app instance, so you can chain routes directly |
| 87 | +const app = Sentry.withElysia(new Elysia()) |
| 88 | + .get("/", () => "Hello World") |
| 89 | + .listen(3000); |
| 90 | +``` |
| 91 | + |
| 92 | +```javascript {tabTitle:Node.js} {filename: index.ts} |
| 93 | +import * as Sentry from "@sentry/elysia"; |
| 94 | +import { Elysia } from "elysia"; |
| 95 | +import { node } from "@elysiajs/node"; |
| 96 | + |
| 97 | +Sentry.init({ |
| 98 | + dsn: "___PUBLIC_DSN___", |
| 99 | + // Adds request headers and IP for users, for more info visit: |
| 100 | + // https://docs.sentry.io/platforms/javascript/guides/elysia/configuration/options/#sendDefaultPii |
| 101 | + sendDefaultPii: true, |
| 102 | + // ___PRODUCT_OPTION_START___ performance |
| 103 | + |
| 104 | + // Set tracesSampleRate to 1.0 to capture 100% |
| 105 | + // of transactions for tracing. |
| 106 | + // We recommend adjusting this value in production |
| 107 | + // Learn more at |
| 108 | + // https://docs.sentry.io/platforms/javascript/guides/elysia/configuration/options/#tracesSampleRate |
| 109 | + tracesSampleRate: 1.0, |
| 110 | + // ___PRODUCT_OPTION_END___ performance |
| 111 | + // ___PRODUCT_OPTION_START___ logs |
| 112 | + |
| 113 | + // Enable logs to be sent to Sentry |
| 114 | + enableLogs: true, |
| 115 | + // ___PRODUCT_OPTION_END___ logs |
| 116 | +}); |
| 117 | + |
| 118 | +// withElysia returns the app instance, so you can chain routes directly |
| 119 | +const app = Sentry.withElysia(new Elysia({ adapter: node() })) |
| 120 | + .get("/", () => "Hello World") |
| 121 | + .listen(3000); |
| 122 | +``` |
| 123 | + |
| 124 | +## Step 3: Add Readable Stack Traces With Source Maps (Optional) |
| 125 | + |
| 126 | +<PlatformContent includePath="getting-started-sourcemaps-short-version" /> |
| 127 | + |
| 128 | +## Step 4: Verify Your Setup |
| 129 | + |
| 130 | +Let's test your setup and confirm that Sentry is working correctly and sending data to your Sentry project. |
| 131 | + |
| 132 | +### Issues |
| 133 | + |
| 134 | +First, let's verify that Sentry captures errors and creates issues in your Sentry project. Add the following route to your app, which triggers an error that Sentry will capture: |
| 135 | + |
| 136 | +```javascript |
| 137 | +app.get("/debug-sentry", () => { |
| 138 | + throw new Error("My first Sentry error!"); |
| 139 | +}); |
| 140 | +``` |
| 141 | + |
| 142 | +<OnboardingOption optionId="performance"> |
| 143 | + |
| 144 | +### Tracing |
| 145 | + |
| 146 | +To test your tracing configuration, update the previous code snippet by starting a trace to measure the time it takes for the execution of your code: |
| 147 | + |
| 148 | +```javascript |
| 149 | +app.get("/debug-sentry", async () => { |
| 150 | + await Sentry.startSpan( |
| 151 | + { |
| 152 | + op: "test", |
| 153 | + name: "My First Test Transaction", |
| 154 | + }, |
| 155 | + async () => { |
| 156 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 157 | + throw new Error("My first Sentry error!"); |
| 158 | + } |
| 159 | + ); |
| 160 | +}); |
| 161 | +``` |
| 162 | + |
| 163 | +</OnboardingOption> |
| 164 | + |
| 165 | +<OnboardingOption optionId="logs"> |
| 166 | + |
| 167 | +<Include name="logs/javascript-quick-start-verify-logs" /> |
| 168 | + |
| 169 | +</OnboardingOption> |
| 170 | + |
| 171 | +### View Captured Data in Sentry |
| 172 | + |
| 173 | +Finally, head over to your project on [Sentry.io](https://sentry.io/) to view the collected data (it takes a couple of moments for the data to appear). |
| 174 | + |
| 175 | +<Include name="quick-start-locate-data-expandable" /> |
| 176 | + |
| 177 | +## Features |
| 178 | + |
| 179 | +### Automatic Error Capturing |
| 180 | + |
| 181 | +The SDK captures 5xx errors automatically via a global `onError` hook. Client errors (3xx/4xx) are not captured by default. |
| 182 | + |
| 183 | +You can customize which errors are captured using the `shouldHandleError` option: |
| 184 | + |
| 185 | +```javascript |
| 186 | +const app = Sentry.withElysia(new Elysia(), { |
| 187 | + shouldHandleError: (context) => { |
| 188 | + const status = context.set.status; |
| 189 | + return status === 500 || status === 503; |
| 190 | + }, |
| 191 | +}); |
| 192 | +``` |
| 193 | + |
| 194 | +### Automatic Tracing |
| 195 | + |
| 196 | +The SDK creates spans for every Elysia lifecycle phase: Request, Parse, Transform, BeforeHandle, Handle, AfterHandle, MapResponse, AfterResponse, and Error. |
| 197 | + |
| 198 | +- The Handle span uses `op: 'request_handler.elysia'`; all other lifecycle spans use `op: 'middleware.elysia'` |
| 199 | +- Transactions use parameterized route names (e.g., `GET /users/:id`) |
| 200 | +- Named function handlers show their function name in spans; arrow functions show as `anonymous` |
| 201 | + |
| 202 | +### Distributed Tracing |
| 203 | + |
| 204 | +The SDK automatically propagates incoming `sentry-trace` and `baggage` headers and injects trace headers into outgoing responses. No additional configuration needed. |
| 205 | + |
| 206 | +### Manual Spans |
| 207 | + |
| 208 | +You can create manual spans within your route handlers: |
| 209 | + |
| 210 | +```javascript |
| 211 | +app.get("/checkout", () => { |
| 212 | + return Sentry.startSpan({ name: "process-payment" }, () => { |
| 213 | + // ... your code |
| 214 | + }); |
| 215 | +}); |
| 216 | +``` |
| 217 | + |
| 218 | +## Runtime Behavior |
| 219 | + |
| 220 | +- **Bun**: The SDK creates root server spans via Elysia's `.wrap()` API with `continueTrace` for trace propagation. No additional runtime instrumentation needed. |
| 221 | +- **Node.js**: The SDK uses Node's HTTP instrumentation for root spans and updates the transaction name with the parameterized route from Elysia. |
| 222 | + |
| 223 | +## Next Steps |
| 224 | + |
| 225 | +- Explore [practical guides](/guides/) on what to monitor, log, track, and investigate after setup |
| 226 | +- Learn how to <PlatformLink to="/usage">manually capture errors</PlatformLink> |
| 227 | +- Continue to <PlatformLink to="/configuration">customize your configuration</PlatformLink> |
| 228 | +- Get familiar with [Sentry's product features](/product) like tracing, insights, and alerts |
| 229 | + |
| 230 | +<Expandable permalink={false} title="Are you having problems setting up the SDK?"> |
| 231 | + |
| 232 | +- Find various topics in <PlatformLink to="/troubleshooting">Troubleshooting</PlatformLink> |
| 233 | +- [Get support](https://sentry.zendesk.com/hc/en-us/) |
| 234 | + |
| 235 | +</Expandable> |
0 commit comments