diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/ITrace.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/ITrace.java deleted file mode 100644 index 483b408db..000000000 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/ITrace.java +++ /dev/null @@ -1,16 +0,0 @@ -package cn.iocoder.dashboard.framework.tracer.core; - -/** - * 用于扩展获取traceId的场景,需要装载到Spring bean容器中. - * - * @author 麻薯 - */ -public interface ITrace { - - /** - * 用于接入三方traceId - * - * @return traceId - */ - String getTraceId(); -} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java index a492149b4..25562a8eb 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java @@ -1,9 +1,5 @@ package cn.iocoder.dashboard.framework.tracer.core.util; -import cn.hutool.core.util.StrUtil; -import cn.hutool.extra.spring.SpringUtil; -import cn.iocoder.dashboard.framework.tracer.core.ITrace; -import io.netty.util.concurrent.FastThreadLocal; import org.apache.skywalking.apm.toolkit.trace.TraceContext; import java.util.UUID; @@ -15,36 +11,6 @@ import java.util.UUID; */ public class TracerUtils { - /** - * 维护请求线程对应的TraceId - */ - private final static FastThreadLocal traceIdMap = new FastThreadLocal<>(); - - /** - * 保存链路流水号 - * - * @param traceId 链路流水号 - */ - public static void saveThreadTraceId(String traceId) { - traceIdMap.set(traceId); - } - - /** - * 根据线程获取链路流水号 - * - * @return 链路流水号 - */ - public static String getThreadTraceId() { - return traceIdMap.get(); - } - - /** - * 根据线程删除链路流水 - */ - public static void deleteThreadTraceId() { - traceIdMap.remove(); - } - /** * 私有化构造方法 */ @@ -54,71 +20,13 @@ public class TracerUtils { /** * 获得链路追踪编号 *

- * 一般来说,通过链路追踪编号,可以将访问日志,错误日志,链路追踪日志,logger 打印日志等,结合在一起,从而进行排错。 + * 直接返回skywalking 的TraceId 如果不存在的话为空字符串"" *

- * 默认情况下,我们使用 Apache SkyWalking 的 traceId 作为链路追踪编号。当然,可能会存在并未引入 Skywalking 的情况,此时使用 UUID 。 - * 方法获取顺序: skywalking - > map -> 扩展接口 -> default - * 项目整体获取traceId的优先级 skywalking > 自定义实现接口 > 默认 - * map中的traceId 当且仅当其他情况为产生时才会出现. * * @return 链路追踪编号 */ public static String getTraceId() { - String traceId; - // 通过 SkyWalking 获取链路编号 - try { - traceId = TraceContext.traceId(); - if (StrUtil.isNotBlank(traceId)) { - return traceId; - } - } catch (Throwable ignore) { - } - // 尝试从map中获取 - traceId = traceIdMap.get(); - if (StrUtil.isNotBlank(traceId)) { - return traceId; - } - // 通过自定义扩展的tracer产生traceId, 在Spring容器加载完成前会获取不到对应的Bean - ITrace tracer = null; - try { - tracer = getTracer(); - } catch (Throwable ignore) { - } - if (null != tracer) { - try { - return tracer.getTraceId(); - } catch (Throwable ignored) { - } - } - // TODO 芋艿 多次调用会问题 - - return defaultTraceId(); - } - - /** - * 仅仅获取Skywalking 中的traceId, 若无skywalking,则会返回"" - * - * @return skywalking 的traceId - */ - public static String getSkywalkingTraceId() { return TraceContext.traceId(); } - /** - * 从Spring 容器中获取 ITrace 类,返回可以为null - * - * @return ITrace - */ - private static ITrace getTracer() { - return SpringUtil.getBean(ITrace.class); - } - - /** - * 默认生成TraceId规则为UUID - * - * @return UUID - */ - private static String defaultTraceId() { - return "UUID:" + UUID.randomUUID().toString(); - } } diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/ServletTraceFilter.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/ServletTraceFilter.java deleted file mode 100644 index dc3c1b03b..000000000 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/ServletTraceFilter.java +++ /dev/null @@ -1,64 +0,0 @@ -package cn.iocoder.dashboard.framework.tracer.filter; - -import cn.hutool.core.util.StrUtil; -import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.lang.Nullable; -import org.springframework.stereotype.Component; -import org.springframework.web.servlet.HandlerInterceptor; - -import javax.servlet.*; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; - -/** - * 对Spring Mvc 的请求拦截, 添加traceId. - * - * @author mashu - */ - -@Slf4j -@Component -public class ServletTraceFilter implements Filter { - - @Value("${cn.iocoder.tracer.name:global-trace-id}") - private String traceIdName; - - @Override - public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { - HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; - HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse; - try { - // 请求中traceId - String reqTraceId = (String)httpServletRequest.getHeader(traceIdName); - // skywalking中的traceId - String skywalkingTraceId = TracerUtils.getSkywalkingTraceId(); - String traceId ; - if (null == reqTraceId && StrUtil.isBlank(skywalkingTraceId)) { - // 两者皆空,添加默认的. - traceId = TracerUtils.getTraceId(); - httpServletResponse.setHeader(traceIdName, traceId); - } else if (null == reqTraceId && StrUtil.isNotBlank(skywalkingTraceId)){ - // 若请求空,则添加,为没有skywalking的系统添加一个TraceId - traceId = skywalkingTraceId; - httpServletResponse.setHeader(traceIdName, traceId); - } else if (null != reqTraceId && StrUtil.isBlank(skywalkingTraceId)) { - // 请求非空, skywalking为空 - traceId = reqTraceId; - } else { - // 两者皆非空,不动请求头 - traceId = skywalkingTraceId; - } - TracerUtils.saveThreadTraceId(traceId); - log.debug("请求进入,添加traceId[{}]", traceId); - - filterChain.doFilter(httpServletRequest, httpServletResponse); - } finally { - // 请求结束,删除本地的链路流水号 - log.debug("请求结束,删除traceId[{}]", TracerUtils.getTraceId()); - TracerUtils.deleteThreadTraceId(); - } - } -}