Next.js APM

Get the most out of Middleware OpsAI for your Next.js applications by using the Middleware Next.js SDK (version 1.4.0 or newer).

🚀 Key Features

  • Supports function code capturing for runtime exceptions
  • Works seamlessly with Next.js Framework
  • Automatic sourcemap handling for better error debugging

🛠️ APM Setup

1. Install the Middleware next.js apm package and sourcemap uploader:

2. Modify Config file:

Add the following config to your next.config.js file. The config covers the following things:

  • Enables sourcemaps for browser and server side code.
  • Sets up instrumentation and exception capturing for next.js.
  • Sets up sourcemap-uploader sdk to upload sourcemaps.

If you're using next.js version < 14.0, remove serverComponentsExternalPackages line.

3. Create Instrumentation file:

Create a custom instrumentation.ts (or .js) file in your project root directory or inside the src folder.

Infra Agent Installed
Infra Agent NOT Installed

Manual Exception Capture

For pages-router in nextjs, you have to manually capture exceptions:

⚙️ VCS Metadata Configuration (Recommended)

If your app has a .git directory, the APM will auto-fetch repository URL, commit SHA, and other metadata for OpsAI.

If your app is containerized or does not have a .git directory, set these environment variables to send VCS info to OpsAI (this helps generate solution PRs):

These are just example commands to fetch values from Git. You can set these environment variables in any way that fits your workflow, such as through your CI/CD pipeline or other automation tools.

📚 Resources