Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to profile complex http request handling in Spring Boot?

I have complex @RestController method, something like this:

@PostMapping("{id}")
@PreAuthorize("hasRole('ADMIN')")
@Transactional
public Response handleRequest(@PathVariable("id") long id, @RequestBody @Valid Request request) {
    return service.handleRequest(id, request);
}

Our request handling is quite slow so we want to check how much time is spent on particular request handling tasks. Unfortunately lot of things are done outside of my method, like:

  • deserializing request
  • validating
  • permission checks
  • starting and ending transaction
  • serializing response

Is there way to simply measure all those parts? Maybe set of loggers that receive trace messages so I can pull timestamps at the end of each step?

The only way I see to do it now is change that method to accept HttpServletRequest and HttpServletResponse and do those parts inside method body. But that way I will lose lot of Spring Boot benefits.

like image 780
Sankozi Avatar asked Feb 12 '19 14:02

Sankozi


2 Answers

you can also check a tuto for adding a custom metrics for actuator, but it seems a little bit complicate (but you'll you have to code your own metrics bean and inject it in your code, override objectMapper for mapping, etc... )

or maybe activate logging info on jackson,spring-security, javax.validation for checking the time in the log for each operation, but not very precise

like image 100
sam3131 Avatar answered Oct 05 '22 11:10

sam3131


what you exactly need is Java Thread Profiler which will tell you what is exactly going wrong and for it you can use any APM Tools and my favourite is GLOWROOT .which I have used in the similar scenarios to measure the performance of APIs and identify the slow traces which will clearly tell you which method is taking time and you can see the entire trace starting from method call to all the methods called inside and even identify slow queries if there are any . Hope this helps

Ths site: https://glowroot.org/

example trace :

https://demo.glowroot.org/transaction/thread-profile?transaction-type=Web&transaction-name=%2Fhot-sauces

like image 38
Manoj Krishna Avatar answered Oct 05 '22 13:10

Manoj Krishna