Browse Source

ActiveSpan

fanxp 1 month ago
parent
commit
26afaedc77

+ 7 - 1
pom.xml

@@ -72,6 +72,7 @@
 
         <zxing.version>3.2.1</zxing.version>
         <hikvision.version>1.1.3</hikvision.version>
+        <apm.toolkit.version>9.3.0</apm.toolkit.version>
     </properties>
 
     <dependencies>
@@ -529,7 +530,12 @@
             <artifactId>javase</artifactId>
             <version>${zxing.version}</version>
         </dependency>
-
+        <!-- apm日志 -->
+        <dependency>
+            <groupId>org.apache.skywalking</groupId>
+            <artifactId>apm-toolkit-trace</artifactId>
+            <version>${apm.toolkit.version}</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 94 - 0
src/main/java/com/xjrsoft/common/aspect/ControllerLogAspect.java

@@ -0,0 +1,94 @@
+package com.xjrsoft.common.aspect;
+
+import cn.hutool.core.exceptions.ExceptionUtil;
+import cn.hutool.core.util.StrUtil;
+import com.alibaba.fastjson.JSON;
+import com.xjrsoft.common.annotation.XjrLog;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.skywalking.apm.toolkit.trace.ActiveSpan;
+import org.apache.skywalking.apm.toolkit.trace.TraceContext;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
+
+import javax.servlet.ServletRequest;
+import java.lang.reflect.Method;
+
+@Slf4j
+@Aspect
+@Component
+public class ControllerLogAspect {
+    public static final String BIG_STR_SUB_PATTERN = "(:s*\"[^,\":]{8192,}?\")";
+
+    /**
+     * API的切点,拦截所有controller
+     */
+    @Pointcut("execution(public * com.xjrsoft.module.*.controller.*.*(..))")
+    public void apiPointcut() {
+    }
+
+    /**
+     * 在方法执行前进行参数判断、在方法执行后记录日志
+     *
+     * @param joinPoint 入参
+     */
+    @Around("apiPointcut()")
+    public Object executeFaceAspect(ProceedingJoinPoint joinPoint) throws Throwable {
+        Object returnObj = null;
+        Object[] parameters = joinPoint.getArgs();
+        try {
+            returnObj = joinPoint.proceed();
+        } catch (Exception e) {
+            log.error("SkyWalking aspect catch exception {}", TraceContext.traceId(), e);
+            ActiveSpan.error(ExceptionUtil.getRootCauseMessage(e));
+            throw e;
+        } finally {
+            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
+            Method method = signature.getMethod();
+            XjrLog xjrLog = method.getAnnotation(XjrLog.class);
+            if (parameters != null && parameters.length > 0) {
+                for (int i = 0; i < parameters.length; i++) {
+                    try {
+                        Object nowObj = parameters[i];
+                        if (nowObj instanceof ServletRequest) {
+                            continue;
+                        }
+                        if (xjrLog != null && xjrLog.saveRequestData()) {
+                            ActiveSpan.tag("params[ " + i + " ]", replaceLog(JSON.toJSONString(nowObj)));
+                        }
+                    } catch (Exception ignore) {
+
+                    }
+                }
+            }
+
+            if (returnObj != null) {
+                try {
+                    if (xjrLog != null && xjrLog.saveResponseData()) {
+                        ActiveSpan.tag("result", replaceLog(JSON.toJSONString(returnObj)));
+                    }
+                } catch (Exception ignore) {
+
+                }
+            }
+        }
+        return returnObj;
+    }
+
+    /*
+     * 格式化日志
+     *
+     * @param logStr 入参
+     * @return String
+     */
+    public static String replaceLog(String logStr) {
+        if (StringUtils.hasLength(logStr)) {
+            return logStr.replaceAll(BIG_STR_SUB_PATTERN, ":\"*\"");
+        }
+        return StrUtil.EMPTY;
+    }
+}

+ 11 - 0
src/main/java/com/xjrsoft/common/exception/GlobalExceptionHandler.java

@@ -5,6 +5,7 @@ import cn.dev33.satoken.exception.NotPermissionException;
 import com.xjrsoft.common.enums.ResponseCode;
 import com.xjrsoft.common.model.result.R;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.skywalking.apm.toolkit.trace.ActiveSpan;
 import org.springframework.validation.BindException;
 import org.springframework.validation.BindingResult;
 import org.springframework.validation.FieldError;
@@ -32,6 +33,8 @@ public class GlobalExceptionHandler {
     @ResponseBody
     public R globalExceptionHandler(Exception e){
         log.error(e.getMessage(), e);
+        // 使用SkyWalking的Trace Context记录错误信息
+        ActiveSpan.error(e.getMessage());
         return R.error(ResponseCode.INTERNAL_SERVER_ERROR.getCode(),e.getMessage());
     }
 
@@ -45,6 +48,8 @@ public class GlobalExceptionHandler {
     @ResponseBody
     public R notLoginExceptionHandler(NotLoginException e){
         log.error(e.getMessage(), e);
+        // 使用SkyWalking的Trace Context记录错误信息
+        ActiveSpan.error(e.getMessage());
         return R.error(ResponseCode.UN_AUTHORIZED.getCode(),ResponseCode.UN_AUTHORIZED.getMessage());
     }
 
@@ -57,6 +62,8 @@ public class GlobalExceptionHandler {
     @ResponseBody
     public R notPermissionExceptionHandler(NotPermissionException e){
         log.error(e.getMessage(), e);
+        // 使用SkyWalking的Trace Context记录错误信息
+        ActiveSpan.error(e.getMessage());
         return R.error(ResponseCode.UN_AUTHORIZED.getCode(),e.getMessage());
     }
 
@@ -76,9 +83,13 @@ public class GlobalExceptionHandler {
             if (!errors.isEmpty()) {
                 // 这里列出了全部错误参数,按正常逻辑,只需要第一条错误即可
                 FieldError fieldError = (FieldError) errors.get(0);
+                // 使用SkyWalking的Trace Context记录错误信息
+                ActiveSpan.error(fieldError.getDefaultMessage());
                 return R.error(fieldError.getDefaultMessage());
             }
         }
+        // 使用SkyWalking的Trace Context记录错误信息
+        ActiveSpan.error(e.getMessage());
         return R.error("请求参数错误");
     }