(improvement)(chat) add traceId and time cost for show (#272)

This commit is contained in:
mainmain
2023-10-24 16:21:30 +08:00
committed by GitHub
parent cd901fbc68
commit e4e39e0496
17 changed files with 229 additions and 20 deletions

View File

@@ -0,0 +1,41 @@
package com.tencent.supersonic.common.interceptor;
import com.tencent.supersonic.common.util.TraceIdUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
@Slf4j
public class LogInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
//use previous traceId
String traceId = request.getHeader(TraceIdUtil.TRACE_ID);
if (StringUtils.isBlank(traceId)) {
TraceIdUtil.setTraceId(TraceIdUtil.generateTraceId());
} else {
TraceIdUtil.setTraceId(traceId);
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView modelAndView)
throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
//remove after Completing
TraceIdUtil.remove();
}
}

View File

@@ -1,6 +1,8 @@
package com.tencent.supersonic.common.pojo;
import com.tencent.supersonic.common.util.TraceIdUtil;
import lombok.Data;
import org.slf4j.MDC;
/***
* result data
@@ -11,6 +13,7 @@ public class ResultData<T> {
private String msg;
private T data;
private long timestamp;
private String traceId;
public ResultData() {
this.timestamp = System.currentTimeMillis();
@@ -21,6 +24,7 @@ public class ResultData<T> {
resultData.setCode(ReturnCode.SUCCESS.getCode());
resultData.setMsg(ReturnCode.SUCCESS.getMessage());
resultData.setData(data);
resultData.setTraceId(MDC.get(TraceIdUtil.TRACE_ID));
return resultData;
}
@@ -28,7 +32,8 @@ public class ResultData<T> {
ResultData<T> resultData = new ResultData<>();
resultData.setCode(code);
resultData.setMsg(message);
resultData.setTraceId(MDC.get(TraceIdUtil.TRACE_ID));
return resultData;
}
}
}

View File

@@ -0,0 +1,30 @@
package com.tencent.supersonic.common.pojo;
import com.tencent.supersonic.common.util.ThreadMdcUtil;
import org.slf4j.MDC;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
public class ThreadPoolExecutorMdcWrapper extends ThreadPoolTaskExecutor {
private static final long serialVersionUID = 3940722618853093830L;
@Override
public void execute(Runnable task) {
super.execute(ThreadMdcUtil.wrap(task, MDC.getCopyOfContextMap()));
}
@Override
public <T> Future<T> submit(Callable<T> task) {
return super
.submit(ThreadMdcUtil.wrap(task, MDC.getCopyOfContextMap()));
}
@Override
public Future<?> submit(Runnable task) {
return super
.submit(ThreadMdcUtil.wrap(task, MDC.getCopyOfContextMap()));
}
}

View File

@@ -0,0 +1,49 @@
package com.tencent.supersonic.common.util;
import org.slf4j.MDC;
import java.util.Map;
import java.util.concurrent.Callable;
public class ThreadMdcUtil {
public static void setTraceIdIfAbsent() {
if (MDC.get(TraceIdUtil.TRACE_ID) == null) {
MDC.put(TraceIdUtil.TRACE_ID, TraceIdUtil.generateTraceId());
}
}
public static <T> Callable<T> wrap(final Callable<T> callable, final Map<String, String> context) {
return () -> {
if (context == null) {
MDC.clear();
} else {
MDC.setContextMap(context);
}
setTraceIdIfAbsent();
try {
return callable.call();
} finally {
MDC.clear();
}
};
}
public static Runnable wrap(final Runnable runnable, final Map<String, String> context) {
return () -> {
if (context == null) {
MDC.clear();
} else {
MDC.setContextMap(context);
}
//设置traceId
setTraceIdIfAbsent();
try {
runnable.run();
} finally {
MDC.clear();
}
};
}
}

View File

@@ -0,0 +1,34 @@
package com.tencent.supersonic.common.util;
import org.slf4j.MDC;
import java.util.UUID;
public class TraceIdUtil {
public static final String TRACE_ID = "traceId";
public static final String PREFIX = "supersonic";
public static String getTraceId() {
String traceId = (String) MDC.get(TRACE_ID);
return traceId == null ? "" : traceId;
}
public static void setTraceId(String traceId) {
MDC.put(TRACE_ID, traceId);
}
public static void remove() {
MDC.remove(TRACE_ID);
}
public static void clear() {
MDC.clear();
}
public static String generateTraceId() {
String uuid = UUID.randomUUID().toString().replace("-", "");
return PREFIX + "_" + uuid;
}
}

View File

@@ -0,0 +1,16 @@
package com.tencent.supersonic.common.util;
import com.tencent.supersonic.common.interceptor.LogInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**");
}
}