1

I have to measure the full time of the request, that comes into controller and then the async method is executed of some service:

@RestController
public class HelloController {

    @Autowired
    private SomeService someService;


    @GetMapping("/async")
    @Timed(value = "async.method.time", description = "Time taken to execute async method")
    public void async() throws InterruptedException {
        Thread.sleep(2000);

        someService.executeAsync();
    }
}

@Service
public class SomeService {

    @Async
    public void executeAsync() throws InterruptedException {
        Thread.sleep(2000);
        System.out.println("Async execution done!");
    }
}

Actually, if I put the @Timed annotation above the async service method, then it will be measured correct, but I would like to measure the time with controller method too. How can I achieve this using Micrometer?

Thanks in advance!

Georgii Lvov
  • 1,771
  • 1
  • 3
  • 17

1 Answers1

0

You can use the @NewSpan annotation. This way will have the full controller execution time as well as break down of the @Async service execution time.

NOTE: With Spring Boot 3.1.0 and Micrometer 1.10.0+, you can follow this blog which guides you with the minimum setup required to enable Micrometer annotations with Spring Boot.

Amith Kumar
  • 4,400
  • 1
  • 21
  • 28