如何使用AOP计算在控制器类中调用的所有内部服务的响应时间

问题描述 投票:0回答:1

My Rest Api是在Spring Boot中开发的,对于日志记录我们使用的是Splunk和Spring AOP。我的问题是如何计算被调用的所有内部服务的响应时间。现在,我能够得到端到端的响应时间。我们需要这个来在Splunk中创建仪表板来跟踪Api的性能。

java spring-boot spring-aop splunk
1个回答
0
投票

这是使用AspectJ的@Around注释的示例。

@Aspect
@Configuration
public class MethodExecutionCalculationAspect {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Around("@annotation(com.in28minutes.springboot.tutorial.basics.example.aop.TrackTime)")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();

        joinPoint.proceed();

        long timeTaken = System.currentTimeMillis() - startTime;
        logger.info("Time Taken by {} is {}", joinPoint, timeTaken);
    }
}

所以,我认为这就是你想要做的。

© www.soinside.com 2019 - 2024. All rights reserved.