Elysia

Learn how to set up Sentry in your Elysia app to capture errors and monitor performance.

You need:

Choose the features you want to configure, and this guide will show you how:

Want to learn more about these features?
  • Issues (always enabled): Sentry's core error monitoring product that automatically reports errors, uncaught exceptions, and unhandled rejections. If you have something that looks like an exception, Sentry can capture it.
  • Tracing: Track software performance while seeing the impact of errors across multiple systems. For example, distributed tracing allows you to follow a request from the frontend to the backend and back.
  • Logs: Centralize and analyze your application logs to correlate them with errors and performance issues. Search, filter, and visualize log data to understand what's happening in your applications.

Run the command for your runtime and preferred package manager to add the Sentry SDK to your application:

Copied
bun add @sentry/elysia

Call Sentry.init() before creating your Elysia app, then wrap the app with Sentry.withElysia() before defining routes.

index.ts
Copied
import * as Sentry from "@sentry/elysia";
import { Elysia } from "elysia";

Sentry.init({
  dsn: "___PUBLIC_DSN___",
  // ___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
});

// 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.

Copied
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.

Copied
npx @sentry/wizard@latest -i sourcemaps

By default, the SDK sends user identity data (IP address, ID, and similar) and other data like HTTP bodies and URL query parameters. This will give you rich debugging context.

The SDK always filters sensitive values whose keys match a built-in denylist, such as auth or password, and sends [Filtered] instead.

To send less data, turn off the categories you don't need in the dataCollection option. For the full list of categories and their defaults, see the dataCollection options.

Copied
Sentry.init({
  dsn: "___PUBLIC_DSN___",
dataCollection: { userInfo: false, // other categories
}, });

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:

Copied
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:

Copied
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:

Copied
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).

Need help locating the captured errors in your Sentry project?
  • Open the Issues page and select an error from the issues list to view the full details and context of this error. For more details, see the Issue Details documentation.
  • Open the Traces page and select a trace to reveal more information about each span, its duration, and any errors. For an interactive UI walkthrough, click here.
  • Open the Logs page and filter by service, environment, or search keywords to view log entries from your application. For an interactive UI walkthrough, click here.

Are you having problems setting up the SDK?
Was this helpful?
Help improve this content
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").