Merge branch 'feature/REQ-2385' into 'master'
Feature/req 2385 See merge request universal/framework/backend/axzo-framework-commons!93
This commit is contained in:
commit
6199411c3b
36
axzo-common-loggings/axzo-common-trace/pom.xml
Normal file
36
axzo-common-loggings/axzo-common-trace/pom.xml
Normal file
@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<artifactId>axzo-common-loggings</artifactId>
|
||||
<groupId>cn.axzo.framework.logging</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>axzo-common-trace</artifactId>
|
||||
<name>Axzo Common trace</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-httpclient</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,63 @@
|
||||
package com.axzo.framework.trace.interceptor;
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* @author xudawei
|
||||
* @date 2024/04/23
|
||||
* @desc 调用前从 MDC中获取上一步骤设置的traceId,放置到 header中,供下一个服务从header中获取trace_id(注意,接口服务方接收到的header里trace_id是小写的)
|
||||
*/
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class FeignFillHeaderInterceptor implements RequestInterceptor {
|
||||
private static final String CTX_LOG_ID = "ctxLogId";
|
||||
private static final String X_REQUEST_ID = "x-request-id";
|
||||
private static final String TRACE_ID = "traceId";
|
||||
private static final String SPAN_ID = "spanId";
|
||||
private static final String LOGIC_ID = "logicId";
|
||||
|
||||
@Override
|
||||
public void apply(RequestTemplate requestTemplate) {
|
||||
log.info("FeignFillHeaderInterceptor,traceId:{}", MDC.get(TRACE_ID));
|
||||
requestTemplate.header(TRACE_ID, MDC.get(TRACE_ID));
|
||||
requestTemplate.header(CTX_LOG_ID, MDC.get(TRACE_ID));
|
||||
requestTemplate.header(X_REQUEST_ID, MDC.get(TRACE_ID));
|
||||
requestTemplate.header(SPAN_ID, this.buildNextSpanId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 下游的spanId:有本次spanId+logic组成
|
||||
* logic默认从0开始,每次调用下游时logic则自增1
|
||||
*/
|
||||
private String buildNextSpanId() {
|
||||
//获取spanId
|
||||
String spanId = this.fetchSpanId();
|
||||
if (!StringUtils.hasText(MDC.get(LOGIC_ID))) {
|
||||
MDC.put(LOGIC_ID, "0");
|
||||
}
|
||||
|
||||
AtomicLong atomicLong = new AtomicLong(Long.parseLong(MDC.get(LOGIC_ID)));
|
||||
Long nextSpanId = atomicLong.getAndIncrement();
|
||||
MDC.put(LOGIC_ID, atomicLong.toString());
|
||||
return spanId + "." + nextSpanId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取spanId
|
||||
*/
|
||||
private String fetchSpanId() {
|
||||
String spanId = MDC.get(SPAN_ID);
|
||||
if (!StringUtils.hasText(spanId)) {
|
||||
MDC.put(SPAN_ID, "0");
|
||||
}
|
||||
return spanId;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,97 @@
|
||||
package com.axzo.framework.trace.interceptor;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.axzo.framework.trace.util.ExceptionUtil;
|
||||
import com.axzo.framework.trace.wrapper.BodyReaderHttpServletRequestWrapper;
|
||||
import lombok.NonNull;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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.util.StringUtils;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 设置日志中traceId-MDC中设置traceId
|
||||
*/
|
||||
@Slf4j
|
||||
@ConditionalOnProperty(prefix = "axzo.log.common.trace", name = "enable", havingValue = "true", matchIfMissing = true)
|
||||
@Component
|
||||
@Order(-99999)
|
||||
public class TraceSpanIdFilter 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";
|
||||
private static final String SPAN_ID = "spanId";
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(@NonNull HttpServletRequest request,
|
||||
@NonNull HttpServletResponse response,
|
||||
@NonNull FilterChain filterChain) throws ServletException, IOException {
|
||||
if ((StringUtils.hasText(request.getContentType()) && request.getContentType().toLowerCase().startsWith("multipart/form-data"))) {
|
||||
this.doFilter(filterChain, request, response);
|
||||
} else {
|
||||
// wrapper
|
||||
BodyReaderHttpServletRequestWrapper bodyRequest = new BodyReaderHttpServletRequestWrapper(
|
||||
request);
|
||||
this.doFilter(filterChain, bodyRequest, response);
|
||||
}
|
||||
}
|
||||
|
||||
private void doFilter(FilterChain filterChain, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
// trace id 补充
|
||||
ExceptionUtil.ignoreException(() -> setTraceId(request, response), null);
|
||||
//do
|
||||
try {
|
||||
filterChain.doFilter(request, response);
|
||||
} finally {
|
||||
MDC.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void setTraceId(@NonNull HttpServletRequest request,
|
||||
@NonNull HttpServletResponse response) {
|
||||
|
||||
String traceId = this.fetchTraceId(request);
|
||||
//set
|
||||
MDC.put(TRACE_ID, traceId);
|
||||
MDC.put(CTX_LOG_ID, traceId);
|
||||
|
||||
MDC.put(SPAN_ID, this.fetchSpanId(request));
|
||||
response.setHeader(TRACE_ID, traceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取traceId
|
||||
*/
|
||||
private String fetchTraceId(HttpServletRequest request) {
|
||||
//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)));
|
||||
// blank to new
|
||||
return StrUtil.blankToDefault(traceId, UUID.randomUUID().toString().replaceAll("-", ""));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取spanId
|
||||
*/
|
||||
private String fetchSpanId(HttpServletRequest request) {
|
||||
// blank to new
|
||||
String spanId = StrUtil.blankToDefault(request.getHeader(SPAN_ID), "0");
|
||||
return spanId;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,28 @@
|
||||
package com.axzo.framework.trace.util;
|
||||
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* @author tanjie@axzo.cn
|
||||
* @date 2021/12/13 14:10
|
||||
*/
|
||||
public class ExceptionUtil {
|
||||
|
||||
/**
|
||||
* 忽略错误
|
||||
* @param supplier
|
||||
* @param catchConsumer
|
||||
*/
|
||||
public static void ignoreException(Runnable supplier, Consumer<Exception> catchConsumer) {
|
||||
try {
|
||||
supplier.run();
|
||||
} catch (Exception e) {
|
||||
if (null != catchConsumer) {
|
||||
catchConsumer.accept(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,58 @@
|
||||
package com.axzo.framework.trace.util;
|
||||
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @author TanJ
|
||||
* @date 2021/5/24
|
||||
*/
|
||||
public class RequestUtil {
|
||||
|
||||
|
||||
/**
|
||||
* getRequest
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static HttpServletRequest getRequest() {
|
||||
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
||||
if (requestAttributes!=null) {
|
||||
return
|
||||
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
|
||||
.getRequest();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* getResponse
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static HttpServletResponse getResponse() {
|
||||
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
||||
if (null != requestAttributes) {
|
||||
return
|
||||
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
|
||||
.getResponse();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* setInheritableHolder
|
||||
*/
|
||||
public static void setInheritableHolder() {
|
||||
RequestContextHolder.setRequestAttributes(RequestContextHolder.getRequestAttributes(), true);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,95 @@
|
||||
package com.axzo.framework.trace.wrapper;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import javax.servlet.ReadListener;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletRequestWrapper;
|
||||
import javax.servlet.http.Part;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* 自定义request
|
||||
*
|
||||
* @author TanJ
|
||||
*/
|
||||
public class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper {
|
||||
|
||||
private final byte[] body;
|
||||
|
||||
private final HttpServletRequest request;
|
||||
|
||||
@SneakyThrows
|
||||
public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
|
||||
super(request);
|
||||
this.request = request;
|
||||
// request.getParameterMap();
|
||||
String sessionStream = getBodyString(request);
|
||||
body = sessionStream.getBytes(Charset.forName("UTF-8"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Collection<Part> getParts() throws IOException, ServletException {
|
||||
return request.getParts();
|
||||
}
|
||||
|
||||
private String getBodyString(ServletRequest request) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
InputStream ins = request.getInputStream();
|
||||
|
||||
try (BufferedReader isr = new BufferedReader(
|
||||
new InputStreamReader(ins, Charset.forName("UTF-8")));) {
|
||||
String line = "";
|
||||
while ((line = isr.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw e;
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BufferedReader getReader() throws IOException {
|
||||
return new BufferedReader(new InputStreamReader(getInputStream()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServletInputStream getInputStream() throws IOException {
|
||||
|
||||
final ByteArrayInputStream bais = new ByteArrayInputStream(body);
|
||||
|
||||
return new ServletInputStream() {
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
return bais.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFinished() {
|
||||
return bais.available() == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReady() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReadListener(ReadListener readListener) {
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -18,5 +18,6 @@
|
||||
<modules>
|
||||
<module>log4j2-starter</module>
|
||||
<module>logback-starter</module>
|
||||
<module>axzo-common-trace</module>
|
||||
</modules>
|
||||
</project>
|
||||
Loading…
Reference in New Issue
Block a user