This article covers the full cycle of working with metrics. First, configure your application to deliver metrics. Configuring Prometheus to collect and store metrics. Configuring Grafana to display collected metrics.
Requirements
1. Maven , but can be adapted for other builders.
2. Docker and docker-compose to run Prometheus and Grafana.
3. Spring boot 2 and higher, however, there are ports for previous versions .
Introduction
Spring boot 2 Micrometer . . Prometheus .
[ spring initilizr]
spring boot .
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
actuator
Spring boot , . .
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
-, actutor 8080 uri /actuator
- github actuator localhost:8080/actuator
, .
{
"_links":{
"self":{
"href":"http://localhost:8080/actuator",
"templated":false
},
"health-path":{
"href":"http://localhost:8080/actuator/health/{*path}",
"templated":true
},
"health":{
"href":"http://localhost:8080/actuator/health",
"templated":false
},
"info":{
"href":"http://localhost:8080/actuator/info",
"templated":false
}
}
}
actuator .
Micrometer - spring boot, , Prometheus .
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
application.yml
Prometheus actuator.
management: endpoints: web: exposure: include: health,prometheus metrics: export: prometheus: enabled: true distribution: percentiles-histogram: "[http.server.requests]": true
health,prometheus
actuator. Prometheus, `percentiles-histogram`, sla .
, http://localhost:8080/actuator/prometheus
.
# TYPE jvm_memory_committed_bytes gauge
jvm_memory_committed_bytes{area="heap",id="G1 Survivor Space",} 9437184.0
jvm_memory_committed_bytes{area="heap",id="G1 Old Gen",} 9.961472E7
jvm_memory_committed_bytes{area="nonheap",id="Metaspace",} 4.2254336E7
jvm_memory_committed_bytes{area="nonheap",id="CodeHeap 'non-nmethods'",} 2555904.0
# TYPE jvm_gc_live_data_size_bytes gauge
jvm_gc_live_data_size_bytes 0.0
# HELP tomcat_sessions_active_max_sessions
# TYPE tomcat_sessions_active_max_sessions gauge
tomcat_sessions_active_max_sessions 0.0
# HELP jvm_threads_live_threads The current number of live threads including both daemon and non-daemon threads
# TYPE jvm_threads_live_threads gauge
jvm_threads_live_threads 27.0
, jvm -.
docker-compose.yml
Prometheus Grafana.
version: '3.7'
services:
grafana:
build: './config/grafana'
ports:
- 3000:3000
volumes:
- ./grafana:/var/lib/grafana
environment:
- GF_SECURITY_ADMIN_USER=admin
- GF_SECURITY_ADMIN_PASSWORD=admin
networks:
monitoring:
aliases:
- grafana
prometheus:
image: prom/prometheus
ports:
- 9090:9090
volumes:
- ./config/prometheus.yml:/etc/prometheus/prometheus.yml
- ./prometheus:/prometheus
networks:
monitoring:
aliases:
- prometheus
networks:
monitoring:
, Grafana , , volume.
grafana , . Dockerfile
Grafana .
FROM grafana/grafana
ADD ./provisioning /etc/grafana/provisioning
prometheus.yml
.
.
scrape_configs:
- job_name: 'sample_monitoring'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['host.docker.internal:8080']
In the prepared example, two popular dashboards are added immediately upon launch.
1. JVM
Conclusion
1. Configured an application that provides metrics.
2. Configured collection of metrics using Prometheus.
3. Configured dashboards for visualizing the result in Grafana.