webflux端点性能记录器

o75abkj4  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(670)

您知道记录springwebflux控制器端点性能以保持React性的良好实践吗?下面的原则似乎不起作用,因为它会阻止io,因为proceedingjoinpoint不返回publisher<>,它只返回一个对象

@Aspect
@Component
@Slf4j
public class LoggingAspect
{

    //AOP expression for which methods shall be intercepted
    @Around("execution(* com.company.service..*(..)))")
    public Object profileAllMethods(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
    {
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();

        //Get intercepted method details
        String className = methodSignature.getDeclaringType().getSimpleName();
        String methodName = methodSignature.getName();

        final StopWatch stopWatch = new StopWatch();

        //Measure method execution time
        stopWatch.start();
        Object result = proceedingJoinPoint.proceed();
        stopWatch.stop();

        //Log method execution time
        log.info("Execution time of " + className + "." + methodName + " :: " + stopWatch.getTotalTimeMillis() + " ms");

        return result;
    }
}
ioekq8ef

ioekq8ef1#

实际上正如在评论中提到的@toerktumlare proceedingJoinPoint.proceed() 返回对象的类型无论控制器端点返回类型是什么,因此可以保持被动的性质。注意 .subscribeOn(Schedulers.parallel()) 在这里是可选的,这是为了让我的后台代码支持并行性。为此发布解决方案:

@Aspect
@Component
@Slf4j
public class LoggingAspect
{

    //AOP expression for which methods shall be intercepted
    @Around("execution(* com.company.service..*(..)))")
    public Object logEndpointPerformance(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
    {
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();

        //Get intercepted method details
        String className = methodSignature.getDeclaringType().getSimpleName();
        String methodName = methodSignature.getName();

        final StopWatch stopWatch = new StopWatch();

        //Measure method execution time
        stopWatch.start();
        Object result = proceedingJoinPoint.proceed();

        if(result instanceof Mono){
            return ((Mono)result).subscribeOn(Schedulers.parallel()).flatMap(r -> {
                logExecutionTime(className, methodName, stopWatch);
                return Mono.just(r);
            });
        }
        else if(result instanceof Flux){
            return ((Flux<Object>)result).subscribeOn(Schedulers.parallel()).collectList().flatMapMany(r -> {
                logExecutionTime(className, methodName, stopWatch);
                return Flux.fromIterable(r);
            });
        }
        else{
            logExecutionTime(className, methodName, stopWatch);
            return result;
        }
    }

    private void logExecutionTime(final String className, final String methodName, final StopWatch stopWatch){
        stopWatch.stop();
        //Log method execution time
        log.debug("[ " + stopWatch.getTotalTimeMillis() + " mls ] lasted execution of" + className + "." + methodName );
    }
}

相关问题