micrometer: @Timed not working with Spring Boot 2.0.0.M7

Hi,

I created a simple project with these dependencies

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

And create a simple Controller

@RestController
public class DemoController {

    private Random random = new Random();

    @GetMapping("/")
    @Timed
    public String calculateTimed() throws InterruptedException {
        Thread.sleep(random.nextInt(1000));
        return "This is a test";
    }
}

And I am expected to see the timed metric when I called /actuator/metrics but I didn’t get anything

Here is the result to http://localhost:9999/actuator/metrics

{
names: [
"jvm.buffer.memory.used",
"jvm.memory.used",
"jvm.buffer.count",
"logback.events",
"process.uptime",
"jvm.memory.committed",
"system.load.average.1m",
"jvm.buffer.total.capacity",
"jvm.memory.max",
"system.cpu.count",
"http.server.requests",
"process.start.time"
]
}

Do I need to add an additional dependency?

Thanks

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 16 (4 by maintainers)

Most upvoted comments

@seedily But it still doesn’t working. I tired using @Timed on arbitrary methods after configuring TimedAspect but isn’t really working

@Bean TimedAspect should be created, below is the offical guidence : https://micrometer.io/docs/concepts#_the_code_timed_code_annotation

@patpatpat123 commenting on this old closed issue is not the best way to get support. See https://github.com/spring-projects/spring-boot/issues/18680; the feature you’re trying to use is not in Spring Boot yet.

@jesperdj I don’t think that the following is required.

@Bean
public MeterRegistryCustomizer<MeterRegistry> meterRegistryCustomizer() {
    return registry -> registry.counter("calendar.create");
}

When you run the method annotated with @Timed once, you will have the name in the list.

To make this work I had to do the following. I’m creating a counter, but for a timer it’s similar.

First, add a dependency to org.aspectj:aspectjweaver to my Spring Boot application:

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
</dependency>

Then, create the counter by creating a MeterRegistryCustomizer bean (for example in the Spring Boot application class; you can also do this in any other @Configuration-annotated class):

@Bean
public MeterRegistryCustomizer<MeterRegistry> meterRegistryCustomizer() {
    return registry -> registry.counter("calendar.create");
}

In the same class, create a CountedAspect bean. This registers an AspectJ aspect that will handle the @Counted annotation:

@Bean
public CountedAspect countedAspect(MeterRegistry meterRegistry) {
    return new CountedAspect(meterRegistry);
}

If you want to use the @Timer annotation, you would have to create a TimedAspect bean in the same way:

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

Then you can use the @Counted and @Timed annotations, for example on your controller methods:

@RestController
public class CalendarController {

    // ...

    @PostMapping("/items")
    @Counted("calendar.create")
    public CalendarItem create(@RequestBody CalendarItem calendarItem) {
        // ...
    }
}

@seedily But it still doesn’t working. I tried using @timed on arbitrary methods after configuring TimedAspect but isn’t really working

was this really fixed? I have followed the official documentation and I cannot see TimedAspect being invoked.