Browse Source

[Feature][dolphinscheduler-api] parse traceId in http header for Cross system delivery to #7237 (#7238)

* to #7237

* rerun test

Co-authored-by: honghuo.zw <honghuo.zw@alibaba-inc.com>
3.0.0/version-upgrade
zwZjut 3 years ago committed by GitHub
parent
commit
f7bdf174be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/aspect/AccessLogAspect.java

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

@ -19,11 +19,11 @@ package org.apache.dolphinscheduler.api.aspect;
import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.spi.utils.StringUtils;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -46,6 +46,8 @@ import org.springframework.web.context.request.ServletRequestAttributes;
public class AccessLogAspect { public class AccessLogAspect {
private static final Logger logger = LoggerFactory.getLogger(AccessLogAspect.class); private static final Logger logger = LoggerFactory.getLogger(AccessLogAspect.class);
private static final String TRACE_ID = "traceId";
@Pointcut("@annotation(org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation)") @Pointcut("@annotation(org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation)")
public void logPointCut(){ public void logPointCut(){
// Do nothing because of it's a pointcut // Do nothing because of it's a pointcut
@ -60,21 +62,24 @@ public class AccessLogAspect {
Method method = sign.getMethod(); Method method = sign.getMethod();
AccessLogAnnotation annotation = method.getAnnotation(AccessLogAnnotation.class); AccessLogAnnotation annotation = method.getAnnotation(AccessLogAnnotation.class);
String tranceId = UUID.randomUUID().toString(); String traceId = UUID.randomUUID().toString();
// log request // log request
if (!annotation.ignoreRequest()) { if (!annotation.ignoreRequest()) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); 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);
if (!StringUtils.isEmpty(traceIdFromHeader)) {
traceId = traceIdFromHeader;
}
// handle login info // handle login info
String userName = parseLoginInfo(request); String userName = parseLoginInfo(request);
// handle args // handle args
String argsString = parseArgs(proceedingJoinPoint, annotation); String argsString = parseArgs(proceedingJoinPoint, annotation);
logger.info("REQUEST TRANCE_ID:{}, LOGIN_USER:{}, URI:{}, METHOD:{}, HANDLER:{}, ARGS:{}", logger.info("REQUEST TRACE_ID:{}, LOGIN_USER:{}, URI:{}, METHOD:{}, HANDLER:{}, ARGS:{}",
tranceId, traceId,
userName, userName,
request.getRequestURI(), request.getRequestURI(),
request.getMethod(), request.getMethod(),
@ -88,7 +93,7 @@ public class AccessLogAspect {
// log response // log response
if (!annotation.ignoreResponse()) { if (!annotation.ignoreResponse()) {
logger.info("RESPONSE TRANCE_ID:{}, BODY:{}, REQUEST DURATION:{} milliseconds", tranceId, ob, (System.currentTimeMillis() - startTime)); logger.info("RESPONSE TRACE_ID:{}, BODY:{}, REQUEST DURATION:{} milliseconds", traceId, ob, (System.currentTimeMillis() - startTime));
} }
return ob; return ob;

Loading…
Cancel
Save