---
title: "Early Preloading"
description: "Initialize Sentry before application dependencies load to enable full third-party auto-instrumentation."
canonical_url: "https://eienjs.com/packages/adonisjs-sentry/advanced/early-preloading"
---

> Index of every page on this site: [llms.txt](https://eienjs.com/llms.txt)

By default, `@eienjs/adonisjs-sentry` initializes Sentry during the provider's `register()` lifecycle hook.

While this captures all unhandled exceptions and route-level spans, some third-party libraries (such as database drivers `pg`/`mysql2`, Redis clients `ioredis`, or HTTP clients) require Sentry to be initialized *before* those modules are first imported by Node.js in order to automatically instrument their queries and network calls.

## How Early Preloading Works

AdonisJS provides a `preloads` mechanism in `adonisrc.ts` that runs modules at the very start of the boot cycle before providers and application code are registered.

When Sentry is initialized in a preload file, `SentryProvider` detects that `Sentry.isInitialized()` is already `true`. The provider gracefully skips calling `Sentry.init(...)` again, configures the exception reporting rules from `config/sentry.ts`, and retains ownership of the graceful shutdown hook.

## Setup Instructions

<steps>

### 1. Create a Preload File

Create a new file at `start/sentry.ts`:

```ts [start/sentry.ts]
import app from '@adonisjs/core/services/app';
import { Sentry } from '@eienjs/adonisjs-sentry';
import env from '#start/env';

if (app.inProduction && env.get('SENTRY_DSN')) {
  Sentry.init({
    dsn: env.get('SENTRY_DSN'),
    environment: app.nodeEnvironment,
    release: env.get('SENTRY_RELEASE'),
    tracesSampleRate: 0.2,
  });
}
```

### 2. Register the Preload in `adonisrc.ts`

Add your preload to the `preloads` array in `adonisrc.ts`, ensuring it is listed early:

```ts [adonisrc.ts]
export default defineConfig({
  preloads: [
    {
      file: () => import('#start/sentry'),
      environment: ['web', 'console'],
    },
    // ... other preloads
  ],
  // ...
});
```

<note>

Keep `@eienjs/adonisjs-sentry/providers/sentry_provider` in your `providers` array in `adonisrc.ts`. The provider is responsible for configuring the ignore lists used by `reportException` and flushing pending events during application shutdown (`Sentry.close(2000)`).

</note>

### 3. Verify Auto-Instrumentation

Run your application and verify that database queries and external HTTP requests appear as nested spans under your route transactions in the Sentry Performance dashboard.

</steps>

## Sitemap

See the full [sitemap](https://eienjs.com/sitemap.md) for all pages.
