[Improvement] Add call cost time for api (#13518)
This commit is contained in:
parent
75c5915b78
commit
df963f8393
|
|
@ -32,6 +32,4 @@ public @interface AccessLogAnnotation {
|
|||
String[] ignoreRequestArgs() default {"loginUser"};
|
||||
|
||||
boolean ignoreRequest() default false;
|
||||
|
||||
boolean ignoreResponse() default true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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…
Reference in New Issue