Spring Boot API response time

时间:2017-05-16 09:34:33

标签: spring-boot spring-boot-actuator

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?

1 个答案:

答案 0 :(得分:1)

你可以尝试AOP""关于DispatcherServlet#service方法的建议可能吗?

未测试:

@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;
    }

}

不要忘记在POM中添加aop启动器:

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

这将为您提供整个请求调用的非常接近的时间,包括Spring拦截器,处理程序,控制器等所有内容。