# How to collect Prometheus metrics from Node.js or PM2 cluster mode

## The problem

If you’re here, then you probably have a Node.js application running in Cluster mode, either through the native Node APIs or through a package manager like PM2. In this mode, however, there is usually a load balancer that switches between several child processes to do computational work. Each of these child processes has their own statistics for resource usage. If you’re using something like Prometheus, to collect custom metrics, they are also saved per process. This results in jagged or incorrect results, when trying to display and analyze the data in a tool such as Grafana. The question is, how do we collect all these metrics and aggregate them for easy consumtion at one place?

These methods use the NPM Package prom-client, to work with Prometheus.
I will not get into detail how to setup the package, instead I will show mostly the code that makes the aggregation.
In addition, we will be using Express.js to serve the results as a web page.

## Node.js cluster mode

First, we need to setup the Prometheus client and an aggregator registry in our Node.js entry script (main.js).

Then, in our application script (server.js), we need to start metrics collection for every child process that is being run.

Run the application npm server.js.

Note: this may not work for third party packages, which sometimes use custom registries.
If you want to use a custom registry, call the following from the child processes:

## PM2 Process Manager

For PM2, the idea is the same, but the implementation is a bit different. Since PM2 occupies the master process, we cannot set the metrics collection server inside it. Instead we will be spawning a separate application to act as the metrics server.

I have the following ecosystem configuration file:

Note that i am using the same script for the server and for the application.
Here is the code of the main.js script:

In the above example, you can see how to setup a communication pipeline to send messages between the different applications, managed by PM2.
Now the only thing left is to implement your application’s prometheus metric colletion.

my-app.js

## Inspiration:

Some parts of the aggregation method were inspired by the following projects. I am adding links, as well as explanation why(if) I didn’t use these projects directly.

• Prom-client - NPM Package - This package is what most other Prometheus packages use as a base. In its README there is description on how to set up metrics collection for Node.js cluster application. I am using this package, as a dependency for other packages (Express prom-bundle) and directly, in some cases. However, the method that is described in its readme, doesn’t work directly with PM2 applications, since PM2 occupies the Node.js master process. From here, I’ve borrowed the general idea of metrics collection and aggregation to a single process.

• PM2 Cluster Prometheus - PM2 Package - This PM2 package might be a “one-click” stop for you, as it implements everything that you will need. I will leave learning how to use it by yourselves. I didn’t use this package, because it’s outdated. Plus it has some weird dependencies (NPM consul and address), which are outdated too. Note: This package uses pmx, which i saw is deprecated.

• PM2 Cluster Prometheus - NPM Package - This package has a totally different author than the previous one - don’t be confused by the name. Actually, this is the package that I’ve used to figure out how to aggregate the Prometheus statistics into one registry. This is another “one-click” solution that you might want to explore. I didn’t use this package, because even if it’s signifficantly newer, it’s still quite old and, according to Snyk.io, has some security issues.

• PM2 Messages - A package for sending Inter-Process Communication(IPC) messages in PM2. I didn’t use it, because I don’t need it for my simple use case and I want to reduce dependencies for my project to a minimum..

### Comments

###### Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×