Monitor your application with Prometheus
Tags: prometheus, node
Published: 2020-06-07 17:53:23

Monitoring applications over time is messy thing to do; you must collect state of the applications, store them in time series and finally create an efficient way to run query and analyze them. I love Prometheus, because it makes monitoring applications very easy. Prometheus is an open-source system monitoring and alerting toolkit originally built at SoundCloud.

With Prometheus anyone can create their own monitoring system with custom metrics and reports. The great thing about Prometheus is that you don't need to think about how to store and query data, all you need to do is to collect data from current state of the system and Prometheus takes care of the rest for you.

Prometheus repeatedly fetches the states of your application and stores them in a time series database. With Prometheus's rich API you can make queries and create reports over the time as simple as possible.

In this article we will use `prom-client` to provide our simple application's metrics. `prom-client` is a Prometheus client developed for node.js. You can find a client for your programming languages in [Prometheus client libraries](https://prometheus.io/docs/instrumenting/clientlibs/).

## Create a simple node application
Install `hapijs`
```
yarn add @hapi/hapi
```
Create `index.js` and create simple Hapi server
```js
'use strict';

const Hapi = require('@hapi/hapi');

const init = async () => {

    const server = Hapi.server({
        port: 3000,
        host: 'localhost'
    });

    server.route({
        method: 'GET',
        path: '/',
        handler: (request, h) => {

            return 'Hello World!';
        }
    });

    await server.start();
    console.log('Server running on %s', server.info.uri);
};

process.on('unhandledRejection', (err) => {

    console.log(err);
    process.exit(1);
});

init();
```

## Collect application state
Let's create `metrics.js` and initiate simple counter metrics.
```js
const client = require("prom-client");

const visitors = new client.Counter({
    name: "server_visits_count",
    help: "server_visits_count"
})


module.exports = {
    visit: function visit() {
        visitors.inc();
    }
};
```
Ok, We have a counter that counts page visits, now we need to register `/metrics` in server to show our metric. Prometheus will fetch `/metrics` periodically and store values.  
In `index.js`
```js
const client = require("prom-client");

...

server.route({
    method: 'GET',
    path: '/metrics',
    handler: (request, h) => {
        return h
            .response(client.register.metrics())
            .type(client.register.contentType);
    }
});
```

Finally lets call `hit` function every time a user visits our web application, In `index.js`:

```js
const metrics = require('./metrics')
...
server.route({
    method: 'GET',
    path: '/',
    handler: (request, h) => {
        metrics.visit();
        return 'Hello World!';
    }
});
```

And done, run your server (`node index.js`) and open application in your browser (`http://localhost:3000`), then open `/metrics` route (`http://localhost:3000/metrics`) you'll see your counter metrics current value, try refreshing application home and see what happens on metrics

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kuaomuetc762gemfoyuy.png)

## Install Prometheus and register your application
Go to Prometheus [official download page](https://prometheus.io/download/) and download proper binary file. Extract binary file open `prometheus.yml` in your favorite editor.  
You need to add your application in `scrape_configs` section:
```yaml
scrape_configs:
  # ...
  - job_name: 'sample'
    static_configs:
      - targets: ['localhost:3000']
```
Save the file and start prometheus, the executable file exists in the root directory.  
Prometheus provides a web UI for running queries, visit `http://localshot:3000`. You can see the registered services (including your applications) in `Status > Service Discovery` section in web UI.  
Start running queries and drawing some graphs in `Graph` section. For instance, in `Graph` section enter `server_visits_count` (this is the name of your application visitor counter metric).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/m1baamgefxjdshhss4bo.png)

You can find codes at [Github](https://github.com/farnabaz/pometheus-example)