1 版本
Sleuth版本:3.1.0
ZipKin版本:io.zipkin.brave:5.13.2
Logback版本:1.2.3
2 启动SpringBoot
2.1 构建MDCScopeDecorator属性
brave.baggage.CorrelationScopeDecorator.Builder
2.2 构建对象:MDCScopeDecorator
brave.context.slf4j.MDCScopeDecorator.Builder
2.3 填充属性:traceId和spanId
brave.baggage.CorrelationScopeDecorator.Builder
2.4 traceId和spanId
brave.baggage.BaggageFields
3 请求接口并打印日志
3.1 生成traceId
3.1.1 初始值
brave.Tracer#nextSpan(brave.propagation.TraceContextOrSamplingFlags)
3.1.2 生成span
brave.Tracer#_toSpan
brave.Tracer#_toSpan
3.1.3 生成decorateContext
brave.Tracer#decorateContext(int, long, long, long, long, long, java.util.List<java.lang.Object>)
3.1.4 生成spanId
随机生成数据: brave.Tracer#nextId
3.1.5 生成traceId
3.1.6 生成localRootId
3.1.7 生成span
brave.http.HttpServerHandler#handleReceive(brave.http.HttpServerRequest)
3.1.8 添加请求属性
org.springframework.cloud.sleuth.instrument.web.servlet.TracingFilter#doFilter
3.2 实际traceId
3.2.1 MDC添加traceId和值
根据MDCScopeDecorator属性个数配置 brave.baggage.CorrelationScopeDecorator.Builder#build
3.2.2 生成最终的traceId
brave.baggage.CorrelationScopeDecorator.Multiple#decorateScope
brave.propagation.TraceContext#traceIdString
public String traceIdString() {
String r = traceIdString;
if (r == null) {
r = toTraceIdString(traceIdHigh, traceId);
traceIdString = r;
}
return r;
}
static String toTraceIdString(long traceIdHigh, long traceId) {
if (traceIdHigh != 0) {
char[] result = RecyclableBuffers.parseBuffer();
writeHexLong(result, 0, traceIdHigh);
writeHexLong(result, 16, traceId);
return new String(result, 0, 32);
}
return toLowerHex(traceId);
}
public static void writeHexLong(char[] data, int pos, long v) {
writeHexByte(data, pos + 0, (byte) ((v >>> 56L) & 0xff));
writeHexByte(data, pos + 2, (byte) ((v >>> 48L) & 0xff));
writeHexByte(data, pos + 4, (byte) ((v >>> 40L) & 0xff));
writeHexByte(data, pos + 6, (byte) ((v >>> 32L) & 0xff));
writeHexByte(data, pos + 8, (byte) ((v >>> 24L) & 0xff));
writeHexByte(data, pos + 10, (byte) ((v >>> 16L) & 0xff));
writeHexByte(data, pos + 12, (byte) ((v >>> 8L) & 0xff));
writeHexByte(data, pos + 14, (byte) (v & 0xff));
}
static void writeHexByte(char[] data, int pos, byte b) {
data[pos + 0] = HEX_DIGITS[(b >> 4) & 0xf];
data[pos + 1] = HEX_DIGITS[b & 0xf];
}
3.3 更新traceId和spanId值
brave.baggage.CorrelationScopeDecorator.Multiple#decorateScope
3.4 更新方法
brave.context.slf4j.MDCScopeDecorator.MDCContext#update
3.5 添加MDCAdapter
org.slf4j.MDC#put
3.6 logbackMDCAdapter添加traceId
ch.qos.logback.classic.util.LogbackMDCAdapter#put
3.7 threadLocal中生成span
brave.propagation.ThreadLocalCurrentTraceContext#newScope
3.8 路径过滤
org.springframework.cloud.sleuth.instrument.web.servlet.TracingFilter#doFilter
3.9 输出日志
3.10 传输traceId和spanId
ch.qos.logback.classic.util.LogbackMDCAdapter#duplicateAndInsertNewMap
4 日志中生成traceId和spanId
5 小结
- SpringBoot启动时在zipkin的包中配置MDCScopeDecorator属性,添加traceId和spanId;
- Sleuth版本:3.1.0:生成traceId和spanId直接使用traceId和spanId获取,日志格式为:[traceId=%X{traceId:-}, spanId=%X{spanId:-}]
- Logback天然配置MDC,通过LogbackMDCAdapter最终填充traceId和spanId值。
|