spring boot + prometheus自定义gauge类型指标

1、添加依赖

<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>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

2、配置文件中新增prometheus配置

# spring-boot-actuator配置
management:
  endpoints.web.exposure.include: '*'
  metrics:
    export:
      prometheus:
        enabled: ${METRICS_ENABLED:true}  #控制是否开启Prometheus
    distribution:
      percentiles-histogram:
        http.server.requests: false
      minimum-expected-value:
        http.server.requests: 20ms
      maximum-expected-value:
        http.server.requests: 200ms
      slo:
        http.server.requests: 50ms,100ms,150ms
    tags:
      tag: ${tagValue:tagValue}

应用启动成功后,可以通过访问ip:port/actuator/prometheus端点查看支持的指标
3、自定义gauge指标

private final Map<String, AtomicInteger> statusCodes = new HashMap<>();
.
.
.
private void createOrUpdateMetric(String healthType, int value) {
    statusCodes.put(healthType, new AtomicInteger(value));
	Set<Tag> tagList = new HashSet<>();
	tagList.add(Tag.of("tag1", "tag1value"));
	tagList.add(Tag.of("tag2", "tag2value"));

    Gauge.builder(HEALTH, statusCodes, statusCodes -> statusCodes.get(healthType).get())
    	 .tags("tag","tagValue")
    	 .tags(tagList)
         .tags(Tags.of(Tag.of(HEALTH_TYPE, healthType)))
         .description(HEALTH_DESCRIPTION + healthType)
         .register(meterRegistry);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值