diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/OperatorLogAspect.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/OperatorLogAspect.java index 4e15112980..da8a7bd3e6 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/OperatorLogAspect.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/OperatorLogAspect.java @@ -18,9 +18,11 @@ package org.apache.dolphinscheduler.api.audit; import org.apache.dolphinscheduler.api.audit.operator.AuditOperator; +import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.service.bean.SpringApplicationContext; import java.lang.reflect.Method; +import java.util.Map; import lombok.extern.slf4j.Slf4j; @@ -54,8 +56,18 @@ public class OperatorLogAspect { log.warn("Operation is null of method: {}", method.getName()); return point.proceed(); } + long beginTime = System.currentTimeMillis(); - AuditOperator operator = SpringApplicationContext.getBean(operatorLog.auditType().getOperatorClass()); - return operator.recordAudit(point, operation.description(), operatorLog.auditType()); + Map paramsMap = OperatorUtils.getParamsMap(point, signature); + Result result = (Result) point.proceed(); + try { + AuditOperator operator = SpringApplicationContext.getBean(operatorLog.auditType().getOperatorClass()); + long latency = System.currentTimeMillis() - beginTime; + operator.recordAudit(paramsMap, result, latency, operation, operatorLog); + } catch (Throwable throwable) { + log.error("Record audit log error", throwable); + } + + return result; } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/OperatorUtils.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/OperatorUtils.java index a0233c7e93..10ccf8d648 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/OperatorUtils.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/OperatorUtils.java @@ -66,7 +66,6 @@ public class OperatorUtils { auditLog.setOperationType(auditType.getAuditOperationType().getName()); auditLog.setDescription(apiDescription); auditLog.setCreateTime(new Date()); - auditLogList.add(auditLog); return auditLogList; } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/operator/AuditOperator.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/operator/AuditOperator.java index 6e1c016313..7d76d5c4bd 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/operator/AuditOperator.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/operator/AuditOperator.java @@ -17,11 +17,18 @@ package org.apache.dolphinscheduler.api.audit.operator; -import org.apache.dolphinscheduler.api.audit.enums.AuditType; +import org.apache.dolphinscheduler.api.audit.OperatorLog; +import org.apache.dolphinscheduler.api.utils.Result; -import org.aspectj.lang.ProceedingJoinPoint; +import java.util.Map; + +import io.swagger.v3.oas.annotations.Operation; public interface AuditOperator { - Object recordAudit(ProceedingJoinPoint point, String describe, AuditType auditType) throws Throwable; + void recordAudit(Map paramsMap, + Result result, + long latency, + Operation operation, + OperatorLog operatorLog) throws Throwable; } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/operator/BaseAuditOperator.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/operator/BaseAuditOperator.java index 5c896a058a..270a5e4df4 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/operator/BaseAuditOperator.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/operator/BaseAuditOperator.java @@ -17,6 +17,7 @@ package org.apache.dolphinscheduler.api.audit.operator; +import org.apache.dolphinscheduler.api.audit.OperatorLog; import org.apache.dolphinscheduler.api.audit.OperatorUtils; import org.apache.dolphinscheduler.api.audit.enums.AuditType; import org.apache.dolphinscheduler.api.service.AuditService; @@ -31,12 +32,11 @@ import java.util.Map; import lombok.extern.slf4j.Slf4j; -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.google.common.base.Strings; +import io.swagger.v3.oas.annotations.Operation; @Service @Slf4j @@ -46,26 +46,27 @@ public abstract class BaseAuditOperator implements AuditOperator { private AuditService auditService; @Override - public Object recordAudit(ProceedingJoinPoint point, String describe, AuditType auditType) throws Throwable { - long beginTime = System.currentTimeMillis(); + public void recordAudit(Map paramsMap, + Result result, + long latency, + Operation operation, + OperatorLog operatorLog) { - MethodSignature signature = (MethodSignature) point.getSignature(); - Map paramsMap = OperatorUtils.getParamsMap(point, signature); + AuditType auditType = operatorLog.auditType(); User user = OperatorUtils.getUser(paramsMap); if (user == null) { log.error("user is null"); - return point.proceed(); + return; } - List auditLogList = OperatorUtils.buildAuditLogList(describe, auditType, user); + List auditLogList = OperatorUtils.buildAuditLogList(operation.description(), auditType, user); setRequestParam(auditType, auditLogList, paramsMap); - Result result = (Result) point.proceed(); if (OperatorUtils.resultFail(result)) { log.error("request fail, code {}", result.getCode()); - return result; + return; } setObjectIdentityFromReturnObject(auditType, result, auditLogList); @@ -73,10 +74,9 @@ public abstract class BaseAuditOperator implements AuditOperator { modifyAuditOperationType(auditType, paramsMap, auditLogList); modifyAuditObjectType(auditType, paramsMap, auditLogList); - long latency = System.currentTimeMillis() - beginTime; - auditService.addAudit(auditLogList, latency); + auditLogList.forEach(auditLog -> auditLog.setLatency(latency)); + auditLogList.forEach(auditLog -> auditService.addAudit(auditLog)); - return result; } protected void setRequestParam(AuditType auditType, List auditLogList, Map paramsMap) { diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AuditService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AuditService.java index b4296452f6..8fb99eaf73 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AuditService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AuditService.java @@ -21,8 +21,6 @@ import org.apache.dolphinscheduler.api.dto.AuditDto; import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.dao.entity.AuditLog; -import java.util.List; - /** * audit information service */ @@ -35,14 +33,6 @@ public interface AuditService { */ void addAudit(AuditLog auditLog); - /** - * add audit by list - * - * @param auditLogList auditLog list - * @param latency api latency milliseconds - */ - void addAudit(List auditLogList, long latency); - /** * query audit log list * diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AuditServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AuditServiceImpl.java index 2ec547bcb6..f8f763b4f4 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AuditServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AuditServiceImpl.java @@ -57,14 +57,6 @@ public class AuditServiceImpl extends BaseServiceImpl implements AuditService { auditLogMapper.insert(auditLog); } - @Override - public void addAudit(List auditLogList, long latency) { - auditLogList.forEach(auditLog -> { - auditLog.setLatency(latency); - addAudit(auditLog); - }); - } - /** * query audit log paging *