Browse Source

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

3.2.0-release
caishunfeng 2 years 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"};
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 {
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
MethodSignature sign = (MethodSignature) proceedingJoinPoint.getSignature();
Method method = sign.getMethod();
@ -74,12 +83,10 @@ public class AccessLogAspect {
// log request
if (!annotation.ignoreRequest()) {
ServletRequestAttributes attributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
String traceIdFromHeader = request.getHeader(TRACE_ID);
if (!StringUtils.isEmpty(traceIdFromHeader)) {
if (StringUtils.isNotEmpty(traceIdFromHeader)) {
traceId = traceIdFromHeader;
}
// handle login info
@ -103,11 +110,7 @@ public class AccessLogAspect {
Object ob = proceedingJoinPoint.proceed();
// log response
if (!annotation.ignoreResponse()) {
log.info("RESPONSE TRACE_ID:{}, BODY:{}, REQUEST DURATION:{} milliseconds", traceId, ob,
(System.currentTimeMillis() - startTime));
}
log.info("Call {}:{} success, cost: {}ms", requestMethod, URI, (System.currentTimeMillis() - startTime));
return ob;
}

Loading…
Cancel
Save