簡體   English   中英

traceId 和 spanId 在整個記錄器中都是一樣的

[英]traceId and spanId are coming as same throughout the loggers

使用具有以下 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 版本由於應用程序依賴性而被修復。

您沒有遺漏任何東西,因為這反映了您通過 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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM