likes
comments
collection
share

优雅哥--如何优雅的监控接口的执行情况

作者站长头像
站长
· 阅读数 5

优雅哥--如何优雅的监控接口的执行情况 在工作中,我们往往会遇到一些接口的报错,在排查问题的时候,由于没有对接口的执行情况,以及入参进行监控,所以排查起问题就特别费劲,今天我们就一起来写一个接口的拦截器。

@Slf4j
@Aspect
@Component
public class LogAspect {

    @SneakyThrows
    @Around("execution(* cn.com.alliance.we.controller..*(..))")
    public Object doAround(ProceedingJoinPoint pjp){
        // 创造计时器
        Stopwatch stWatch = Stopwatch.createStarted();
        //获取请求信息
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sra.getRequest();
        String tenantId = "unknown";
        try {
            tenantId = TenantContextHolder.getTenantId().toLowerCase(Locale.ROOT);
        }catch (Exception e){
            log.error("LogAspect获取租户异常:", e);
        }
        //获取请求地址
        String requestURI = request.getRequestURI();
        //组合请求参数,进行日志打印
        Object[] objects = pjp.getArgs();
        Object[] arguments  = new Object[objects.length];
        for (int i = 0; i < objects.length; i++) {
            if (objects[i] instanceof ServletRequest || objects[i] instanceof ServletResponse || objects[i] instanceof MultipartFile) {
                //过滤掉不能序列化的参数
                continue;
            }
            arguments[i] = objects[i];
        }
        //获取登录信息
        OcManageUser currentUserOrNull = OcManageUserContext.getCurrentUserOrNull();
        String currentOrgCode = "Unknown";
        String currentEmpCode = "Unknown";
        if(currentUserOrNull != null){
            currentOrgCode = currentUserOrNull.getOrgCode();
            currentEmpCode = currentUserOrNull.getEmpCode();
        }
        Object result = null;
        try {
            result = pjp.proceed();
            //请求操作成功
            String resultJosnString = "";
            if (result != null) {
                try{
                    resultJosnString = JsonUtil.bean2Json(result);
                }catch (Exception e){
                    resultJosnString = String.valueOf(result);
                }
            }
            //记录请求完成执行时间:
            long usedTime = stWatch.elapsed(TimeUnit.MILLISECONDS);
            //记录日志
            if (log.isInfoEnabled()) {
                log.info("URI信息:{},租户:{},登录人:{},登录网点:{}, 耗时:{},请求参数:{}, 响应参数:{}", v("requestURI", requestURI),tenantId,currentEmpCode,currentOrgCode, v("elapsed", usedTime), JsonUtil.bean2Json(arguments), resultJosnString);
            }
            return result;
        }catch (Throwable et){
            //请求异常操作
            //记录请求完成执行时间:
            long usedTime = stWatch.elapsed(TimeUnit.MILLISECONDS);
            //记录日志
            log.error("URI信息:{},租户:{},登录人:{},登录网点:{}, 耗时:{},请求参数:{}, 响应异常信息:", v("requestURI", requestURI),tenantId,currentEmpCode,currentOrgCode, v("elapsed", usedTime), JsonUtil.bean2Json(arguments), et);
            throw et;
        }
    }

}

写这么一个拦截器并不复杂,主要的还是需要注意一些细节,比如过滤掉一些不能序列化的参数;还有就是可以根据自己的需要,将请求头的参数也打印出来。

转载自:https://juejin.cn/post/7329433979806826530
评论
请登录