Set up Prometheus: Install Prometheus, configure it to scrape metrics from a target service (e.g., a Node.js application), and verify that metrics are

Set up Prometheus: Install Prometheus, configure it to scrape metrics from a target service (e.g., a Node.js application), and verify that metrics are

Setting up Prometheus involves several steps, including installation, configuration, and verification. Here's a step-by-step guide to install Prometheus, configure it to scrape metrics from a Node.js application, and verify that the metrics are being collected.

Step 1: Install Prometheus

  1. Download Prometheus: Go to the Prometheus download page and download the latest version for your operating system. For example, you can use wget or curl for Linux:

     wget https://github.com/prometheus/prometheus/releases/latest/download/prometheus-<version>.linux-amd64.tar.gz
    

    Replace <version> with the latest version number.

  2. Extract the downloaded file:

     tar xvf prometheus-<version>.linux-amd64.tar.gz
     cd prometheus-<version>.linux-amd64
    
  3. Start Prometheus: You can start Prometheus with the default configuration by running:

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

Step 2: Set Up a Node.js Application with Metrics

  1. Create a simple Node.js application:

    If you don’t have a Node.js application, create a new one. Ensure you have Node.js and npm installed. Create a new directory and initialize a new project:

     mkdir node-app
     cd node-app
     npm init -y
    
  2. Install the prom-client library:

    This library helps expose metrics to Prometheus.

     npm install express prom-client
    
  3. Create a simple Express server that exposes metrics:

    Create a file named app.js with the following content:

     const express = require('express');
     const { register, collectDefaultMetrics } = require('prom-client');
    
     const app = express();
     const port = 3000;
    
     // Collect default metrics
     collectDefaultMetrics();
    
     app.get('/', (req, res) => {
         res.send('Hello World!');
     });
    
     // Expose the metrics endpoint
     app.get('/metrics', async (req, res) => {
         res.set('Content-Type', register.contentType);
         res.end(await register.metrics());
     });
    
     app.listen(port, () => {
         console.log(`Server running at http://localhost:${port}`);
     });
    
  4. Start your Node.js application:

     node app.js
    

Step 3: Configure Prometheus to Scrape Metrics

  1. Edit the prometheus.yml file:

    Open the prometheus.yml file in the Prometheus directory and add a job configuration to scrape metrics from your Node.js application. It should look like this:

     global:
       scrape_interval: 15s  # Default scrape interval
    
     scrape_configs:
       - job_name: 'node-app'
         static_configs:
           - targets: ['localhost:3000']
    

    This configuration tells Prometheus to scrape metrics from the Node.js application running on localhost at port 3000.

  2. Restart Prometheus:

    If Prometheus is still running, stop it (Ctrl + C) and start it again:

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

Step 4: Verify Metrics Collection

  1. Access the Prometheus UI: Open your web browser and go to http://localhost:9090.

  2. Check the targets: In the Prometheus UI, navigate to the “Status” -> “Targets” page. You should see your node-app listed there. It should show the last scrape time and any errors.

  3. Query metrics: Navigate to the “Graph” page in the Prometheus UI. You can type in metrics like nodejs_http_requests_total or process_cpu_seconds_total to see if metrics are being collected.

Conclusion

You have now successfully set up Prometheus, configured it to scrape metrics from a Node.js application, and verified that metrics are being collected.