Elysia
Learn how to set up Sentry in your Elysia app to capture errors and monitor performance.
This SDK is currently in ALPHA. Alpha features are still in progress, may have bugs, and might include breaking changes. Please reach out on GitHub if you have any feedback or concerns.
You need:
- A Sentry account and project
- An Elysia application (
v1.4.0+) - Bun or Node.js 18+ (with @elysiajs/node adapter)
Choose the features you want to configure, and this guide will show you how:
Run the command for your runtime and preferred package manager to add the Sentry SDK to your application:
You do not need @elysiajs/opentelemetry. The @sentry/elysia SDK handles all instrumentation natively.
bun add @sentry/elysia
Call Sentry.init() before creating your Elysia app, then wrap the app with Sentry.withElysia() before defining routes.
index.tsimport * as Sentry from "@sentry/elysia";
import { Elysia } from "elysia";
Sentry.init({
dsn: "___PUBLIC_DSN___",
// Adds request headers and IP for users, for more info visit:
// https://docs.sentry.io/platforms/javascript/guides/elysia/configuration/options/#sendDefaultPii
sendDefaultPii: true,
// ___PRODUCT_OPTION_START___ performance
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for tracing.
// We recommend adjusting this value in production
// Learn more at
// https://docs.sentry.io/platforms/javascript/guides/elysia/configuration/options/#tracesSampleRate
tracesSampleRate: 1.0,
// ___PRODUCT_OPTION_END___ performance
// ___PRODUCT_OPTION_START___ logs
// Enable logs to be sent to Sentry
enableLogs: true,
// ___PRODUCT_OPTION_END___ logs
});
// withElysia returns the app instance, so you can chain routes directly
const app = Sentry.withElysia(new Elysia())
.get("/", () => "Hello World")
.listen(3000);
The SDK captures 5xx errors automatically via a global onError hook. Client errors (3xx/4xx) are not captured by default. You can customize which errors are captured using the shouldHandleError option.
const app = Sentry.withElysia(new Elysia(), {
shouldHandleError: (context) => {
const status = context.set.status;
return status === 500 || status === 503;
},
});
The stack traces in your Sentry errors probably won't look like your actual code without unminifying them. To fix this, upload your source maps to Sentry. The easiest way to do this is by using the Sentry Wizard.
Alternatively, take a look at our Uploading Source Maps documentation.
npx @sentry/wizard@latest -i sourcemaps
Let's test your setup and confirm that Sentry is working correctly and sending data to your Sentry project.
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:
app.get("/debug-sentry", () => {
throw new Error("My first Sentry error!");
});
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:
app.get("/debug-sentry", async () => {
await Sentry.startSpan(
{
op: "test",
name: "My First Test Transaction",
},
async () => {
await new Promise((resolve) => setTimeout(resolve, 100));
throw new Error("My first Sentry error!");
},
);
});
To verify that Sentry catches your logs, add some log statements to your application:
Sentry.logger.info("User example action completed");
Sentry.logger.warn("Slow operation detected", {
operation: "data_fetch",
duration: 3500,
});
Sentry.logger.error("Validation failed", {
field: "email",
reason: "Invalid email",
});
Finally, head over to your project on Sentry.io to view the collected data (it takes a couple of moments for the data to appear).
- Explore practical guides on what to monitor, log, track, and investigate after setup
- Learn how to manually capture errors
- Continue to customize your configuration
- Get familiar with Sentry's product features like tracing, insights, and alerts
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").