Unable to get micrometer metrics based on @Timed in springboot 3.2.2

222 Views Asked by At

I have a simple springboot REST API in which I want to check the time taken by different actions. I am using Springboot 3.2.2 and @Timed annotation provided by Micrometer. When I execute my rest endpoint I am unable to see the timer details either in /actuator/metrics endpoint or in logs.

Following are dependencies I have in pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.2</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-tracing-bridge-otel</artifactId>
    </dependency>
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-core</artifactId>
        <version>1.12.2</version>
    </dependency>
</dependencies>

Following is controller class

import io.micrometer.core.annotation.Timed;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Timed(value = "first.timer", description = "Time taken by GET method")
    @GetMapping("/apiEndpoint")
    public String helloWorld() {
        doBusiness();
        return "Hello world called!";
    }

    @Timed(value = "second.timer", description = "Time taken by business method")
    private void doBusiness()
    {
        //Does some long running task
    }
}

Below is application.yml entries-

management:
  metrics:
    distribution:
      percentiles-histogram:
        http:
          server:
            requests: true
  endpoint:
    metrics:
      enabled: true
  endpoints:
    web:
      exposure:
        include: health,info,metrics
  tracing:
    sampling:
      probability: 1.0
    propagation:
      type: w3c

logging:
  level:
    root: DEBUG

Do I need to set any specific parameter to enable this feature? I'd be grateful for any reference examples showcasing the usage of Micrometer annotations in the latest Spring Boot 3.2.2 documentation.

I went through Spring docs and unable to find anything specific.

1

There are 1 best solutions below

1
Ryuzaki L On BEST ANSWER

You have to create TimedAspect for @Timed annotation to publish the metrics as shown here, because spring boot uses aspect's for micrometer annotations

You can use @Timed annotation after configuring the TimedAspect Aspect provided by Micrometer. Add below lines of code in your @Configuration class

@Bean
public TimedAspect timedAspect(MeterRegistry registry) {
  return new TimedAspect(registry);
}