Monitoring Spring Boot Application with Prometheus and Grafana

Introduction

Creating a Spring Boot Application

  • Spring Boot Actuator (Ops)
  • Prometheus (Observability)
  • Spring Web (Optional: only to create a simple REST controller.)
management:
endpoints:
web:
exposure:
include:
- prometheus
@RestController
@SpringBootApplication
public class PrometheusIntegrationApplication {

final static Logger logger = LoggerFactory.getLogger(PrometheusIntegrationApplication.class);

public static void main(String[] args) {
SpringApplication.run(PrometheusIntegrationApplication.class, args);
}

@GetMapping("/something")
public ResponseEntity<String> createLogs() {
logger.warn("Just checking");
return ResponseEntity.ok().body("All Ok");
}
http://localhost:8080/actuator/prometheus

Understanding the Metics data

jvm_memory_used_bytes{area="heap",id="G1 Survivor Space",} 1005592.0

Configuring Prometheus

scrape_configs:
- job_name: 'Spring Boot Application input'
metrics_path: '/actuator/prometheus'
scrape_interval: 2s
static_configs:
- targets: ['localhost:8000']
labels:
application: "My Spring Boot Application"
services:
prometheus:
image: prom/prometheus:v2.35.0
network_mode: host
container_name: prometheus
restart: unless-stopped
volumes:
- ./data/prometheus/config:/etc/prometheus/
command:
- "--config.file=/etc/prometheus/prometheus.yaml"

Visualizing Metrics in Grafana

grafana:
image: grafana/grafana-oss:8.5.2
pull_policy: always
network_mode: host
container_name: grafana
restart: unless-stopped
links:
- prometheus:prometheus
volumes:
- ./data/grafana:/var/lib/grafana
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
- GF_SERVER_DOMAIN=localhost

Configuring Grafana Data Source

Creating Grafana Dashboard

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store