Node.js Open-Source Monitoring Tools
Tags: node, javascript, opensource, devops
Published: 2019-05-16 14:18:23

{% parler https://www.parler.io/audio/7723913179/1eb8f9234e500cc772c089ba348248edf768458d.1d238b69-6730-40a0-8aa4-d9a932e47577.mp3 %}

What is the most important feature your Node.js application can have? Do you think it’s having fancy fuzzy logic for your full-text search, or maybe using sockets for real-time chats? You tell me. What’s the fanciest, most amazing and sexy feature you can add to your Node.js application?

Want to know mine? **High performance with no downtime**. Performant applications need to do three things well.

1. Ensure minimal downtime
2. Have predictable resource usage
3. Scale effectively based on load

In Part 1, [Node.js Key Metrics to Monitor](https://dev.to/sematext/top-node-js-metrics-to-monitor-3460-temp-slug-2084298), we talked about key Node.js metrics you should monitor in order to understand the health of your application. I also explained bad practices in Node.js you should avoid, such as blocking the thread and creating memory leaks, but also a few neat tricks you can use to boost the performance of your application, like using the cluster module to create worker processes and forking long-running processes to run separately from the main thread.

In this article, I’ll explain how to add monitoring to your Node.js application with 5 different open-source tools. They may not have full-blown features like [Sematext](https://sematext.com/) or Datadog, but keep in mind they’re open-source products and can hold their own just fine.

![monitoring overview 1](https://sematext.com/wp-content/uploads/2019/05/monitoring-overview-1.png)

## Node.js monitoring with Appmetrics

[Node Application Metrics Dashboard](https://developer.ibm.com/node/monitoring-post-mortem/application-metrics-node-js/) shows the performance metrics of your running Node.js application. It’s a simple module you install and require at the top of your main Node.js source file. You [install the module from npm](https://www.npmjs.com/package/appmetrics-dash) by running the following command in your terminal.

```bash
$ npm install appmetrics-dash
```

[Appmetrics](https://github.com/RuntimeTools/appmetrics-dash) provides a very easy-to-use web-based dashboard. Everything you need to do in order to get a dashboard for all HTTP servers created by your application then you add this snippet in your app.js file, or whatever you call your main source file.

```javascript
// Before all other 'require' statements
require('appmetrics-dash').attach()
```

You’ll now have a new server route `/appmetrics-dash` where you can see a plethora of useful metrics.

- CPU Profiling
- HTTP Incoming Requests
- HTTP Throughput
- Average Response Times (top 5)
- CPU
- Memory
- Heap
- Event Loop Times
- Environment
- Other Requests
- HTTP Outbound Requests

This tool doesn’t only display metrics. It lets you generate Node Reports and Heap Snapshots directly from the dashboard. Apart from that, you have access to Flame Graphs. Pretty cool for an open source tool.

![screencapture localhost 3000 appmetrics dash 2019 04 29 09 51 12 1](https://sematext.com/wp-content/uploads/2019/05/screencapture-localhost-3000-appmetrics-dash-2019-04-29-09_51_12-1.png)

## Monitoring Node.js with Express Status Monitor

[Express.js](https://expressjs.com/) is the de-facto framework of choice for Node.js developers. [Express Status Monitor](https://github.com/RafalWilinski/express-status-monitor) is an incredibly simple, self-hosted module, you add to your Express server. It exposes a `/status` route that reports real-time server metrics with the help of [Socket.io](https://socket.io/) and [Chart.js](https://www.chartjs.org/).

Installing the tool from [npm](https://www.npmjs.com/package/express-status-monitor) is as simple as this.

```bash
$ npm install express-status-monitor
```

After you have the module installed, you need to add it before any other middleware or router.

```javascript
app.use(require('express-status-monitor')())
```

Once you run your server go to the `/status` route and check out your Node.js metrics.

![screencapture localhost 3000 status 2019 04 14 10 31 47 1](https://sematext.com/wp-content/uploads/2019/05/screencapture-localhost-3000-status-2019-04-14-10_31_47-1.png)

## Monitoring Node.js with Prometheus

Unless you’re living under a rock, you must have heard of [Prometheus](https://prometheus.io/). It’s the most notable and famous open-source monitoring tool you can use today. Prometheus is 100% open source and community-driven. All components are available under the [Apache 2 License](http://www.apache.org/licenses/LICENSE-2.0) on [GitHub](https://github.com/prometheus). It’s also a [graduated member project](https://www.cncf.io/projects/) of the [Cloud Native Computing Foundation](https://cncf.io/), alongside projects like [Kubernetes](https://kubernetes.io/) and [Fluentd](https://www.fluentd.org/).

To start monitoring with Prometheus, you need to [download the latest release](https://prometheus.io/download) and install it.

```bash
$ tar xvfz prometheus-\*.tar.gz
$ cd prometheus-\*
```

Then you start it by running the executable file but before running this command you need to create a prometheus.yml file. It’s a configuration file for collecting metrics from monitored targets by scraping metrics HTTP endpoints on these targets.

```yaml
# prometheus.yml
scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 1s
    static_configs:
      - targets: ['127.0.0.1:3000']
        labels:
          service: 'test-prom'
          group: 'production'
```

Now you can run Prometheus.

```bash
$ ./prometheus --config.file=prometheus.yml
```

However, I’m rather lazy, and I like [Docker](https://www.docker.com/) a lot. So the way I do it is to run the [official Prometheus Docker image](https://hub.docker.com/r/prom/prometheus/) and avoid all the hassle of downloading it.

## Monitoring Node.js with Prometheus and Docker

First of all, navigate to the root directory of your Node.js application. Here you’ll create a `prometheus-data` directory and place the `prometheus.yml` file in it. Once you’ve done this, go ahead and move on to running the Prometheus Docker container.

You’ll grab the official Prometheus Docker image and run a container with the docker run command.

```bash
$ docker run -d \
    --name prometheus \
    --network="host" \
    -v "$(pwd)/prometheus-data":/prometheus-data \
    prom/prometheus \
    -config.file=/prometheus-data/prometheus.yml
```

I’ve chosen to run the container with `–network="host"` to let the Prometheus container access the localhost of my host, and in doing so, the Node.js application’s exposed HTTP endpoint. Otherwise, if you were running both Prometheus and Node.js in containers, you’d set up a [network](https://docs.docker.com/network/bridge/) between the two to only be visible between each other.

The `-v` flag will map the `prometheus-data` directory from the host to an identically named directory inside of the container.

With the Prometheus container running, you can move on to adding the needed configuration in your Node.js application to expose a metrics endpoint. You’ll need to install the [Prometheus Client for Node.js from npm](https://www.npmjs.com/package/prom-client) first.

```bash
$ npm install prom-client
```

Then you’ll add the base configuration for Prometheus.

```javascript
// after all 'require' statements
const client = require('prom-client')
const collectDefaultMetrics = client.collectDefaultMetrics
collectDefaultMetrics({ timeout: 1000 })
app.get('/metrics', (req, res) => {
  res.set('Content-Type', client.register.contentType)
  res.end(client.register.metrics())
})
```

What’s left is to run the Node.js server and open up `http://localhost:9090/graph` to access the Prometheus graphs.

![Selection 436 1](https://sematext.com/wp-content/uploads/2019/05/Selection_436-1.png)

## Monitoring Node.js performance with Clinic.js

[Clinic.js](https://clinicjs.org/) includes three tools to help diagnose and pinpoint Node.js performance issues. It’s surprisingly easy to use. All you need to do is install the [module from npm](https://www.npmjs.com/package/clinic) and run it. This will generate reports for you that make troubleshooting much easier.

To install Clinic.js run this command in your terminal.

```
$ npm install clinic
```

Once you have it installed it’s all up to choosing what kind of report to generate. You can pick between three.

- **Doctor**
  - Collects metrics by injecting probes
  - Assess health and heuristics
  - Creates recommendations
- **Bubbleprof**  – a new, completely unique, approach to profiling your Node.js code
  - Collects metrics using async\_hooks
  - Tracks latency between operations
  - Creates bubble graphs
- **Flame**  – uncovers the bottlenecks and hot paths in your code with flamegraphs
  - Collects metrics by CPU sampling
  - Tracks top-of-stack frequency
  - Creates flame graphs

Let’s start by running the Doctor and load testing a Node.js application.

```bash
$ clinic doctor -- node app.js
```

While it’s running, run a load test with your desired tool.

```bash
$ loadtest -n 1000 -c 100 [http://localhost:3000/api](http://localhost:3000/api)
```

Once it’s done running, stop the server and Clinic.js Doctor will open up a report you can check out.

![screencapture file home raha code sandbox node nodejs monitoring clinic 2961 clinic doctor html 2019 04 29 13 59 42 1](https://sematext.com/wp-content/uploads/2019/05/screencapture-file-home-raha-code-sandbox-node-nodejs-monitoring-clinic-2961-clinic-doctor-html-2019-04-29-13_59_42-1.png)

Using this same approach you can run either Bubbleprof or Flame and get graphs for the respective tools.

![screencapture file home raha code sandbox node nodejs monitoring clinic 3954 clinic bubbleprof html 2019 04 29 14 04 33 1](https://sematext.com/wp-content/uploads/2019/05/screencapture-file-home-raha-code-sandbox-node-nodejs-monitoring-clinic-2961-clinic-doctor-html-2019-04-29-13_59_42-1.png)

![Selection 456 1](https://sematext.com/wp-content/uploads/2019/05/Selection_456-1.png)

## Monitoring Node.js with PM2

Running Node.js applications in production gets a lot easier with PM2. It’s a process manager that easily lets you run apps in cluster mode. Or, in English, it’ll spawn a process for every CPU core your host has.

Start by installing [PM2](https://www.npmjs.com/package/pm2).

```bash
$ npm install pm2 -g
```

Once it’s installed you spawn the PM2 daemon by running this command in your terminal, if your main source file is app.js.

```bash
$ pm2 start app.js -i 0
```

The `-i 0` flag stands for instances. This will run your Node.js application in cluster mode, where the 0 stands for number of CPU cores. You can manually put whatever number you want, but letting PM2 count the cores and spawn that amount of workers is much easier.

[Monitoring Node.js with PM2](https://github.com/Unitech/pm2) is just as easy.

```bash
$ pm2 monit
```

This command will open a dashboard in the terminal. Here you can monitor processes, logs, loop delay, process memory, and CPU.

![Selection 458 1](https://sematext.com/wp-content/uploads/2019/05/Selection_458-1.png)

## Wrapping up Monitoring Node.js with Open-Source Tools

Performance metrics are crucial for keeping your users happy. In this article, I’ve shown you how to add monitoring to your Node.js application with 5 different open-source tools. After learning about [Node.js key metrics to monitor](https://sematext.com/blog/top-nodejs-metrics-to-watch/) in Part 1 of this series, adding tooling to monitor your apps in real life is the natural learning progression. The last part of the series will cover [production ready Node.js monitoring with Sematext](https://sematext.com/blog/nodejs-monitoring-made-easy-with-sematext/).

If you want to check out sample code, [here’s a repo](https://github.com/adnanrahic/nodejs-monitoring-sematext/tree/develop) with all samples. You can also clone the repo and pick any of the tools right away.

If you need full-stack observability for your software stack, check out [Sematext](https://sematext.com/). We’re pushing to [open source our products](https://github.com/sematext) and make an impact.

_Hope you guys and girls enjoyed reading this as much as I enjoyed writing it. If you liked it, slap that tiny share button so more people will see this tutorial. Until next time, be curious and have fun._