0

I am developing the Spring Boot API's.

Now comes the monitoring part. I want to add monitoring to it. So I used actuator, fetch data(like gc, memory etc) from it & plot it on Grafana.

Now comes the response time of API's. Actuator guage just return the last API Hit. So basically how do I calculate the response time of every API for evry hit. I mean where I should place

long startTime = System.currentTimeMillis(); 

and

long endTime = System.currentTimeMillis(); long diff = endTime - startTime; 

I have tried placing it on controller, 1st line of controller & last line of controller but the result is different when I am comparing it with Guage.

How should I correctly measure it?

2
  • what you are looking for is a histogram, Spring boot acuator doesn't have support for histogram, But Spring Boot Actuator can work with Dropwizard's metrics library(metrics.dropwizard.io/3.2.2), which has histogram support, check it out. Commented May 16, 2017 at 9:58
  • What I am more curious abt that is there any way I can get that data manuyally? Response time with every API Hit? Commented May 16, 2017 at 10:02

1 Answer 1

1

You can try and have an AOP "around" advice around the DispatcherServlet#service method maybe?

Untested:

@Aspect @Component public class AroundExample { @Around("org.springframework.web.servlet.DispatcherServlet.service()") public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { // start stopwatch Object retVal = pjp.proceed(); // stop stopwatch return retVal; } } 

Don't forget to add the aop starter in your POM:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> 

That would give you a pretty close timing of the whole request invocation, including all the stuff like Spring interceptors, handlers, controllers, etc.

Sign up to request clarification or add additional context in comments.

3 Comments

public class XYZ { @Around("execution(* org.springframework.web.servlet.DispatcherServlet.service(..))") public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { // start stopwatch System.out.println("Before"); Object retVal = pjp.proceed(); // stop stopwatch System.out.println("Me here"); return retVal; } } This code is executing but not printing anything? What Am i Missing?
What do you mean by "executing, but not printing anything"? Did you set a breakpoint in the debugger?
I mean no compile errors are there but still even i after i place prints, nothing is printed. I mean code is compiling but not printing anything

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.