Spring Boot 普罗米修斯千分尺 - 仪表未更新

twe*_*sat 5 spring-boot prometheus micrometer

我有一个SpringBoot 2.2.4.RELEASE像 RestRepostory 这样的

import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;

@RestController
public class MyController {

    private MeterRegistry meterRegistry;

    public MyController(MeterRegistry meterRegistry) {
        this.meterRegistry = meterRegistry;
    }

    private Gauge myGauge;
    private Integer myInteger = 0;

    @PostConstruct
    private void init() {
        myGauge = Gauge.builder("my.gauge", myInteger, Integer::intValue)
                .register(meterRegistry);
    }

    @GetMapping("/count")
    public void count() {
        myInteger = 5;
    }

}
Run Code Online (Sandbox Code Playgroud)

应用程序启动后,访问http://localhost:8082/actuator/prometheus可以看到

# HELP my_gauge  
# TYPE my_gauge gauge
my_gauge 0.0
Run Code Online (Sandbox Code Playgroud)

但是访问http://localhost:8082/count/后,该值仍然是 0.0

有什么问题 ?我也不明白构建器函数的第三个参数。是这个原因吗?

我也尝试过使用计数器。当我使用计数函数增加它时,它工作得很好。

vic*_*ofd 0

我知道这是一个老问题,可能您已经解决或尝试了另一种解决方案来解决您的问题。我刚刚遇到了同样的问题,最终,这是对仪表方法如何工作的误解。第三/第四个,取决于规范方法的构造函数、参数,接收一个惰性函数。

我在 Prometheus Google Group 上问了类似的问题,同时找到了解决方案。

以下是我在 API 上实现的解决方案的片段:

private final Map<String, AtomicInteger> statusCodes = new HashMap<>();

.
.
.

private void createOrUpdateMetric(String healthType, Status status) {
    statusCodes.put(healthType, new AtomicInteger(healthToCode(status)));

    Gauge.builder(HEALTH, statusCodes, statusCodes -> statusCodes.get(healthType).get())
         .tags(Tags.of(Tag.of(HEALTH_TYPE, healthType)))
         .description(HEALTH_DESCRIPTION + healthType)
         .register(meterRegistry);
}
Run Code Online (Sandbox Code Playgroud)

是我在 Prometheus Groups 上提出的问题