Hands on Coding: Spring Metrics with Prometheus for Beginner
1. Overview
Welcome to our hands-on guide where we'll delve into the world of monitoring Spring Boot applications using Prometheus and Grafana. In today's fast-paced digital landscape, ensuring the smooth operation and performance of our applications is paramount. With the powerful combination of Prometheus and Grafana, we can gather insightful metrics and visualize them in a meaningful way, allowing us to monitor and optimize our Spring Boot applications effectively.
In this guide, we'll walk through the process step-by-step, covering everything you need to know to set up basic monitoring for your Spring Boot 3 application with Docker.
2. Spring Boot 3 Application
The Spring Boot project contains the basic configuration needed for demonstration.
2.1 Dependencies
We need the actuator to expose the Spring metrics and use the micrometer registry to convert it to Prometheus.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency>
2.2 Configuration
In this section, we allow health and Prometheus endpoints from the actuator. By default, the actuator endpoint is/actuator, which lists all the enabled endpoints.
logging: level: root: info server: port: 8080 spring: application: name: lab-spring-prometheus management: endpoints: web: exposure: include: health, prometheus endpoint: health: show-details: always prometheus: metrics: export: enabled: true
3. Prometheus
In Prometheus configuration, we need to specify the metrics path and set some labels that we will need in our Grafana dashboard later.
Note: If you want to scrape data from a Spring Boot app that runs on your IDE, you must change the targets to host.docker.internal. Don't forget to update the extra_hosts in the docker-compose file as well.
scrape_configs: - job_name: 'lab-spring-prometheus' metrics_path: '/actuator/prometheus' scrape_interval: 5s static_configs: # - targets: [ 'host.docker.internal:8080' ] - targets: [ 'spring-app:8080' ] labels: namespace: czetsuyatech application: 'lab-spring-prometheus'
4. Grafana
4.1 Spring Boot Statistics
If you want a Spring Boot statistics dashboard out of the box you may check the following plugin in Grafana's marketplace. Or simply, import them.
- 19004 - https://grafana.com/grafana/dashboards/19004-spring-boot-statistics/
- 11378 - https://grafana.com/grafana/dashboards/11378-justai-system-monitor/
For Grafana, we can start with a basic data source configuration. This points to the Prometheus URL we define in docker-compose. Note that you can also do this in the Grafana user interface.
Grafana's default username and password is "admin".
apiVersion: 1 datasources: - name: Lab Prometheus type: prometheus access: proxy url: http://prometheus:9090 isDefault: true
5. Docker Configuration
5.1 Dockerfile
We are loading the Spring Boot jar to our docker container. Make sure to build the project first in your IDE, so that the jar is generated.
FROM eclipse-temurin:17-jdk-alpine VOLUME /tmp COPY ./target/*.jar app.jar ENTRYPOINT ["java","-jar","/app.jar"]
5.2 Docker Compose File
This configuration runs our Spring boot app, Prometheus, and Grafana in docker with a common network. Thus, each service can access each other using the service name. If you want to access a non-docker component, let's say the Spring Boot app, you need to do the necessary configuration with the host.
version: '3.8' networks: backend: services: spring-app: build: dockerfile: docker/Dockerfile context: ../ ports: - "8080:8080" networks: - backend prometheus: image: prom/prometheus:v2.51.1 container_name: lab-prometheus restart: no ports: - "9090:9090" volumes: - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml networks: - backend # extra_hosts: # - 'host.docker.internal:host-gateway' grafana: image: grafana/grafana container_name: lab-grafana restart: no ports: - "3000:3000" volumes: - ./grafana/datasources:/etc/grafana/provisioning/datasources networks: - backend
To run, simply go to docker folder of this project and execute: docker-compose up --build
6. GitHub Repository
The code is available in GitHub including the docker-compose file.
Post a Comment