DEV Community

Cover image for How to add Sentry to Next.js the right way!
Alex Patterson for CodingCatDev

Posted on • Originally published at codingcat.dev

How to add Sentry to Next.js the right way!

Original: https://codingcat.dev/podcast/how-to-add-sentry-to-next-js-the-right-way

Welcome back, coding enthusiasts! If you've ever had Next.js up and running and found yourself in need of better monitoring on both the front and back end, you've landed on the right page. Today, we'll dive deep into adding Sentry to Next.js with insights from Luca, who works on the JavaScript SDKs at Sentry, focusing especially on the Next.js SDK.

Let's roll up our sleeves and get started!

Introduction to Sentry for Next.js

Sentry is a powerful tool for error tracking and performance monitoring. If you rely on Next.js for your web applications, integrating Sentry can provide detailed insights into both client-side and server-side issues.

What You'll Learn

Step 1: Setting Up Sentry

To get started with Sentry in your Next.js project, you'll need to install the Sentry SDK. Let's walk through the process.

Installation

First, initialize your project and use the command line wizard:

npx create-next-app@latest my-sentry-nextjs-appcd my-sentry-nextjs-app

Enter fullscreen mode Exit fullscreen mode

Now, install the Sentry package:

npx @sentry/wizard@latest -i nextjs
Enter fullscreen mode Exit fullscreen mode

This installs the Sentry SDK and sets the stage for the configuration.

Initial Configuration

With Sentry installed, you'll need to configure it. Sentry will prompt you with several options to customize its functionality according to your needs.

Handling Ad Blockers

One interesting challenge comes from Sentry occasionally being listed by ad blockers. Luca mentions that although it's a hurdle, their team is transparent about it, promising nothing shady occurs. Enabling the option to circumvent ad blockers ensures that you'll capture all necessary events.

{"allowAdBlockers": true}

Enter fullscreen mode Exit fullscreen mode

Step 2: Configuring the SDK

Next, we'll delve into configuring the Sentry SDK. The wizard generates three primary configuration files for different runtime environments: client, server, and edge.

Client

import * as Sentry from "@sentry/nextjs";

Sentry.init({
  dsn: "https://64540574fda64fe21bb5e3c4c14d698e@o1029244.ingest.us.sentry.io/4507492345053184",
  // Replay may only be enabled for the client-side
  integrations: [Sentry.replayIntegration()],

  // Set tracesSampleRate to 1.0 to capture 100%
  // of transactions for tracing.
  // We recommend adjusting this value in production
  tracesSampleRate: 1.0,

  // Capture Replay for 10% of all sessions,
  // plus for 100% of sessions with an error
  replaysSessionSampleRate: 0.1,
  replaysOnErrorSampleRate: 1.0,

  // ...

  // Note: if you want to override the automatic release value, do not set a
  // `release` value here - use the environment variable `SENTRY_RELEASE`, so
  // that it will also get attached to your source maps
});

Enter fullscreen mode Exit fullscreen mode

Server

import * as Sentry from "@sentry/nextjs";

Sentry.init({
  dsn: "https://64540574fda64fe21bb5e3c4c14d698e@o1029244.ingest.us.sentry.io/4507492345053184",

  // Set tracesSampleRate to 1.0 to capture 100%
  // of transactions for tracing.
  // We recommend adjusting this value in production
  tracesSampleRate: 1.0,

  // ...

  // Note: if you want to override the automatic release value, do not set a
  // `release` value here - use the environment variable `SENTRY_RELEASE`, so
  // that it will also get attached to your source maps
});

Enter fullscreen mode Exit fullscreen mode

Edge

import * as Sentry from "@sentry/nextjs";

Sentry.init({
  dsn: "https://64540574fda64fe21bb5e3c4c14d698e@o1029244.ingest.us.sentry.io/4507492345053184",

  // Set tracesSampleRate to 1.0 to capture 100%
  // of transactions for tracing.
  // We recommend adjusting this value in production
  tracesSampleRate: 1.0,

  // ...

  // Note: if you want to override the automatic release value, do not set a
  // `release` value here - use the environment variable `SENTRY_RELEASE`, so
  // that it will also get attached to your source maps
});

Enter fullscreen mode Exit fullscreen mode

Instrumentation

Instrumentation is key, especially for monitoring applications. The wizard provides an instrumentation.ts file, a feature to register and monitor server behavior.

export async function register() {
  if (process.env.NEXT_RUNTIME === "nodejs") {
    await import("./sentry.server.config");
  }

  if (process.env.NEXT_RUNTIME === "edge") {
    await import("./sentry.edge.config");
  }
}

Enter fullscreen mode Exit fullscreen mode

Navigating Sentry in Development

Let's take a spin through the Sentry interface, which offers a wealth of information on how your Next.js app is performing.

Performance Monitoring

Performance is critical. Sentry monitors API requests, rendering times, and more, providing a trace view that details loading time, resource fetching, and more.

For high-traffic sites receiving thousands of views a day, consider these best practices to ensure optimal performance.

Filtering Noise

Third-party code might throw errors that clutter your dashboard. Sentry allows you to filter these out.

import { Integrations } from '@sentry/nextjs';new Integrations.ThirdPartyFilter({filterKeys: ['sentry-demo']});

Enter fullscreen mode Exit fullscreen mode

Improving Replay Rates

Replays can quickly eat into your quota. Optimizing replay settings ensures you're gathering quality data without unnecessary noise.

{"replaysOnErrorSampleRate": 0.1}

Enter fullscreen mode Exit fullscreen mode

Introduction

In this episode of Code with CodingCat, we delve into integrating Sentry with a Next.js application to monitor its performance and errors effectively. Joined by Luca, a developer who works on the Sentry JavaScript SDKs, we embark on a step-by-step exploration of setting up Sentry, configuring its features, and optimizing the monitoring process.

This session covers everything from the initial setup to advanced deployment strategies, offering valuable insights into error handling and performance tracking. Whether you're a novice to Sentry or looking to fine-tune your monitoring setup, this episode is packed with practical advice and expert tips.

Guest Information

Luca

Luca is a developer at Sentry, focusing on maintaining the Next.js SDK. He brings his wealth of experience to the episode, helping to unlock the full potential of Sentry's features for monitoring JavaScript applications.

Related Resources and Links

Episode Summary

Join us for an exciting exploration of integrating Sentry with a Next.js application. In this episode, Luca guides us through the setup process, starting with the initial installation of the Sentry SDK. He details the various configuration options available and explains how to tailor the setup to suit different needs, such as circumventing ad blockers, managing source maps, and adjusting log levels.

We also dive deep into performance monitoring and troubleshooting, with Luca providing expert insights into how Sentry tracks errors across front-end and back-end processes. This episode covers essential features like session replay, breadcrumbs, and the ability to filter out noise from third-party libraries.

Luca also discusses some advanced deployment strategies on platforms like Vercel and highlights the importance of optimizing for performance with real-time monitoring.

By the end of this episode, you'll be equipped with the knowledge to seamlessly integrate Sentry into your Next.js projects and harness its full capabilities to monitor, track, and optimize application performance effectively.

Key Takeaways

  • Seamless integration of Sentry with Next.js for error tracking and performance monitoring.
  • Detailed explanation of Sentry's configuration options to tailor the setup.
  • Insights into troubleshooting common issues with Sentry and Next.js.
  • Advanced deployment strategies and optimizations for real-time monitoring.
  • The importance of filtering out noisy third-party errors for cleaner data.

Notable Quotes and Examples

"We can upload source maps to your project in Sentry, like associated with your application." — [Timestamp: 15:30]

"Everything that happens is instrumented. This feature is actually experimental, but the folks over at Vercel have been very helpful." — [Timestamp: 10:20]

"Session replay records your DOM and all the changes, and it's streamed to Sentry servers to stitch it back together." — [Timestamp: 45:14]

Detailed Episode Breakdown

00:00 - Introduction

Luca introduces Sentry and its capabilities with Next.js, setting the stage for a hands-on demonstration of the integration process.

01:57 - Installation Guide

Luca walks us through the installation of the Sentry SDK, emphasizing the importance of using the command line wizard to connect your account and select your project.

npm install @sentry/nextjs

Enter fullscreen mode Exit fullscreen mode

03:39 - Configuration Details

Luca elaborates on various SDK features, including circumventions for ad blockers, edge, and server setups. He explains how to wrap your Next config and discusses the significance of source maps.

06:08 - Performance Monitoring

The episode delves into the performance monitoring features offered by Sentry, with a focus on capturing performance measurements and understanding the user's journey via session replay.

"When I first saw this feature, my mind was blown because it was so crazy." — [Timestamp: 8:40]

14:16 - Troubleshooting Tips

Luca shares common troubleshooting tips, highlighting potential pitfalls with source maps and runtime tagging. He discusses ways to minimize errors from third-party libraries.

24:12 - Advanced Features

An exploration of advanced features such as Sentry's integration with Cron jobs on platforms like Vercel. Luca explains how to configure tracing and debugging options for maximum efficiency.

40:07 - Deployment Insights

The episode explains deployment strategies, particularly focusing on the seamless integration with Vercel’s infrastructure for continuous integration and deployment.

55:02 - Performance Optimization

A discussion on optimizing performance monitoring by leveraging web vitals and understanding long load times, offering valuable insights into maintaining high-performance standards.

Social Media Highlights

  • "Discover how to integrate Sentry with your Next.js projects and enhance your monitoring abilities with Luca!"
  • "Diving into Sentry's session replay feature was a revelation—seeing the user journey has never been easier!"
  • "Learn how to optimize error tracking by filtering out noisy third-party errors with Sentry. Game changer!"
  • "Deploying on Vercel with Sentry integration? Tune in for expert tips on streamlining your CI/CD pipeline!"
  • "Understanding web vitals in real-time with Sentry's performance monitoring tool could save your next project!"

Top comments (0)