Spring Boot 2.0 Prometheus Backward Compatibility

大兔子大兔子 提交于 2019-12-05 08:09:06

To make Trial no 1 work, just add in the Prometheus default registry as a bean that Micrometer will be able to leverage.

@Bean
public CollectorRegistry collectorRegistry() {
    return CollectorRegistry.defaultRegistry;
}

Micrometer doesn't use the default registry by default since it doesn't allow un-registering of meters and can make unit testing quite difficult.

To make Trial no 2 work will require re-implementing the prometheus actuator endpoint, since that class changed drastically with SpringBoot 2. I wouldn't recommend that approach.

It took me very long to figure out the real problem, rather I had to debug Spring code to solve this. Here is the solution, you need to exclude "micrometer-core" from "spring-boot-starter-actuator" and add "micrometer-registry-prometheus" specifically, like below:

  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <exclusions>
            <exclusion>
                <groupId>io.micrometer</groupId>
                <artifactId>micrometer-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
   <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!