Setting up a Prometheus Server and Integrating with a Node.js Application

Matías Salinas
4 min readMar 18, 2023

--

Introduction

In this article, we will set up a Prometheus server, an open-source monitoring and alerting toolkit, and integrate it with a Node.js application. We will demonstrate how to ingest data into Prometheus using a custom exporter and how to query the data using PromQL, the powerful query language built for Prometheus.

What is Prometheus?

Prometheus is an open-source systems monitoring and alerting toolkit that focuses on reliability and scalability. It is designed to collect and store time-series data and provides a flexible query language, PromQL, to extract insights from the data. Prometheus works well with other Cloud Native Computing Foundation (CNCF) projects such as Kubernetes, Grafana, and Alertmanager, and is widely used for monitoring containerized environments and microservices.

Setting up a Prometheus server

To set up a Prometheus server, you’ll first need to install it on your machine. You can download the appropriate binary for your platform from the official Prometheus website.

Create a new directory for the Prometheus configuration:

mkdir prometheus
cd prometheus

Next, create a prometheus.yml configuration file in the new directory:

global:
scrape_interval: 15s

scrape_configs:
- job_name: 'ms_exporter'
static_configs:
- targets: ['localhost:3000']

This configuration file tells Prometheus to scrape data from a Node.js exporter (running on port 3000) every 15 seconds.

Now, run the Prometheus with the following command:

./prometheus --config.file=prometheus.yml

This command maps the local prometheus.yml configuration file to the container and exposes the Prometheus server on port 9090.

You should now have a running Prometheus server at http://localhost:9090.

Creating a Node.js application with a custom exporter

To create a Node.js application with a custom exporter, first, create a new directory for your application and install the required dependencies:

mkdir nodejs-exporter
cd nodejs-exporter
npm init -y
npm install express prom-client

Next, create a server.js file in the nodejs-exporter directory with the following content:

const express = require('express');
const client = require('prom-client');
const axios = require('axios');

const app = express();
const port = 3000;

const collectDefaultMetrics = client.collectDefaultMetrics;
collectDefaultMetrics({ prefix: 'ms_exporter_' });

const customCounter = new client.Counter({
name: 'ms_exporter_custom_counter',
help: 'Custom counter for demonstration purposes',
});

app.get('/increment', (req, res) => {
customCounter.inc();
res.send('Incremented custom counter');
});

app.get('/metrics', async (req, res) => {
try {
res.set('Content-Type', client.register.contentType);
const metrics = await client.register.metrics();
res.end(metrics);
} catch (err) {
res.status(500).send(err.message);
}
});

app.get('/query', async (req, res) => {
const query = req.query.query;
const response = await axios.get(`http://localhost:9090/api/v1/query?query=${encodeURIComponent(query)}`);
res.send(response.data);
});

app.listen(port, () => {
console.log(`Node.js Prometheus integration listening at http://localhost:${port}`);
});

This script creates an Express application with three endpoints:

  1. /increment: Increments a custom counter named ms_exporter_custom_counter.
  2. /metrics: Exposes Prometheus-compatible metrics, including the custom counter and default Node.js metrics, with a custom prefix ms_exporter_.
  3. /query: Accepts a PromQL query as a URL parameter and returns the result from the Prometheus server.

Start the Node.js application by running:

node server.js

Prometheus should now scrape metrics from the Node.js application every 15 seconds, as configured in the prometheus.yml file.

Ingesting data into Prometheus

With the Node.js and Express application running, you can now ingest data into Prometheus using the /increment endpoint. To increment the custom counter, simply send a GET request to http://localhost:3000/increment.

Prometheus will automatically scrape the metrics from the Node.js exporter, including the custom counter, every 15 seconds as specified in the prometheus.yml configuration file.

Querying data from the Node.js application

To query data from the Node.js application, you can use the /query endpoint. This endpoint accepts a PromQL query as a URL parameter and returns the result from the Prometheus server.

For example, you can query the custom counter’s value by sending a GET request to:

http://localhost:3000/query?query=ms_exporter_custom_counter

This will return the current value of the custom counter in JSON format.

You can also query other metrics, such as the default Node.js metrics, by modifying the PromQL query in the URL parameter.

Repository Example: https://github.com/msalinas92/Prometheus-Server-and-Integrating-with-a-Node.js-Application

In conclusion, integrating Prometheus with a Node.js and Express application allows you to ingest custom data and query it using the powerful PromQL language. This integration can help you monitor your application’s performance, detect anomalies, and troubleshoot issues more effectively.

--

--

Matías Salinas
Matías Salinas

No responses yet