简体   繁体   中英

Springboot: micrometer metrics not showing up

I am writing my first spring boot (2.6.3) application.

Here the relevant dependencies I am using:

<dependencyManagement>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.6.3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-to-slf4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

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

I exposed by actuator all the features:

management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: '*'
  info:
    env:
      enabled: true

So I can see all the endpoints listed by the url /actuator .

Now, I added some annotations like @Counted and @Timed to some methods, I invoked them, but they don't show up into /actuator/metrics .

How could I solved that issue?

Thank you so much in advance!

Are you aware that @Timed annotations are supported on @Controller classes and @RequestMapping methods only?

https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.metrics.supported.spring-mvc

I am not sure @Counted is supported by default.

EDIT: The @Counted annotation doesn't work by default. To make it work, you'll need to add an CountedAspect to your context.

See: https://github.com/micrometer-metrics/micrometer/blob/main/micrometer-core/src/main/java/io/micrometer/core/aop/CountedAspect.java

Also, there's an open issue for @Counter auto-configuration: https://github.com/spring-projects/spring-boot/issues/17260

Solved by adding the following:

@Bean
CountedAspect countedAspect(MeterRegistry registry) {
    return new CountedAspect(registry);
}
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.13</version>
</dependency>

It works with OpenAPI delegates too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM