Browse Source

[Improvement] Add call cost time for api (#13518)

3.2.0-release
caishunfeng 1 year ago committed by GitHub
parent
commit
df963f8393
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/aspect/AccessLogAnnotation.java
  2. 19
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/aspect/AccessLogAspect.java

2
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/aspect/AccessLogAnnotation.java

@ -32,6 +32,4 @@ public @interface AccessLogAnnotation {
String[] ignoreRequestArgs() default {"loginUser"}; String[] ignoreRequestArgs() default {"loginUser"};
boolean ignoreRequest() default false; boolean ignoreRequest() default false;
boolean ignoreResponse() default true;
} }

19
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/aspect/AccessLogAspect.java

@ -65,6 +65,15 @@ public class AccessLogAspect {
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
String URI = null;
String requestMethod = null;
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
URI = request.getRequestURI();
requestMethod = request.getMethod();
}
// fetch AccessLogAnnotation // fetch AccessLogAnnotation
MethodSignature sign = (MethodSignature) proceedingJoinPoint.getSignature(); MethodSignature sign = (MethodSignature) proceedingJoinPoint.getSignature();
Method method = sign.getMethod(); Method method = sign.getMethod();
@ -74,12 +83,10 @@ public class AccessLogAspect {
// log request // log request
if (!annotation.ignoreRequest()) { if (!annotation.ignoreRequest()) {
ServletRequestAttributes attributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes != null) { if (attributes != null) {
HttpServletRequest request = attributes.getRequest(); HttpServletRequest request = attributes.getRequest();
String traceIdFromHeader = request.getHeader(TRACE_ID); String traceIdFromHeader = request.getHeader(TRACE_ID);
if (!StringUtils.isEmpty(traceIdFromHeader)) { if (StringUtils.isNotEmpty(traceIdFromHeader)) {
traceId = traceIdFromHeader; traceId = traceIdFromHeader;
} }
// handle login info // handle login info
@ -103,11 +110,7 @@ public class AccessLogAspect {
Object ob = proceedingJoinPoint.proceed(); Object ob = proceedingJoinPoint.proceed();
// log response log.info("Call {}:{} success, cost: {}ms", requestMethod, URI, (System.currentTimeMillis() - startTime));
if (!annotation.ignoreResponse()) {
log.info("RESPONSE TRACE_ID:{}, BODY:{}, REQUEST DURATION:{} milliseconds", traceId, ob,
(System.currentTimeMillis() - startTime));
}
return ob; return ob;
} }

Loading…
Cancel
Save