1

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!

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
  • 1
    Does this answer your question? [How to measure service methods using spring boot 2 and micrometer](https://stackoverflow.com/questions/48704789/how-to-measure-service-methods-using-spring-boot-2-and-micrometer) – Chin Huang Feb 03 '22 at 17:38
  • The Micrometer developer said [_never count something you can time_](https://github.com/micrometer-metrics/micrometer/issues/805#issuecomment-420725434). Timers include a count. – Chin Huang Feb 03 '22 at 17:52

2 Answers2

4

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

Kerkhofsd
  • 269
  • 1
  • 9
  • I generated all the stuffs by Maven OpenAPI generator plugin using the Delegate Pattern. It generates the API interface, implemented by the Controller, and the latter one uses my delegate where I placed the meter annotations. Can they work here? – Antonio Petricca Feb 03 '22 at 19:10
2

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.

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74