Home  Devops   Prometheus ...

Prometheus example to monitor app

Prometheus is an open-source systems monitoring and alerting toolkit, which is particularly well-suited for monitoring dynamic cloud environments like Kubernetes. Below is a simple example of setting up Prometheus to monitor a basic application.

Prerequisites

  1. Docker: Ensure Docker is installed on your machine.
  2. Docker Compose: Ensure Docker Compose is installed.

Step-by-Step Example

1. Create a Directory for Your Setup

Create a directory for your Prometheus setup and navigate into it.

mkdir prometheus-setup
cd prometheus-setup

2. Create a Prometheus Configuration File

Create a file named prometheus.yml with the following content:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'example-app'
    static_configs:
      - targets: ['example-app:8080']

This configuration tells Prometheus to scrape metrics from itself and from an example application running on port 8080.

3. Create a Simple Application with Metrics

Create a directory for your example application:

mkdir example-app
cd example-app

Create a simple Node.js application that exposes metrics. Create a package.json file:

{
  "name": "example-app",
  "version": "1.0.0",
  "description": "An example application to be monitored by Prometheus",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "^4.17.1",
    "prom-client": "^13.2.0"
  }
}

Create an app.js file:

const express = require('express');
const client = require('prom-client');
const app = express();
const collectDefaultMetrics = client.collectDefaultMetrics;

collectDefaultMetrics();

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.get('/metrics', (req, res) => {
  res.set('Content-Type', client.register.contentType);
  res.end(client.register.metrics());
});

app.listen(8080, () => {
  console.log('Example app listening on port 8080!');
});

Install the dependencies:

npm install

4. Create a Docker Compose File

Create a docker-compose.yml file in the prometheus-setup directory:

version: '3'

services:
  prometheus:
    image: prom/prometheus
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    ports:
      - "9090:9090"

  example-app:
    build: ./example-app
    ports:
      - "8080:8080"

5. Build and Run the Setup

From the prometheus-setup directory, build and run the setup using Docker Compose:

docker-compose up --build

This will start both Prometheus and the example application. Prometheus will begin scraping metrics from the example application.

6. Access Prometheus

Open your browser and go to http://localhost:9090. You should see the Prometheus web interface.

7. Verify Metrics

In the Prometheus web interface, go to the "Targets" page (http://localhost:9090/targets). You should see the prometheus and example-app targets listed and marked as "UP".

You can also go to the "Graph" page and query for some default metrics, such as up, which should show a graph indicating that both Prometheus and the example application are up and being scraped.

Published on: Jul 08, 2024, 05:11 AM  
 

Comments

Add your comment