feat(trace-log):增加加载条件注解;适配traceId
This commit is contained in:
parent
79592cb534
commit
5bdbeb940f
@ -5,6 +5,7 @@ import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
@ -23,8 +24,9 @@ import java.util.Map;
|
||||
* @author tanjie@axzo.cn
|
||||
* @date 2021/12/13 11:13
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
@ConditionalOnProperty(prefix = "axzo.log.pokonya", name = "enable", havingValue = "true", matchIfMissing = true)
|
||||
@Component
|
||||
public class RequestLogHandlerInterceptor implements HandlerInterceptor, WebMvcConfigurer {
|
||||
|
||||
private static final String SPLIT_EXCLUDE_PARAM = "\\|";
|
||||
|
||||
@ -3,11 +3,13 @@ package cn.axzo.pokonyan.filter;
|
||||
|
||||
import cn.axzo.pokonyan.util.ExceptionUtil;
|
||||
import cn.axzo.pokonyan.wrapper.BodyReaderHttpServletRequestWrapper;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.google.common.base.Strings;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.skywalking.apm.toolkit.trace.TraceContext;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
@ -19,17 +21,22 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author TanJ
|
||||
* @date 2021/5/24
|
||||
*/
|
||||
@Slf4j
|
||||
@ConditionalOnProperty(prefix = "axzo.log.pokonya", name = "enable", havingValue = "true", matchIfMissing = true)
|
||||
@Component
|
||||
@Order(-99999)
|
||||
public class TraceIdFilter extends OncePerRequestFilter {
|
||||
|
||||
private static final String TRACE_ID = "traceId";
|
||||
private static final String CTX_LOG_ID = "ctxLogId";
|
||||
private static final String X_REQUEST_ID = "x-request-id";
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
@ -47,21 +54,29 @@ public class TraceIdFilter extends OncePerRequestFilter {
|
||||
|
||||
|
||||
//do
|
||||
filterChain.doFilter(bodyRequest, response);
|
||||
try {
|
||||
filterChain.doFilter(bodyRequest, response);
|
||||
} finally {
|
||||
MDC.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void setTraceId(@Nonnull HttpServletRequest request,
|
||||
@Nonnull HttpServletResponse response) {
|
||||
|
||||
request.getParameterMap();
|
||||
String traceId = request.getHeader(TRACE_ID);
|
||||
traceId = StrUtil.isBlank(traceId) ? MDC.get(TRACE_ID) : traceId;
|
||||
//设置SkyWalking
|
||||
if (Strings.isNullOrEmpty(traceId)) {
|
||||
MDC.put(TRACE_ID, TraceContext.traceId());
|
||||
response.setHeader(TRACE_ID, MDC.get(TRACE_ID));
|
||||
}
|
||||
//header: ctxLogId -> traceId -> x-request-id
|
||||
String traceId = StrUtil.blankToDefault(request.getHeader(CTX_LOG_ID),
|
||||
StrUtil.blankToDefault(request.getHeader(TRACE_ID),
|
||||
request.getHeader(X_REQUEST_ID)));
|
||||
//skywalking or new
|
||||
traceId = StrUtil.blankToDefault(traceId,
|
||||
StrUtil.blankToDefault(TraceContext.traceId(),
|
||||
UUID.randomUUID().toString().replaceAll("-", "") ));
|
||||
//set
|
||||
MDC.put(TRACE_ID, traceId);
|
||||
MDC.put(CTX_LOG_ID, traceId);
|
||||
response.setHeader(TRACE_ID, traceId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user