Commit d16ca426b59af7b467844f422de241b2e3e4d3b4
1 parent
3d2961fa
不再产生默认流水号,以skywalking为准,删除多余的拦截器
Showing
3 changed files
with
1 additions
and
173 deletions
src/main/java/cn/iocoder/dashboard/framework/tracer/core/ITrace.java deleted
src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java
| 1 | package cn.iocoder.dashboard.framework.tracer.core.util; | 1 | package cn.iocoder.dashboard.framework.tracer.core.util; |
| 2 | 2 | ||
| 3 | -import cn.hutool.core.util.StrUtil; | ||
| 4 | -import cn.hutool.extra.spring.SpringUtil; | ||
| 5 | -import cn.iocoder.dashboard.framework.tracer.core.ITrace; | ||
| 6 | -import io.netty.util.concurrent.FastThreadLocal; | ||
| 7 | import org.apache.skywalking.apm.toolkit.trace.TraceContext; | 3 | import org.apache.skywalking.apm.toolkit.trace.TraceContext; |
| 8 | 4 | ||
| 9 | import java.util.UUID; | 5 | import java.util.UUID; |
| @@ -16,36 +12,6 @@ import java.util.UUID; | @@ -16,36 +12,6 @@ import java.util.UUID; | ||
| 16 | public class TracerUtils { | 12 | public class TracerUtils { |
| 17 | 13 | ||
| 18 | /** | 14 | /** |
| 19 | - * 维护请求线程对应的TraceId | ||
| 20 | - */ | ||
| 21 | - private final static FastThreadLocal<String> traceIdMap = new FastThreadLocal<>(); | ||
| 22 | - | ||
| 23 | - /** | ||
| 24 | - * 保存链路流水号 | ||
| 25 | - * | ||
| 26 | - * @param traceId 链路流水号 | ||
| 27 | - */ | ||
| 28 | - public static void saveThreadTraceId(String traceId) { | ||
| 29 | - traceIdMap.set(traceId); | ||
| 30 | - } | ||
| 31 | - | ||
| 32 | - /** | ||
| 33 | - * 根据线程获取链路流水号 | ||
| 34 | - * | ||
| 35 | - * @return 链路流水号 | ||
| 36 | - */ | ||
| 37 | - public static String getThreadTraceId() { | ||
| 38 | - return traceIdMap.get(); | ||
| 39 | - } | ||
| 40 | - | ||
| 41 | - /** | ||
| 42 | - * 根据线程删除链路流水 | ||
| 43 | - */ | ||
| 44 | - public static void deleteThreadTraceId() { | ||
| 45 | - traceIdMap.remove(); | ||
| 46 | - } | ||
| 47 | - | ||
| 48 | - /** | ||
| 49 | * 私有化构造方法 | 15 | * 私有化构造方法 |
| 50 | */ | 16 | */ |
| 51 | private TracerUtils() { | 17 | private TracerUtils() { |
| @@ -54,71 +20,13 @@ public class TracerUtils { | @@ -54,71 +20,13 @@ public class TracerUtils { | ||
| 54 | /** | 20 | /** |
| 55 | * 获得链路追踪编号 | 21 | * 获得链路追踪编号 |
| 56 | * <p> | 22 | * <p> |
| 57 | - * 一般来说,通过链路追踪编号,可以将访问日志,错误日志,链路追踪日志,logger 打印日志等,结合在一起,从而进行排错。 | 23 | + * 直接返回skywalking 的TraceId 如果不存在的话为空字符串"" |
| 58 | * <p> | 24 | * <p> |
| 59 | - * 默认情况下,我们使用 Apache SkyWalking 的 traceId 作为链路追踪编号。当然,可能会存在并未引入 Skywalking 的情况,此时使用 UUID 。 | ||
| 60 | - * 方法获取顺序: skywalking - > map -> 扩展接口 -> default | ||
| 61 | - * 项目整体获取traceId的优先级 skywalking > 自定义实现接口 > 默认 | ||
| 62 | - * map中的traceId 当且仅当其他情况为产生时才会出现. | ||
| 63 | * | 25 | * |
| 64 | * @return 链路追踪编号 | 26 | * @return 链路追踪编号 |
| 65 | */ | 27 | */ |
| 66 | public static String getTraceId() { | 28 | public static String getTraceId() { |
| 67 | - String traceId; | ||
| 68 | - // 通过 SkyWalking 获取链路编号 | ||
| 69 | - try { | ||
| 70 | - traceId = TraceContext.traceId(); | ||
| 71 | - if (StrUtil.isNotBlank(traceId)) { | ||
| 72 | - return traceId; | ||
| 73 | - } | ||
| 74 | - } catch (Throwable ignore) { | ||
| 75 | - } | ||
| 76 | - // 尝试从map中获取 | ||
| 77 | - traceId = traceIdMap.get(); | ||
| 78 | - if (StrUtil.isNotBlank(traceId)) { | ||
| 79 | - return traceId; | ||
| 80 | - } | ||
| 81 | - // 通过自定义扩展的tracer产生traceId, 在Spring容器加载完成前会获取不到对应的Bean | ||
| 82 | - ITrace tracer = null; | ||
| 83 | - try { | ||
| 84 | - tracer = getTracer(); | ||
| 85 | - } catch (Throwable ignore) { | ||
| 86 | - } | ||
| 87 | - if (null != tracer) { | ||
| 88 | - try { | ||
| 89 | - return tracer.getTraceId(); | ||
| 90 | - } catch (Throwable ignored) { | ||
| 91 | - } | ||
| 92 | - } | ||
| 93 | - // TODO 芋艿 多次调用会问题 | ||
| 94 | - | ||
| 95 | - return defaultTraceId(); | ||
| 96 | - } | ||
| 97 | - | ||
| 98 | - /** | ||
| 99 | - * 仅仅获取Skywalking 中的traceId, 若无skywalking,则会返回"" | ||
| 100 | - * | ||
| 101 | - * @return skywalking 的traceId | ||
| 102 | - */ | ||
| 103 | - public static String getSkywalkingTraceId() { | ||
| 104 | return TraceContext.traceId(); | 29 | return TraceContext.traceId(); |
| 105 | } | 30 | } |
| 106 | - /** | ||
| 107 | - * 从Spring 容器中获取 ITrace 类,返回可以为null | ||
| 108 | - * | ||
| 109 | - * @return ITrace | ||
| 110 | - */ | ||
| 111 | - private static ITrace getTracer() { | ||
| 112 | - return SpringUtil.getBean(ITrace.class); | ||
| 113 | - } | ||
| 114 | - | ||
| 115 | - /** | ||
| 116 | - * 默认生成TraceId规则为UUID | ||
| 117 | - * | ||
| 118 | - * @return UUID | ||
| 119 | - */ | ||
| 120 | - private static String defaultTraceId() { | ||
| 121 | - return "UUID:" + UUID.randomUUID().toString(); | ||
| 122 | - } | ||
| 123 | 31 | ||
| 124 | } | 32 | } |
src/main/java/cn/iocoder/dashboard/framework/tracer/filter/ServletTraceFilter.java deleted
| 1 | -package cn.iocoder.dashboard.framework.tracer.filter; | ||
| 2 | - | ||
| 3 | -import cn.hutool.core.util.StrUtil; | ||
| 4 | -import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils; | ||
| 5 | -import lombok.extern.slf4j.Slf4j; | ||
| 6 | -import org.springframework.beans.factory.annotation.Value; | ||
| 7 | -import org.springframework.lang.Nullable; | ||
| 8 | -import org.springframework.stereotype.Component; | ||
| 9 | -import org.springframework.web.servlet.HandlerInterceptor; | ||
| 10 | - | ||
| 11 | -import javax.servlet.*; | ||
| 12 | -import javax.servlet.http.HttpServletRequest; | ||
| 13 | -import javax.servlet.http.HttpServletResponse; | ||
| 14 | -import java.io.IOException; | ||
| 15 | - | ||
| 16 | -/** | ||
| 17 | - * 对Spring Mvc 的请求拦截, 添加traceId. | ||
| 18 | - * | ||
| 19 | - * @author mashu | ||
| 20 | - */ | ||
| 21 | - | ||
| 22 | -@Slf4j | ||
| 23 | -@Component | ||
| 24 | -public class ServletTraceFilter implements Filter { | ||
| 25 | - | ||
| 26 | - @Value("${cn.iocoder.tracer.name:global-trace-id}") | ||
| 27 | - private String traceIdName; | ||
| 28 | - | ||
| 29 | - @Override | ||
| 30 | - public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { | ||
| 31 | - HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; | ||
| 32 | - HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse; | ||
| 33 | - try { | ||
| 34 | - // 请求中traceId | ||
| 35 | - String reqTraceId = (String)httpServletRequest.getHeader(traceIdName); | ||
| 36 | - // skywalking中的traceId | ||
| 37 | - String skywalkingTraceId = TracerUtils.getSkywalkingTraceId(); | ||
| 38 | - String traceId ; | ||
| 39 | - if (null == reqTraceId && StrUtil.isBlank(skywalkingTraceId)) { | ||
| 40 | - // 两者皆空,添加默认的. | ||
| 41 | - traceId = TracerUtils.getTraceId(); | ||
| 42 | - httpServletResponse.setHeader(traceIdName, traceId); | ||
| 43 | - } else if (null == reqTraceId && StrUtil.isNotBlank(skywalkingTraceId)){ | ||
| 44 | - // 若请求空,则添加,为没有skywalking的系统添加一个TraceId | ||
| 45 | - traceId = skywalkingTraceId; | ||
| 46 | - httpServletResponse.setHeader(traceIdName, traceId); | ||
| 47 | - } else if (null != reqTraceId && StrUtil.isBlank(skywalkingTraceId)) { | ||
| 48 | - // 请求非空, skywalking为空 | ||
| 49 | - traceId = reqTraceId; | ||
| 50 | - } else { | ||
| 51 | - // 两者皆非空,不动请求头 | ||
| 52 | - traceId = skywalkingTraceId; | ||
| 53 | - } | ||
| 54 | - TracerUtils.saveThreadTraceId(traceId); | ||
| 55 | - log.debug("请求进入,添加traceId[{}]", traceId); | ||
| 56 | - | ||
| 57 | - filterChain.doFilter(httpServletRequest, httpServletResponse); | ||
| 58 | - } finally { | ||
| 59 | - // 请求结束,删除本地的链路流水号 | ||
| 60 | - log.debug("请求结束,删除traceId[{}]", TracerUtils.getTraceId()); | ||
| 61 | - TracerUtils.deleteThreadTraceId(); | ||
| 62 | - } | ||
| 63 | - } | ||
| 64 | -} |