springboot2-integration-prometheus

springboot2集成prometheus并配置grafana图表

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
<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>

application.properties

1
2
3
management.endpoints.web.exposure.include=*
spring.application.name=springboot_prometheus2
management.metrics.tags.application=${spring.application.name}

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import io.micrometer.core.instrument.MeterRegistry;

@SpringBootApplication
public class Springboot2PrometheusApplication {

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

@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
}

springboot2启用/actuator/prometheus端点,供Prometheus来抓取指标。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/actuator/prometheus

# HELP process_uptime_seconds The uptime of the Java virtual machine
# TYPE process_uptime_seconds gauge
process_uptime_seconds{application="springboot_prometheus",} 1528.62
# HELP jvm_gc_memory_allocated_bytes_total Incremented for an increase in the size of the young generation memory pool after one GC to before the next
# TYPE jvm_gc_memory_allocated_bytes_total counter
jvm_gc_memory_allocated_bytes_total{application="springboot_prometheus",} 1.27722912E8
# HELP system_cpu_count The number of processors available to the Java virtual machine
# TYPE system_cpu_count gauge
system_cpu_count{application="springboot_prometheus",} 8.0
# HELP tomcat_global_error_total
# TYPE tomcat_global_error_total counter
tomcat_global_error_total{application="springboot_prometheus",name="http-nio-8081",} 2.0
# HELP tomcat_threads_busy_threads
# TYPE tomcat_threads_busy_threads gauge
tomcat_threads_busy_threads{application="springboot_prometheus",name="http-nio-8081",} 1.0
# HELP tomcat_threads_current_threads
# TYPE tomcat_threads_current_threads gauge
tomcat_threads_current_threads{application="springboot_prometheus",name="http-nio-8081",} 10.0
# HELP jvm_memory_max_bytes The maximum amount of memory in bytes that can be used for memory management
# TYPE jvm_memory_max_bytes gauge
jvm_memory_max_bytes{application="springboot_prometheus",area="heap",id="PS Survivor Space",} 1.048576E7
jvm_memory_max_bytes{application="springboot_prometheus",area="heap",id="PS Old Gen",} 2.776629248E9
jvm_memory_max_bytes{application="springboot_prometheus",area="nonheap",id="Code Cache",} 2.5165824E8
jvm_memory_max_bytes{application="springboot_prometheus",area="heap",id="PS Eden Space",} 1.367867392E9
jvm_memory_max_bytes{application="springboot_prometheus",area="nonheap",id="Compressed Class Space",} 1.073741824E9
jvm_memory_max_bytes{application="springboot_prometheus",area="nonheap",id="Metaspace",} -1.0

配置grafana曲线

grafana模板:https://grafana.com/dashboards/4701
在Grafana内点击如图所示import按钮,在如图所示位置填写4701,然后点击load。

源码

https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_prometheus
https://github.com/vacual/spring-boot-examples/tree/master/spring-boot-micrometer-vacual

启动程序

1
2
3
ps -ef|grep java | grep -v grep 
root 1133 1 0 May24 ? 01:12:30 java -jar spring-boot-prometheus-1.0.jar
root 8847 1 1 14:20 ? 00:00:29 java -jar -Dserver.port=8081 spring-boot-micrometer1-2.0.0.jar

prometheus配置

1
2
3
4
5
6

- job_name: 'springboot_prometheus'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['127.0.0.1:8081']

参考

聊聊springboot2的micrometer
SpringBoot使用prometheus监控