DEV Community

Cover image for How to monitor your Next.js Application performance and track errors with Sentry
Gbengacode
Gbengacode

Posted on • Edited on

How to monitor your Next.js Application performance and track errors with Sentry

What is Sentry

Sentry is an open-source platform for tracking and monitoring errors, empowering developers to detect, prioritize, and address issues in real-time promptly. This tool delivers comprehensive error reports, stack traces, and deep insights into the underlying causes of errors, facilitating swift diagnosis and resolution. It extends its support across various platforms and languages such as JavaScript, Node.js, Python, and Java. By leveraging Sentry's robust features, development teams can effectively maintain the stability and dependability of their applications.

How to setup Sentry in your Next.js Application

The easiest way to setup is to through the use of SDK
Sentry captures data by using an SDK within your application’s runtime.

Procedures

  1. Sign in to Sentry
  2. Bootstrap a Next.js Application by running ```javascript

npx create-next-app@latest

3. Install the SDK through the installation wizard ```javascript


npx @sentry/wizard@latest -i nextjs


Enter fullscreen mode Exit fullscreen mode

The wizard will prompt you to log in to Sentry. It will then automatically do the following steps for you:

  • create config files with the default Sentry.init() calls for each runtime (node, browser, edge)

  • create or update your Next.js config with the default Sentry configuration

  • create .sentryclirc with an auth token to upload source maps (this file is automatically added to .gitignore)
    add an example page to your app to verify your Sentry setup

We can pass extra configuration the through the option in the Sentry.init() calls.



Sentry.init({
  dsn: "https://69cb89218edf8713f71b66a00c5c9f66@o4506789727305728.ingest.sentry.io/4506789728419840",
  maxBreadcrumbs: 50,
  debug: true,
});


Enter fullscreen mode Exit fullscreen mode

The list of the common options are dsn, debug, release, environment etc. You can check the doc for more option exploration https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/options/

Let's make a sample Next.js API call and test Sentry error reporting



"use client"
import * as Sentry from "@sentry/nextjs"
export default function Home() {
const failedApiCall = async () => {
try {
throw new Error("this is an error")
} catch (error) {
Sentry.captureException(error)
}
}
return (
<main className="flex min-h-screen flex-col items-center justify-between p-24">
<button className="p-3 border border-blue-500" onClick={failedApiCall}>Throw an Error</button>
</main>
);
}

Enter fullscreen mode Exit fullscreen mode




Sentry Performance Monitoring and Error Reporting

Sentry performance monitor and Error reporting

From the report, we can get useful insight into the error where the application that leads to the error the source URL the browser, and the system which the user uses to access the application and other useful sights.
That is it for now. For more posts like this kindly follow, like, and comment. See you in my next post

Top comments (1)

Collapse
 
emmabase profile image
Emmanuel Eneche

Great share. Thank you for this insights, will try sentry out.