0
点赞
收藏
分享

微信扫一扫

spring boot整合Prometheus采集自定义指标

小编 2022-03-12 阅读 115

前言

介绍如何为springboot应用自定义Prometheus指标,如何让Prometheus采集springboot应用的自定义指标

一、springboot 引入Prometheus

1.加入prometheus依赖

compile "io.prometheus:simpleclient_hotspot:0.0.24"

compile "io.micrometer:micrometer-registry-prometheus:1.0.5"

compile "io.micrometer:micrometer-spring-legacy:1.0.5"

2. application.yml配置文件开启springboot 的actuator

management.port=8081

management.context-path=/actuator

management.security.enabled=false

3.创建Prometheus metrics bean对象,管理指标

@Bean

MeterRegistryCustomizer meterRegistryCustomizer(MeterRegistry meterRegistry) {

return meterRegistry1 -> {

meterRegistry.config()

.commonTags("application", "k8s-demo");//定义应用名

};

}

4.定义一个gauge指标,统计文件大小,标签为文件名,文件路径,创建时间

 @Autowired
    private PrometheusMeterRegistry meterRegistry;

    @Override
    public void afterPropertiesSet() throws Exception {

        CollectorRegistry prometheusRegistry = meterRegistry.getPrometheusRegistry();
        Gauge fileSize= Gauge.build().name("file_log_size").labelNames("fileName","path","date").help("File size").register();
       
   fileSize.labels(".m2","C:\\Users\\.m2","2021-12-13").set(256);
        fileSize.labels("jdks","C:\\Users\\.m2\\jdks","2021-12-13").set(1024);
        
        prometheusRegistry.register(fileSize);
    }

5.启动springboot程勋,访问http://localhost:8081/actuator/prometheus 可以看到自定义的指标

二、 Prometheus采集应用指标

1.Prometheus 配置文件增加如下配置, .\prometheus.exe  --config.file=prometheus.yml 启动windows Prometheus

 - job_name: 'log_file_size'
    metrics_path: 'actuator/prometheus'
    static_configs:
     - targets: ['localhost:8081'] 

2.访问Prometheus http://localhost:9090/ 查看指标已经被Prometheus采集到

举报

相关推荐

0 条评论