Skip to content

Latest commit

 

History

History
173 lines (133 loc) · 7.26 KB

README.md

File metadata and controls

173 lines (133 loc) · 7.26 KB

Study about Spring Boot with Prometheus and Grafana

Kotlin icon Spring icon Spring Boot icon Docker icon Docker icon

⤴️ Table of Contents

❓ About

Studies about Spring Boot with Prometheus and Grafana from the article Springboot App monitoring with Grafana & Prometheus by Vishnu M V.

Added some business metrics to Grafana and learned how to provision dashboards and datasources.

Back to Top

📊 System Diagrams

graph LR
    subgraph Linux
        A[Application]
    end

    subgraph Windows/MacOSX
        B[Application]
    end

    subgraph docker
        P[Prometheus] -- " study-springboot-grafana-prometheus-app-1:8080 " --> A
        P[Prometheus] -- " host.docker.internal:8080 " --> B
        G[Grafana] -- " prometheus:9090 " --> P
    end
Loading

Back to Top

👩‍🏫 Learnings

Very easy to enable it with spring boot actuator and there are dashboards ready for use.

Some difficulties getting it to work cross-platform. How docker networking works differently between Windows and Linux. There is no direct way to access the host IP address from within the container, making prometheus difficult to extract information from the application by Prometheus scraper.

More investigation would be needed to make the prometheus scraper work correctly on various OS platforms using only one target. For now, I have added both URLs to be discarded and accept that one of them will fail.

Back to Top

🏃 How to Run

Pre-requisites:

Initialize the Prometheus and Grafana with Docker Compose with the command:

docker compose up
Component Url User Password
Prometheus http://localhost:9090 - -
Grafana http://localhost:3000 admin admin

Run with Maven

mvn spring-boot:run

Run with Docker

Linux/MacOSX:

docker run --rm \
  -p 8080:8080 \
  -w /app \
  -v ./src:/app/src \
  -v ./pom.xml:/app/pom.xml \
  -v data:/root/.m2 \
  --network=study-springboot-grafana-prometheus_monitoring \
  --name=/study-springboot-grafana-prometheus-app-1 \
  --label com.docker.compose.project=study-springboot-grafana-prometheus \
  maven:3.8.8-eclipse-temurin-21-alpine mvn spring-boot:run

Windows (PowerShell):

$srcPath = (Resolve-Path 'src').Path
$pomPath = (Resolve-Path 'pom.xml').Path

docker run --rm `
  -p 8080:8080 `
  -w /app `
  -v ${srcPath}:/app/src `
  -v ${pomPath}:/app/pom.xml `
  -v data:/root/.m2 `
  --network=study-springboot-grafana-prometheus_monitoring `
  --name=/study-springboot-grafana-prometheus-app-1 `
  --label com.docker.compose.project=study-springboot-grafana-prometheus `
  maven:3.8.8-eclipse-temurin-21-alpine mvn spring-boot:run

Back to Top

How to Test

Some endpoints to test the business metrics in Grafana.

Action URI Description
Flip a Coin GET http://localhost:8080/api/flipCoin Returns head or tail value and register a total count metric and another metric telling how many tails or heads

And below the Grafana Dashboards

Dashboard URL Description
Business Metrics http://localhost:3000/d/d5ae9a0e-e1b5-4e87-8a0a-d0720cc17a88/business-metrics Metrics added inside the application
JVM (Micrometer) http://localhost:3000/d/d815b226-48af-4ecd-89de-50782a2d0ada/jvm-micrometer Metrics related to the JVM
Spring Boot 3.x Statistics http://localhost:3000/d/spring_boot_21_2/spring-boot-3-x-statistics Metrics related to Spring Boot

Back to Top

📖 References

Back to Top