0

I'm looking to track the response times of API calls. I then want to plot the response times of the calls( GET, PUT, POST DELETE) on a graph afterwards to compare the time differences.

This is what I'm currently doing to find the response time of a GET call but I'm not quite sure if it's right.

@RequestMapping(value="/Students", method = RequestMethod.GET)
    public ResponseEntity<List<Students>> getStudents()
    {
        long beginTime = System.currentTimeMillis();
        List<Students> students =  (List<Students>) repository.findAll();
        if(students.isEmpty())
        {
            return new ResponseEntity(HttpStatus.NO_CONTENT);
        }
        long responseTime = System.currentTimeMillis() - beginTime;
        logger.info("Response time for the call was "+responseTime);
        return new ResponseEntity(students, HttpStatus.OK);
    }

I believe I am returning the response time before I actually return the data to the client which is the whole point of this but I wouldn't be able to put it after the return statement as it would be unreachable code.

Are there any better ways of trying to track the times of the calls?

mvantastic
  • 47
  • 3
  • 9
  • Possible duplicate of [How to log time taken by Rest web service in Spring Boot?](https://stackoverflow.com/questions/42857658/how-to-log-time-taken-by-rest-web-service-in-spring-boot) – Vasan Oct 29 '17 at 00:19
  • 1
    Use the Spring Boot Actuator, which will record metrics for that. Which you can stream to something like Grafana (other options are of course available). – M. Deinum Oct 29 '17 at 20:00
  • Hi @M.Deinum , thanks for the reply. What would be the best way to export the metrics from spring so I can visualize it on grafana? I'm having trouble exporting it into influxdb and I'm not sure if that would be the best way to do it either – mvantastic Oct 30 '17 at 12:11
  • Take a look at one of the exporters provided by Spring Boot (also check there [reference guide](https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-metrics.html#production-ready-metric-writers). ) – M. Deinum Oct 30 '17 at 12:31

1 Answers1

-1

You can use Around Advice of springboot and in the advice you can log the time. The way it works is once a call is made to the controller, the Around Advice intercepts it and starts a Timer(to record the time taken). From the advice we proceed to the main controller using jointPoint.proceed() method. Once the controller returns the value you can log the timer value. return the Object.

Here is the sample code:

in build.grale include

 compile("org.aspectj:aspectjweaver:1.8.8")

Create a Component Class and put around @Aspect

@Component
@Aspect
public class advice{

@Around(("@annotation(logger)")
   public Object loggerAspect(ProceedingJoinPoint joinPoint){
    // start the Timer
    Object object = jointPoint.proceed();
    // log the timer value
    return object;
  }
}

Add annotation @logger in the controller method. you are good to go.

Hope this helps.

you can refer the link for full explanation.