Introduction to NodeJS Server Monitoring.

Akshay Bhargava
3 min readNov 3, 2021

--

Prometheus is a monitoring solution for storing time series data like metrics. Grafana allows to visualize the data stored in Prometheus. We will be looking forward to set up simple metrics by using prom-client npm package. A prom-client instance supports histogram, summaries, gauges and counters.

We are going to simply start up a very basic node-express template and will be attaching the snippet for the same. Since we are here to look for prom-client usage we would not be going over understanding this basic setup.

Step 1 : Initialize a npm repository

npm init -y

Step 2: Install corresponding packages

npm i express cors  prom-client

Step 3: Basic Express Setup :

Files app.js and routes — metrics.route.js & prom.route.js.

We have defined 2 routes one is for exposing our metrics to grafana (/metrics). The other one is (/prom) that we use to test our metrics.

Now we move on to create our prom-client file and initialize all the required fields.

Step 4: Prom-client configurations

A short summary for the above types/metrics.

In prom-client library we have 4 different types of metrics.

The counter metric is the one which wither increases or resets. It cannot person other operations.
The gauge metrics behave the same way as counter but it can be decreased or set to a certain value.
Histograms track sizes and frequency of events.
Summaries calculate percentiles of observed values.

We can now require the above file in our main app.js file

require ('./promClient');

After this step we have setup the prom-client and the server is ready to start. There are yet some steps to actually use the metrics that we setup in the route - /prom

Step 5: Create Controller to handle route redirects

So now our prom.route.js file would be looking like —

The final step is to collect the metrics and expose them on the /metrics route.

Step 6: Expose /metrics route and collect metrics from prom-client

Since it is a very small change it would be good without a separate controller file. So our metrics.route.js file would change like

And well now since everything is up and running. We can our routes and metrics available on /metrics

default metrics
call the route 5 times and see your reflected value in /metrics
another example of setting the value 500 using route — /gauge/set/50

Prom client is very good in getting the server health and monitoring the data. In real life it can be used to monitor auth states like

Total currently logged in users,
All the failed login attempts since the server started.
All database connection errors… and many more

The way to visualize them is very well supported by Grafana and it is very compatible with Prometheus returned data as well. The combination will be very useful in your next node project. For more information you can read the docs, since the example by me is a very short example of what prom-client is capable of.

https://www.npmjs.com/package/prom-client

https://github.com/akshay271703/prom-client-nodejs-short

--

--