Spring Boot 2.0 Prometheus Backward Compatibility

旧巷老猫 提交于 2020-01-02 04:10:08

问题


I am migrating to Spring Boot 2.0 and I am having issues with my Prometheus Metrics.

I know that MicroMeter is the new way of doing stuff, which is not as crisp as the Prometheus libs but OK.

My issue is that If I do not want to change my metrics now I cannot upgrade to Spring Boot 2.0. Am I right?

I tried the following:

Trial no 1

  • Keep my implementations "as is"
  • add the new dependency io.micrometer:micrometer-registry-prometheus:1.0.2 to my app (actuator is already in there)
  • change stuff in application.properties to get access to the endpoint actuator/prometheus

=> My Counters and Gauges from the past got ignored. OK I understand that from a technical point of view.

Trial no 2

  • Keep my implementations "as is"
  • add the "old" 'io.prometheus' dependencies and remove the micrometer dependency
  • change stuff in application.properties to get access to the endpoint actuator/prometheus

=> Now I get the following excpetion

Caused by: java.lang.ClassNotFoundException: org.springframework.boot.actuate.endpoint.AbstractEndpoint
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_161]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_161]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) ~[na:1.8.0_161]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_161]
... 37 common frames omitted

So my question is: Is there a "soft migration" way which works?


回答1:


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.




回答2:


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>


来源:https://stackoverflow.com/questions/49570812/spring-boot-2-0-prometheus-backward-compatibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!