【问题标题】:traceId and spanId are coming as same throughout the loggerstraceId 和 spanId 在整个记录器中都相同
【发布时间】:2021-09-25 03:04:12
【问题描述】:

使用具有以下 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版本由于应用依赖而修复。

【问题讨论】:

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


    【解决方案1】:

    您没有遗漏任何内容,因为这反映了您通过 Spring Cloud Sleuth 的默认检测获得的行为。在跟踪中打开自己的 span 后,您会看到 span 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();
    }
    

    【讨论】:

    • 嗨 Gregor,这段代码应该在哪里,代码中的任何特定类型的类和“Span”正在使用哪个库。这也是侦探和勇敢行为的差异吗,因为在另一个 maven 中我不必这样做。
    • 这不是任何特定的配置。您可以在要打开新跨度的任何地方使用上面的代码。请查看Span lifecycle section in the Spring Cloud Sleuth documentation
    • 我尝试按照您的建议做同样的事情,但似乎给了我相同的结果 (traceid=spanId)
    • 进口勇敢.Span;导入勇敢。示踪剂;导入 org.springframework.beans.factory.annotation.Autowired; public class CustomisedSpan { private final static Logger logger = LogManager.getLogger(CustomisedSpan.class); @Autowired Tracer 跟踪器; public void customSpan() throws Throwable { Span span = this.tracer.nextSpan().name("customSpan");尝试 (Tracer.SpanInScope ws = this.tracer.withSpanInScope(span.start())) { logger.info("应该记录自定义跨度"); } 最后 { span.finish(); } } }
    • 打开自己的自定义跨度后,您绝对应该获得不同的跨度 ID。例如,如果将上述示例代码添加到 Web 控制器的任何处理程序方法中,则在调用处理程序方法时应该会看到不同的 span ID。
    猜你喜欢
    • 2020-08-21
    • 1970-01-01
    • 2021-08-31
    • 2021-04-17
    • 2023-03-17
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 2023-02-22
    相关资源
    最近更新 更多