PrometheusSetting Up Prometheus to Monitor Your Node.js Application

mdshamsfiroz
3 min readOct 31, 2024

--

In the world of microservices and distributed systems, monitoring is crucial. Prometheus has emerged as a powerful tool for collecting and querying metrics. In this guide, we’ll walk through the process of setting up Prometheus to monitor a Node.js application.

Prerequisites

Before we begin, ensure you have:

  1. A Linux-based system (we’ll use Ubuntu in this example)
  2. Root or sudo access
  3. A Node.js application running (we’ll assume it’s on localhost:3000)

Step 1: Installing Prometheus

First, let’s install Prometheus:

# Update system packages
sudo apt-get update
# Install Prometheus
sudo apt-get install -y prometheus
# Start Prometheus service
sudo systemctl start prometheus
sudo systemctl enable prometheus
# Check status of Prometheus
sudo systemctl status prometheus

This will install Prometheus, start it as a service, and enable it to start on boot.

Step 2: Configuring Prometheus

Now, let’s configure Prometheus to scrape metrics from our Node.js application. We’ll create a new configuration file:

sudo nano /etc/prometheus/prometheus.yml

Add the following content:

global:
scrape_interval: 15s # Default scrape interval
scrape_configs:
- job_name: 'node_app'
static_configs:
- targets: ['localhost:3000'] # Replace with your Node.js app endpoint

Save the file and exit. Then, reload the Prometheus configuratio

sudo systemctl reload prometheus

Step 3: Preparing Your Node.js Application

To expose metrics for Prometheus, we need to add a metrics endpoint to our Node.js application. We’ll use the prom-client library for this.Install the library:

npm install prom-client

Then, modify your Node.js application to expose metrics:

const express = require('express');
const promClient = require('prom-client');
const app = express();
const collectDefaultMetrics = promClient.collectDefaultMetrics;
// Collect default metrics
collectDefaultMetrics();
// Create a custom counter
const httpRequestsTotal = new promClient.Counter({
name: 'http_requests_total',
help: 'Total number of HTTP requests',
labelNames: ['method', 'route', 'status']
});
// Example route
app.get('/', (req, res) => {
httpRequestsTotal.inc({ method: 'GET', route: '/', status: 200 });
res.send('Hello World!');
});
// Metrics endpoint
app.get('/metrics', async (req, res) => {
res.set('Content-Type', promClient.register.contentType);
res.end(await promClient.register.metrics());
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});

This code sets up a basic Express server with a custom metric (http_requests_total) and exposes a /metrics endpoint for Prometheus to scrape.

Step 4: Verifying Metric Collection

To verify that Prometheus is collecting metrics from your Node.js application:

  1. Access the Prometheus UI by opening a web browser and navigating to http://localhost:9090.
  2. In the Prometheus UI, go to the “Status” dropdown and select “Targets”. You should see your Node.js application listed as a target, and it should be in the “UP” state.
  3. To query your metrics, go to the “Graph” tab and enter a metric name (e.g., http_requests_total) in the query box. Click "Execute" to see the data.

Conclusion

Congratulations! You’ve successfully set up Prometheus to monitor your Node.js application. This setup allows you to:

  1. Collect default Node.js metrics automatically
  2. Create and track custom metrics specific to your application
  3. Query and visualize these metrics using the Prometheus UI

From here, you can:

  • Create more complex queries using PromQL (Prometheus Query Language)
  • Set up alerting based on your metrics
  • Integrate with visualization tools like Grafana for more advanced dashboards

Remember, this is just the beginning. As your application grows, you can expand your monitoring setup to include more detailed metrics, additional services, and more sophisticated alerting and visualization configurations.
So, whether you’re a tech enthusiast, a professional, or just someone who wants to learn more, I invite you to follow me on this journey. Subscribe to my blog and follow me on social media to stay in the loop and never miss a post.

Together, let’s explore the exciting world of technology and all it offers. I can’t wait to connect with you!”

Connect me on Social Media: https://linktr.ee/mdshamsfiroz

Happy coding! Happy learning!

Happy monitoring!

--

--

mdshamsfiroz
mdshamsfiroz

Written by mdshamsfiroz

Trying to learn tool by putting heart inside to make something

No responses yet