How To Integrate Sentry.io into a NestJs Application

How To Integrate Sentry.io into a NestJs Application

Time has consistently proven that navigating the software engineering space comes with arming oneself with other skill sets one of which is the skill of debugging Software solutions. As seamless as debugging could be, having to resolve bugs in server-side applications could be much of a herculean endeavor.Thankfully, the advent of third-party applications like Sentry that aid in resolving anomalies with software products(server-side and client-side alike) have the made software development experience a whole lot less of the titanic task it is known to be.

And so, if you have ever run through a wall or experienced difficulty with bringing on sentry into your Nestjs server-side codebase. Look No Further! You have just stumbled on the right technical article.

In this article, I would be putting you through how to integrate the Sentry.io Error Tracking Software into your Nest Application. So, sit tight and let's dive right in.

Requirements

It is already assumed that the developer already engages with Nestjs to some measurable degree or better yet understands the basics of Nodejs or server-side development.

Basic knowledge of the fundamentals of logging concept would be helpful though not necessarily required.

Now that you have seen and realized all that is required to kickstart, Let's Dive Right In!

Let's Get Started

If you are new to Nestjs or you haven't engaged with Nestjs before now, you would have to install the Nest Command Line Interface i.e. (Nest CLI).

So, navigate to your terminal and run the script below...

$ npm i -g @nestjs/cli

After successfully installing the CLI which usually would take ample time depending on your PC, create a new Nestjs Application with which you would be working for the sole purpose of this article. Run the script below.

$ nest new nestjs/sentry.io

Depending on the chosen package manager for your application, run either of the scripts below to install all the needful dependencies for the procedure.

$ yarn add @sentry/node @sentry/minimal @sentry/integrations rxjs
$ npm i @sentry/node @sentry/minimal @sentry/integrations rxjs

If you haven't created an account with sentry.io by now.

Well, you are in the wrong and now is the time to do so!

Navigate to this portion of the dashboard to create a project for your application after having registered or logged on to the sentry app.

The dashboard on sentry.io that shows where the user can readily create a project

Afterward, navigate to the SDK set-up to collect the client keys.

Screen showing the user how they can navigate to collect the client keys

Thereafter, head back to your application to initialize sentry & register your sentry account on your application via the collected keys. This will be done in your src/main.ts file.

It is important to as well create an interceptor file i.e. (src/interceptor.ts) for sentry as displayed below.

src/interceptor.ts

Be sure to inject the interceptor file above into your desired controller by importing the interceptor into your choice controller as this is the only means by which the request is identified by sentry.

import { UseInterceptors, Controller, Get } from "@nestjs/common";
import { SentryInterceptor } from "./sentry.interceptor";

@UseInterceptors(SentryInterceptor)
@Controller()
@Get("/sentry/route")

As with any other middleware in a typical Nodejs application, if Sentry must log an error to the dashboard, it must be ensured that the request (via any endpoint) that goes into the application passes through the sentry middleware before any other. In the same vein, any error which occurs in the server has to first be handled by the sentry error-handling middleware so that it does not escape Sentry's log. To ensure this, you have to register the Sentry request handler before the request goes into the controller and also as well register Sentry error handler before it gets handled by the server as shown below.

This happens in the src/main.ts file.

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

/** Register sentry as an app middleware in order to collect any request made to the application before passing it onto the appropriate controller. */
app.use(Sentry.Handlers.requestHandler() as express.RequestHandler);

 /** Register sentry error handler before passing error onto the app's error handling function. */
  app.use(Sentry.Handlers.errorHandler() as express.ErrorRequestHandler);

The last and final phase would be to confirm that all errors(if any) are being logged and recorded on Sentry.

A tricky method to quickly record or log an error would be to attempt to kickstart or run your application on the same port from two different terminals so that one of the terminals brings about error stating that the port in question is already in use at some other terminal.

Here's what I mean.

Once you can produce this error from another terminal in your app, head back to your Sentry project dashboard and check if there is any corresponding error of the sort logged on your dashboard. Something such as the one you see below.

If there is no error like the one displayed above, you might have to look through again carefully as you might probably have omitted or taken one procedure for the other.

If there is an error displayed like the one seen above, then all I can say is

Big Congratulations to Youuu!

You have successfully integrated the almighty Application Performance Monitoring & Error tracking software i.e. sentry.io into your Nestjs application.

Be sure to watch this space for more amazing articles.

Do not also forget to drop feedback, questions or comment should you have any.

If you like, be sure to suggest articles you might need me to work on.

Until later,

Cheers!