spanId生成规则优化
This commit is contained in:
+1
-2
@@ -34,9 +34,8 @@ public class DubboTraceFilter implements Filter {
|
||||
} else { //服务消费者逻辑
|
||||
String traceId = MDCTraceUtils.getTraceId();
|
||||
if (StrUtil.isNotEmpty(traceId)) {
|
||||
String spanId = MDCTraceUtils.getSpanId();
|
||||
invocation.setAttachment(MDCTraceUtils.KEY_TRACE_ID, traceId);
|
||||
invocation.setAttachment(MDCTraceUtils.KEY_SPAN_ID, spanId);
|
||||
invocation.setAttachment(MDCTraceUtils.KEY_SPAN_ID, MDCTraceUtils.getNextSpanId());
|
||||
}
|
||||
}
|
||||
try {
|
||||
|
||||
+3
-4
@@ -1,11 +1,11 @@
|
||||
package com.central.log.trace;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.central.log.properties.TraceProperties;
|
||||
import feign.RequestInterceptor;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@@ -30,10 +30,9 @@ public class FeignTraceConfig {
|
||||
if (traceProperties.getEnable()) {
|
||||
//传递日志traceId
|
||||
String traceId = MDCTraceUtils.getTraceId();
|
||||
if (!StringUtils.isEmpty(traceId)) {
|
||||
String spanId = MDCTraceUtils.getSpanId();
|
||||
if (StrUtil.isNotEmpty(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;
|
||||
|
||||
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 java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* 日志追踪工具类
|
||||
@@ -47,13 +48,16 @@ public class MDCTraceUtils {
|
||||
*/
|
||||
public static final int FILTER_ORDER = -1;
|
||||
|
||||
private static final TransmittableThreadLocal<AtomicInteger> spanNumber = new TransmittableThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 创建traceId并赋值MDC
|
||||
*/
|
||||
public static void addTrace() {
|
||||
String traceId = createTraceId();
|
||||
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) {
|
||||
MDC.put(KEY_TRACE_ID, traceId);
|
||||
MDC.put(KEY_PARENT_ID, spanId);
|
||||
MDC.put(KEY_SPAN_ID, createTraceId());
|
||||
MDC.put(KEY_SPAN_ID, spanId);
|
||||
initSpanNumber();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,13 +88,21 @@ public class MDCTraceUtils {
|
||||
public static void removeTrace() {
|
||||
MDC.remove(KEY_TRACE_ID);
|
||||
MDC.remove(KEY_SPAN_ID);
|
||||
MDC.remove(KEY_PARENT_ID);
|
||||
spanNumber.remove();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建traceId
|
||||
*/
|
||||
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;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.central.log.properties.TraceProperties;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@@ -41,7 +41,7 @@ public class WebTraceFilter extends OncePerRequestFilter {
|
||||
try {
|
||||
String traceId = request.getHeader(MDCTraceUtils.TRACE_ID_HEADER);
|
||||
String spanId = request.getHeader(MDCTraceUtils.SPAN_ID_HEADER);
|
||||
if (StringUtils.isEmpty(traceId)) {
|
||||
if (StrUtil.isEmpty(traceId)) {
|
||||
MDCTraceUtils.addTrace();
|
||||
} else {
|
||||
MDCTraceUtils.putTrace(traceId, spanId);
|
||||
|
||||
Reference in New Issue
Block a user