优化日志全链路追踪增加spanId和parentId
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
package com.central.gateway.filter;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.log.properties.TraceProperties;
|
||||
import org.slf4j.MDC;
|
||||
import com.central.log.trace.MDCTraceUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
@@ -31,10 +29,13 @@ public class TraceFilter implements GlobalFilter, Ordered {
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
if (traceProperties.getEnable()) {
|
||||
//链路追踪id
|
||||
String traceId = IdUtil.fastSimpleUUID();
|
||||
MDC.put(CommonConstant.LOG_TRACE_ID, traceId);
|
||||
MDCTraceUtils.addTrace();
|
||||
|
||||
ServerHttpRequest serverHttpRequest = exchange.getRequest().mutate()
|
||||
.headers(h -> h.add(CommonConstant.TRACE_ID_HEADER, traceId))
|
||||
.headers(h -> {
|
||||
h.add(MDCTraceUtils.TRACE_ID_HEADER, MDCTraceUtils.getTraceId());
|
||||
h.add(MDCTraceUtils.SPAN_ID_HEADER, MDCTraceUtils.getSpanId());
|
||||
})
|
||||
.build();
|
||||
|
||||
ServerWebExchange build = exchange.mutate().request(serverHttpRequest).build();
|
||||
|
||||
Reference in New Issue
Block a user