spanId生成规则优化
This commit is contained in:
+1
-2
@@ -34,9 +34,8 @@ public class DubboTraceFilter implements Filter {
|
|||||||
} else { //服务消费者逻辑
|
} else { //服务消费者逻辑
|
||||||
String traceId = MDCTraceUtils.getTraceId();
|
String traceId = MDCTraceUtils.getTraceId();
|
||||||
if (StrUtil.isNotEmpty(traceId)) {
|
if (StrUtil.isNotEmpty(traceId)) {
|
||||||
String spanId = MDCTraceUtils.getSpanId();
|
|
||||||
invocation.setAttachment(MDCTraceUtils.KEY_TRACE_ID, traceId);
|
invocation.setAttachment(MDCTraceUtils.KEY_TRACE_ID, traceId);
|
||||||
invocation.setAttachment(MDCTraceUtils.KEY_SPAN_ID, spanId);
|
invocation.setAttachment(MDCTraceUtils.KEY_SPAN_ID, MDCTraceUtils.getNextSpanId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
|||||||
+3
-4
@@ -1,11 +1,11 @@
|
|||||||
package com.central.log.trace;
|
package com.central.log.trace;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.central.log.properties.TraceProperties;
|
import com.central.log.properties.TraceProperties;
|
||||||
import feign.RequestInterceptor;
|
import feign.RequestInterceptor;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
@@ -30,10 +30,9 @@ public class FeignTraceConfig {
|
|||||||
if (traceProperties.getEnable()) {
|
if (traceProperties.getEnable()) {
|
||||||
//传递日志traceId
|
//传递日志traceId
|
||||||
String traceId = MDCTraceUtils.getTraceId();
|
String traceId = MDCTraceUtils.getTraceId();
|
||||||
if (!StringUtils.isEmpty(traceId)) {
|
if (StrUtil.isNotEmpty(traceId)) {
|
||||||
String spanId = MDCTraceUtils.getSpanId();
|
|
||||||
template.header(MDCTraceUtils.TRACE_ID_HEADER, traceId);
|
template.header(MDCTraceUtils.TRACE_ID_HEADER, traceId);
|
||||||
template.header(MDCTraceUtils.SPAN_ID_HEADER, spanId);
|
template.header(MDCTraceUtils.SPAN_ID_HEADER, MDCTraceUtils.getNextSpanId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
+19
-7
@@ -1,10 +1,11 @@
|
|||||||
package com.central.log.trace;
|
package com.central.log.trace;
|
||||||
|
|
||||||
import cn.hutool.core.util.IdUtil;
|
import cn.hutool.core.util.IdUtil;
|
||||||
import cn.hutool.core.util.RandomUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||||
import org.slf4j.MDC;
|
import org.slf4j.MDC;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 日志追踪工具类
|
* 日志追踪工具类
|
||||||
@@ -47,13 +48,16 @@ public class MDCTraceUtils {
|
|||||||
*/
|
*/
|
||||||
public static final int FILTER_ORDER = -1;
|
public static final int FILTER_ORDER = -1;
|
||||||
|
|
||||||
|
private static final TransmittableThreadLocal<AtomicInteger> spanNumber = new TransmittableThreadLocal<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建traceId并赋值MDC
|
* 创建traceId并赋值MDC
|
||||||
*/
|
*/
|
||||||
public static void addTrace() {
|
public static void addTrace() {
|
||||||
String traceId = createTraceId();
|
String traceId = createTraceId();
|
||||||
MDC.put(KEY_TRACE_ID, traceId);
|
MDC.put(KEY_TRACE_ID, traceId);
|
||||||
MDC.put(KEY_SPAN_ID, traceId);
|
MDC.put(KEY_SPAN_ID, "0");
|
||||||
|
initSpanNumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -61,8 +65,8 @@ public class MDCTraceUtils {
|
|||||||
*/
|
*/
|
||||||
public static void putTrace(String traceId, String spanId) {
|
public static void putTrace(String traceId, String spanId) {
|
||||||
MDC.put(KEY_TRACE_ID, traceId);
|
MDC.put(KEY_TRACE_ID, traceId);
|
||||||
MDC.put(KEY_PARENT_ID, spanId);
|
MDC.put(KEY_SPAN_ID, spanId);
|
||||||
MDC.put(KEY_SPAN_ID, createTraceId());
|
initSpanNumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -84,13 +88,21 @@ public class MDCTraceUtils {
|
|||||||
public static void removeTrace() {
|
public static void removeTrace() {
|
||||||
MDC.remove(KEY_TRACE_ID);
|
MDC.remove(KEY_TRACE_ID);
|
||||||
MDC.remove(KEY_SPAN_ID);
|
MDC.remove(KEY_SPAN_ID);
|
||||||
MDC.remove(KEY_PARENT_ID);
|
spanNumber.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建traceId
|
* 创建traceId
|
||||||
*/
|
*/
|
||||||
public static String createTraceId() {
|
public static String createTraceId() {
|
||||||
return RandomUtil.randomString(16);
|
return IdUtil.getSnowflake().nextIdStr();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getNextSpanId() {
|
||||||
|
return StrUtil.format("{}.{}", getSpanId(), spanNumber.get().incrementAndGet());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void initSpanNumber() {
|
||||||
|
spanNumber.set(new AtomicInteger(0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -1,10 +1,10 @@
|
|||||||
package com.central.log.trace;
|
package com.central.log.trace;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.central.log.properties.TraceProperties;
|
import com.central.log.properties.TraceProperties;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
import org.springframework.web.filter.OncePerRequestFilter;
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
@@ -41,7 +41,7 @@ public class WebTraceFilter extends OncePerRequestFilter {
|
|||||||
try {
|
try {
|
||||||
String traceId = request.getHeader(MDCTraceUtils.TRACE_ID_HEADER);
|
String traceId = request.getHeader(MDCTraceUtils.TRACE_ID_HEADER);
|
||||||
String spanId = request.getHeader(MDCTraceUtils.SPAN_ID_HEADER);
|
String spanId = request.getHeader(MDCTraceUtils.SPAN_ID_HEADER);
|
||||||
if (StringUtils.isEmpty(traceId)) {
|
if (StrUtil.isEmpty(traceId)) {
|
||||||
MDCTraceUtils.addTrace();
|
MDCTraceUtils.addTrace();
|
||||||
} else {
|
} else {
|
||||||
MDCTraceUtils.putTrace(traceId, spanId);
|
MDCTraceUtils.putTrace(traceId, spanId);
|
||||||
|
|||||||
@@ -18,8 +18,8 @@
|
|||||||
|
|
||||||
<!-- 彩色日志格式 -->
|
<!-- 彩色日志格式 -->
|
||||||
<property name="CONSOLE_LOG_PATTERN"
|
<property name="CONSOLE_LOG_PATTERN"
|
||||||
value="[${APP_NAME}:${ServerIP}:${ServerPort}] %clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%level){blue} %clr(${PID}){magenta} %clr([%X{traceId},%X{spanId},%X{parentId}]){yellow} %clr([%thread]){orange} %clr(%-40.40logger{39}){cyan} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" />
|
value="[${APP_NAME}:${ServerIP}:${ServerPort}] %clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%level){blue} %clr(${PID}){magenta} %clr([%X{traceId}-%X{spanId}]){yellow} %clr([%thread]){orange} %clr(%-40.40logger{39}){cyan} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" />
|
||||||
<property name="CONSOLE_LOG_PATTERN_NO_COLOR" value="[${APP_NAME}:${ServerIP}:${ServerPort}] %d{yyyy-MM-dd HH:mm:ss.SSS} %level ${PID} [%X{traceId},%X{spanId},%X{parentId}] [%thread] %-40.40logger{39} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" />
|
<property name="CONSOLE_LOG_PATTERN_NO_COLOR" value="[${APP_NAME}:${ServerIP}:${ServerPort}] %d{yyyy-MM-dd HH:mm:ss.SSS} %level ${PID} [%X{traceId}-%X{spanId}] [%thread] %-40.40logger{39} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" />
|
||||||
|
|
||||||
<!-- 控制台日志 -->
|
<!-- 控制台日志 -->
|
||||||
<appender name="StdoutAppender" class="ch.qos.logback.core.ConsoleAppender">
|
<appender name="StdoutAppender" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ public class TraceFilter implements GlobalFilter, Ordered {
|
|||||||
ServerHttpRequest serverHttpRequest = exchange.getRequest().mutate()
|
ServerHttpRequest serverHttpRequest = exchange.getRequest().mutate()
|
||||||
.headers(h -> {
|
.headers(h -> {
|
||||||
h.add(MDCTraceUtils.TRACE_ID_HEADER, MDCTraceUtils.getTraceId());
|
h.add(MDCTraceUtils.TRACE_ID_HEADER, MDCTraceUtils.getTraceId());
|
||||||
h.add(MDCTraceUtils.SPAN_ID_HEADER, MDCTraceUtils.getSpanId());
|
h.add(MDCTraceUtils.SPAN_ID_HEADER, MDCTraceUtils.getNextSpanId());
|
||||||
})
|
})
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user