Dieser Artikel behandelt den gesamten Zyklus der Arbeit mit Metriken. Konfigurieren Sie zunächst Ihre Anwendung so, dass Metriken bereitgestellt werden. Konfigurieren von Prometheus zum Sammeln und Speichern von Metriken. Grafana so konfigurieren, dass gesammelte Metriken angezeigt werden.
Bedarf
1. Maven , kann aber für andere Bauherren angepasst werden.
2. Docker und Docker-Compose , um Prometheus und Grafana auszuführen.
3. Spring Boot 2 und höher Es gibt jedoch Ports für frühere Versionen .
Einführung
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']
Im vorbereiteten Beispiel werden zwei beliebte Dashboards sofort nach dem Start hinzugefügt.
1. JVM
Fazit
1. Konfigurierte eine Anwendung, die Metriken bereitstellt.
2. Konfigurierte Erfassung von Metriken mit Prometheus.
3. Konfigurierte Dashboards zur Visualisierung des Ergebnisses in Grafana.