spring 千分尺跟踪:为什么spanId是traceId的一部分,并且长度为32个字符而不是16个?

eh57zj3b  于 2023-11-16  发布在  Spring
关注(0)|答案(1)|浏览(211)

AFAIK,spanIdtraceId进入MDC是16个字符的字符串。但在我的应用程序中,我注意到spanId是32个字符的traceId的一部分。下面是一个相同的示例。
2023-10-31,[traceId=6540ee3f2997f144f45483631005208e,spanId=f45483631005208e] [http-nio-8085-exec-1] INFO com.example.trace.Controller - Inside Hi
我使用的是千分尺跟踪v1.1.6,Brave v5.15.1是我的供应商。

server.port=8085
spring.application.name=exampleApp

logging.pattern.console=%d{yyyy-MM-dd }, [[[%mdc]]] [%thread] %-5level %logger{36} - %msg%n

management.tracing.sampling.probability=0.0
management.tracing.propagation.type=B3_MULTI

logging.level.root=info

字符串
我试着在调试模式下查看东西,找到了生成它的方法:

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);
}


然而,这并没有帮助我太多。
如何将16个字符的traceId添加到MDC中,而不是将32个字符的traceId+spanId作为traceId

o0lyfsai

o0lyfsai1#

这是因为在BraveAutoConfiguration中,Tracing对象是用.traceId128Bit(true)构建的。
为了覆盖这个,我们必须在我们的应用程序中定义自定义Tracing bean,而不是依赖于Spring自动配置。
以下是Bean:

package com.example.trace;

import brave.Tracing;
import brave.TracingCustomizer;
import brave.handler.SpanHandler;
import brave.propagation.CurrentTraceContext;
import brave.propagation.Propagation;
import brave.sampler.Sampler;
import org.springframework.boot.actuate.autoconfigure.tracing.TracingProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.util.List;

@Configuration
@EnableConfigurationProperties(TracingProperties.class)
public class BraveAutoConf {

    @Bean
    public Tracing braveTracing(Environment environment, TracingProperties properties, List<SpanHandler> spanHandlers,
                                List<TracingCustomizer> tracingCustomizers, CurrentTraceContext currentTraceContext,
                                Propagation.Factory propagationFactory, Sampler sampler) {

        //NOTE: skipped the type/produce/consume=W3C and span joining property compatibility check. (This is invalid config)

        String applicationName = environment.getProperty("spring.application.name", "AppName");
        Tracing.Builder builder = Tracing.newBuilder()
                .currentTraceContext(currentTraceContext)
                .traceId128Bit(false)
                .supportsJoin(properties.getBrave().isSpanJoiningSupported())
                .propagationFactory(propagationFactory)
                .sampler(sampler)
                .localServiceName(applicationName);
        spanHandlers.forEach(builder::addSpanHandler);
        for (TracingCustomizer tracingCustomizer : tracingCustomizers) {
            tracingCustomizer.customize(builder);
        }
        return builder.build();
    }

}

字符串
这将生成16个字符的traceId,而不是32个字符的trace+spanId。但是,请注意,traceId和第一个spanId将与此配置相同。

相关问题