traceId 和 spanId 在整个记录器中都相同

时间:2021-07-16 09:35:35

标签: java spring spring-boot spring-cloud spring-cloud-sleuth

使用具有以下 gradle 依赖项的 Spring boot 来获取 Sleuth 的跟踪和跨度,虽然我在我的日志中获得了跟踪和跨度 ID,但它们都是相同的,就像在控制器和服务类中一样。

gradle.build:

compile('org.springframework.boot:spring-boot-starter:2.1.4.RELEASE')
compile 'org.springframework.cloud:spring-cloud-starter-sleuth:2.1.4.RELEASE'

logback.xml:

<property name="CONSOLE_LOG_PATTERN"
          value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level  trace=%X{X-B3-TraceId} span=%X{X-B3-SpanId} MSG=%m%n"/>

方面类:

@Around("execution(*  com.test.common.controller.*.*(..))")
public Object controllerAspect(ProceedingJoinPoint joinPoint) throws Throwable {
    Long startTime = System.currentTimeMillis();
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes == null) {
        return joinPoint.proceed();
    }
    HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
    myLoggingServices(joinPoint, requestAttributes, httpServletRequest, startTime);
    return joinPoint.proceed();
}

控制台日志:

2021-07-16 13:41:56.008 [qtp1043203786-34] INFO   trace=474376a508632a04 span=474376a508632a04 qualifiedClass=com.test.common.controller.MyController time=14

2021-07-16 13:41:56.009 [qtp1043203786-34] INFO   trace=474376a508632a04 span=474376a508632a04 qualifiedClass=com.test.common.controller.MyController time=1

2021-07-16 13:41:56.291 [qtp1043203786-34] INFO   trace=474376a508632a04 span=474376a508632a04 qualifiedClass=com.test.common.service.impl.MyServiceImpl time=0

2021-07-16 13:41:56.292 [qtp1043203786-34] INFO   trace=474376a508632a04 span=474376a508632a04 qualifiedClass=com.test.common.service.impl.MyServiceImpl time=0

有什么需要补充的吗,请查看 Spring boot 版本由于应用程序依赖性而被修复。

1 个答案:

答案 0 :(得分:0)

您没有遗漏任何东西,因为这反映了您通过 Spring Cloud Sleuth 的默认检测获得的行为。一旦您在跟踪中打开自己的跨度,您将看到跨度 ID 将不同:

@Autowired
private Tracer tracer;

[...]

Span span = this.tracer.nextSpan().name("customSpan");
try (Tracer.SpanInScope ws = this.tracer.withSpan(span.start())) {
    [...]
    log.info("Should log custom span");
    [...]
}
finally {
    span.end();
}
相关问题