Artikel ini membahas seluruh siklus bekerja dengan metrik. Pertama, konfigurasikan aplikasi Anda untuk mengirimkan metrik. Mengonfigurasi Prometheus untuk mengumpulkan dan menyimpan metrik. Mengonfigurasi Grafana untuk menampilkan metrik yang dikumpulkan.
Persyaratan
1. Maven , tapi bisa diadaptasi untuk builder lain.
2. Docker dan buruh pelabuhan-menulis untuk menjalankan Prometheus dan Grafana.
3. Spring boot 2 dan lebih tinggi, namun, ada port untuk versi sebelumnya .
pengantar
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']
Dalam contoh yang disiapkan, dua dasbor populer ditambahkan segera setelah peluncuran.
1. JVM
Kesimpulan
1. Mengkonfigurasi aplikasi yang menyediakan metrik.
2. Koleksi metrik yang dikonfigurasi menggunakan Prometheus.
3. Dashboard yang dikonfigurasi untuk memvisualisasikan hasil di Grafana.