mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-12 20:51:48 +00:00
(improvement)(chat) add traceId and time cost for show (#272)
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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("/**");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user