全局请求添加TraceId轻松看日志

2022-11-13 17:11:20 请求 添加 全局

引言

不知道大家有没有一堆日志就是定位不到那块是异常部分,接口错误无法复现,也找不到报错信息等比较棘手的问题。

其实解决上面的问题很简单,只要我们为每一个请求都分配一个唯一的 RequestId 或者叫 TraceId ,一旦出了问题,只需要拿着 Id 去日志里一搜,妖魔鬼怪立马原形毕露。

对于分布式链路追踪,有很多开源中间件,本文主要通过 logback 的 MDC 实现。

请求拦截器

@Component
public class TraceIdInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(httpservletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String traceId = String.fORMat("%s - %s",request.getRequestURI(), UUID.fastUUID().toString(true));
        MDC.put("traceId", traceId);
        return true;
 }

注册拦截器

@Component
@RequiredArgsConstructor
public class GlobalWEBmvcConfigurer implements WebMvcConfigurer {
    private final TraceIdInterceptor traceIdInterceptor;
    @Override
    public void addInterceptors(InterceptorReGIStry registry) {
        registry.addInterceptor(traceIdInterceptor);
    }
}

统一返回值

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonResponse<T> implements Serializable {
    
    private Integer code;
    
    private String message;
    
    private T Data;
    private String traceId;
    public CommonResponse(Integer code, String message) {
        this.code = code;
        this.message = message;
        this.traceId = MDC.get("traceId");
    }
}

日志配置

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="LOG_PATTERN"
              value="%d{yyyy-MM-dd} %d{HH:mm:ss.SSS} [%highlight(%-5level)] [%boldYellow(%X{traceId})] [%boldYellow(%thread)] %boldGreen(%logger{36} %F.%L) %msg%n">
    </property>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${LOG_PATTERN}</pattern>
        </encoder>
        <!-- 控制台打印INFO及以上级别的日志 -->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
    </appender>
    <root>
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

测试

接口返回值

{
  "code": 0,
  "message": "",
  "traceId": "/commerce-user/user/findById - 73e470120ef24d57a111de6b671d030b",
  "data": {
    "id": 1,
    "username": "test1",
    "passWord": "test",
    "extrainfo": "{}",
    "createTime": "2022-06-20T09:23:18.000+00:00",
    "updateTime": "2022-06-20T09:23:18.000+00:00",
    "balance": 0
  }
}

日志

如此,如果线上有接口出现问题,拿着 traceId 到日志文件搜索,就能检索到该请求的日志调用链路,处理问题就变得简单了。

异步调用配置

因为是 ThreadLocal ,异步任务必然是获取不到 traceId 的,需要再线程池配置中手动添加。

线程池配置

@Configuration
public class AsyncConfig implements AsyncConfigurer {
    @Bean
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(20);
        executor.setQueueCapacity(20);
        executor.seTKEepAliveSeconds(60);
        executor.setThreadNamePrefix("user-async-");   
        // 等待所有任务结果候再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        // 定义拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //设置线程装饰器
        executor.setTaskDecorator(runnable -> ThreadMdcUtils.wrapAsync(runnable, MDC.getCopyOfContextMap()));
        // 初始化线程池, 初始化 core 线程
        executor.initialize();
        return executor;
    }
}
public class ThreadMdcUtils {
    public static Runnable wrapAsync(Runnable task, Map<String,String> context){
        return () -> {
            if(context==null){
                MDC.clear();
            }else {
                MDC.setContextMap(context);
            }
            if(MDC.get("traceId")==null){
                MDC.put("traceId", UUID.fastUUID().toString(true));
            }
            try {
                task.run();
            }finally {
                MDC.clear();
            }
        };
    }
}

再次测试

以上就是全局请求添加TraceId轻松看日志的详细内容,更多关于全局请求添加TraceId的资料请关注其它相关文章!

相关文章