I learned in code school that you’re never truly finished with an application, and I've found it to be true in all my work since then. The end of creating an application is simply the beginning of a new set of processes, because people will be using your app. There are all kinds of possibilities that can occur, and by possibilities I mean issues, successes, hiccups, signals, or complete disasters. That's where monitoring comes in!

Developers use Node.js to build fast and scalable applications quickly with an event-driven, non-blocking, input/output architecture. But verifying apps with asynchronous nested callbacks is complex. You need to watch your running Node.js applications closely to ensure the best performance for your users, to maximize your system availability, and to maintain your system’s health. 

This article explains everything you need to know about Node.js monitoring. And if you want to get your hands dirty, I’ll show some examples with New Relic and our integration with Node.js that you can try for yourself.

Node.js monitoring—What is it?

Node.js monitoring is simply adding code to your project that will trace the code execution and report on it. To understand Node.js monitoring, you’ll need to be familiar with application monitoring in general. 

You might already have an idea of what application performance monitoring (APM) is. An easy way to describe it: monitoring allows you to visualize the health of your application. This includes detecting anomalies, reducing latency, squashing errors, and optimizing your customers’ experience (and optimizing your own experience, too, especially if you’re doing all this yourself).

Why Node.js monitoring is important

Monitoring your Node.js application is important because many Node application requests are based on HTTP requests. Without a proper monitoring system, it’s very difficult to pinpoint a performance problem diagnosis. When you monitor, you group transactions and give one or more request paths a name. These names are used to:

  • Visualize where your application is spending its time. 
  • Identify slow HTTP requests.
  • Group metrics.
  • Show you the portions of your app that are suffering from slow database performance.

To see an example of using OpenTelemetry with Node.js apps, check out Intro to OpenTelemetry for Node.js apps.

NEW RELIC NODE.JS INTEGRATION
Node.js logo
Start monitoring your Node.js data today.
Install the Node.js quickstart Install the Node.js quickstart

What should be monitored on Node.js

All this sounds great, but what are you looking for? A dependable monitoring system will provide the information you need to quickly identify the source of the issue. Think of it as a control panel, or one of those dark security rooms with all the camera screens. To understand the health of your app, you’ll need some pivotal information like this: process ID, log management, request rate, application availability, resource usage, uptime, downtime, system health, error rates and handling, number of connections, load average, and latency. 

Here’s an example of monitoring transactions for a Node.js app in New Relic: From the number of transactions to their success rate to the time of day where a transaction occurred.

Check out our docs for more info on using New Relic with Node.js

Let’s walk through an example of Node.js monitoring

Here’s an example of how to monitor your Node.js application using New Relic. Our integration with Node.js gives you instant observability with prebuilt dashboards and visualizations.

Imagine you’re running an app for a restaurant. You’d want to know everything there is to know about latency to complete web transactions to errors.  If things go wrong, the business loses money. For this tutorial, let’s use one of the New Relic open source sample applications, FoodMe. If you don't have a project of your own, you can use this one to play around and test New Relic observability.

Node.js monitoring with New Relic

The New Relic Node.js quickstart comes with a preloaded dashboard that gives you an overview of your data in cool interactive visualizations. The quickstart contains four alerts:

  • Apdex score: This alert is triggered when the application performance index  is below 0.5 for five minutes 
    • An Apdex score is a ratio value of the number of satisfied and tolerating requests to the total requests made.
    • At a glance you understand your end users’ overall satisfaction with your app.
    • To learn more about Apdex, watch the What Is Apdex and Why Is It Important? video
  • High CPU utilization: This alert is triggered when the CPU utilization is above 90 percent.
    • This is important because multiple instances of a service running can cause high CPU usage (so it can take extra long for things to work). 
  • Memory usage: This alert is triggered when the memory usage is above 90 percent.
  • Transaction errors: This alert is triggered when the transactions fail more than 10 percent of the time in five minutes.
    • A transaction error refers to function calls and methods calls that make up that unit of work
    • This includes information about the app, database calls, the duration of the transaction, and any errors that may occur.

To go further in depth, check out our documentation on installing the Node.js agent.

When monitoring an application there are a few things to consider. For example, FoodMe is an online restaurant, also known as RelicStaurant, where users can create a cart of food items they would like to purchase. So, to ensure a good user experience you'd want visualizations of the data so you can see everything is running the way you want it to. So, let's look at how long web transactions are taking, the error and success rate of those transactions, and how many there are.    

Also, if you’re thinking, “This project seems cool,” we love contributions! Check out New Relic’s open source projects and the projects we support to learn more.

After forking the FoodMe repo, follow these steps.

Get started with the FoodMe sample app

  1. Install it:

npm install

  1. Run the app:

npm run build

npm run newstart

You should see the screen below automatically pop up, but if not, go ahead and put http://localhost:3000/ in the browser. 

Install the Node.js quickstart to see what you can do with New Relic 

Now that you have FoodMe up and running, let's install the New Relic Node.js quickstart! Before you begin, you’ll need to log in to your account or sign up for a free New Relic account.

1. On the New Relic Node.js quickstart page, select Install now.
You’ll see this screen:

2. Select Begin installation and follow the steps on the next page:

3. Give your application a name. 

4. Install the agent using npm install newrelic --save.

5. Download your custom configuration file and put it in the root directory of your Node.js application. (Hint: Put it on the same level as your package.json file.)

VSCode Explorer screenshot of the newrelic.js file in the root directory

6. Add New Relic to your application’s main module:

In the first line of the main module of your app, add require(‘newrelic’);. It should look like this screenshot:

7. Start your application. 

8. As an optional step, you can connect your log and infrastructure data.

9. Check for data.

To play the part of the users of your app, add food items to your cart in FoodMe, and interact with the UI by adding and deleting items. Now your data is sent to New Relic, and you can see the visualizations.

Here's my example, where you can see the web transactions percentile, the Apdex score, throughput, and error rate.

After pretending to be a hungry user and clicking through the UI a few times and fulfilling my order, my next dashboard example shows a jump in Apdex score and throughput. The Apdex score is 1, well above 0.5, a good rating. This means users are not at all impeded by response time. Currently, all things are working as they should, as you can see in this screenshot:

 

You can see that the error rate is at 0 percent, which means this app is perfect, for now anyway!

Summary

This example shows how powerful it is to have instant visualizations for your  application performance. Solutions to monitor applications' successes, hiccups, and disasters are vital to businesses. In this tutorial, you learned how to monitor to get an overview of what’s happening in an app, which helps you better understand Node.js.

Now, let’s dive into common challenges you will likely face while monitoring Node.js and best practices to overcome them.

Node.js monitoring challenges

Node.js monitoring is not without its challenges. Given the asynchronous and event-driven nature of the platform, some common challenges associated with monitoring Node.js applications include:

  1. Asynchronous execution: Traditional monitoring tools may struggle to trace asynchronous operations and events, making it difficult to understand the execution flow.
  2. Event loop insights: Understanding the event loop's health and identifying bottlenecks can be challenging without proper monitoring tools.
  3. Memory leaks: Identifying and diagnosing memory leaks in Node.js applications tends to be complex due to the automatic garbage collection and heap management.
  4. Real-time performance monitoring: Monitoring in real-time without impacting application performance gets challenging, especially in high-traffic production environments.
  5. Scalability: As Node.js applications scale horizontally, monitoring all instances becomes crucial, and traditional monitoring solutions may struggle to keep up.
  6. Distributed tracing: In microservices architectures, tracking and tracing requests across different services can be complex.

Node.js monitoring best practices

Monitoring Node.js applications is crucial for maintaining performance, identifying issues, and ensuring the overall health of your system. Let's look at some best practices for monitoring Node.js applications:

  1. Use specialized monitoring tools: Choose monitoring tools specifically designed for Node.js applications. Tools like New Relic are better equipped to handle the asynchronous and event-driven nature of Node.js.
  2. Collect key metrics: Monitor essential metrics such as CPU usage, memory usage, event loop latency, garbage collection statistics, and response times. These metrics provide insights into the health and performance of your application.
  3. Asynchronous tracing: Implement asynchronous tracing to track the flow of asynchronous operations, promises, and callbacks. This helps identify performance bottlenecks and improves the overall understanding of your application's behavior.
  4. Real-time monitoring: Utilize real-time monitoring to identify and respond to performance issues promptly. Real-time monitoring enables you to detect anomalies and bottlenecks as they occur, allowing for quicker resolution.
  5. Set up alerts: Define and configure alerts based on key performance indicators (KPIs) and thresholds. Alerts help you proactively address issues before they impact users or lead to system failures.
  6. Monitor dependencies: Monitor external dependencies like databases, third-party APIs, and other services. Issues with dependencies can significantly impact your application's performance and reliability.
  7. Log aggregation: Centralize and aggregate logs from different instances of your Node.js application. Log aggregation makes analyzing and troubleshooting issues easier, especially in a distributed environment.
  8. Security monitoring: Integrate security monitoring tools to detect and respond to potential security threats. Regularly review security logs and implement measures to protect against common vulnerabilities.

Start monitoring your Node.js application with New Relic.

New Relic is an observability platform that helps you build better software. You can bring in data from any digital source so that you can fully understand how to improve your system.