简体   繁体   English

侦探日志traceId未传播到另一个服务

[英]Sleuth log traceId not propagated to another service

I try to add a distributed tracing in my microservices (under Kubernetes in Azure). 我尝试在我的微服务中添加分布式跟踪(在Azure的Kubernetes下)。

I added the dependencies in the parent pom.xml : 我在父pom.xml中添加了依赖项:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
    <relativePath/>
</parent>
<dependencies>
    {...}
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-sleuth-core</artifactId>
        <version>1.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-sleuth-zipkin</artifactId>
        <version>1.1.3.RELEASE</version>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>CAMDEN.SR4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        {...}
    </dependencies>
</dependencyManagement>

I use 1.4.1 and CAMDEN.SR4 because fabric8 kubeflix doesn't support newer versions. 我使用1.4.1和CAMDEN.SR4,因为fabric8 kubeflix不支持较新的版本。 I forced 1.1.3.RELEASE to try newest sleuth version to see if it was a bug in older version of sleuth. 我强迫1.1.3.RELEASE尝试最新版本的侦探,以查看它是否是较早版本的侦探中的错误。

I use this configuration of logback-spring.xml : 我使用logback-spring.xml的此配置:

<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <springProperty scope="context" name="springAppName" source="spring.application.name"/>
    <jmxConfigurator/>
    <property name="CONSOLE_LOG_PATTERN"
          value="%d{yyyy-MM-dd HH:mm:ss.SSS} [${springAppName},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}] [%thread] %-5level %logger{35} - %msg%n"/>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>
    <root level="DEBUG">
        <appender-ref ref="STDOUT"/>
    </root>
    <logger name="org.springframework" level="ERROR"/>
    <logger name="com.netflix" level="ERROR"/>
    <logger name="io.fabric8" level="ERROR"/>
    <logger name="org.apache" level="ERROR"/>
</configuration>

And here is my application.yml : 这是我的application.yml:

spring:
  zipkin:
    baseUrl: http://zipkin:8080
  sleuth:
    sampler:
      percentage: 1.0
server:
  port: 8080

The zipkin URL is a Kubernetes services exposing the Zipkin server (Spring boot app with @EnableZipkinServer) zipkin URL是一个Kubernetes服务,用于公开Zipkin服务器(带有@EnableZipkinServer的Spring boot应用)

I then call a first service (services-1) with this code : 然后,我用以下代码调用第一个服务(services-1):

private RestTemplate template = new RestTemplate();

@GetMapping("/{key}/{language}")
public String getLabel(@PathVariable String key, @PathVariable String language) throws UnknownHostException {
    log.info("Entering getLabel");
    String testResponse = template.getForObject("http://services-i18n-2/test", String.class);
    String s = labelService.getLabel(key, language) + " " + message + " " + InetAddress.getLocalHost().getHostName() + ", response=" + testResponse;
    log.info("Exiting getLabel");
    return s;
}

which produces these logs : 产生这些日志:

2017-04-05T11:04:48.497345669Z 2017-04-05 11:04:48.497 [services-1,eaf3dbcb2f92091b,95dd9e6082990923,false] [XNIO-2 task-4] INFO  c.l.m.i18n.web.LabelController - Entering getLabel
2017-04-05T11:04:48.519851116Z 2017-04-05 11:04:48.519 [services-1,eaf3dbcb2f92091b,95dd9e6082990923,false] [XNIO-2 task-4] DEBUG c.l.m.i18n.service.LabelService - Response Label(key=Key(value=action.login), language=Language(value=fr), value=s'authentifier)
2017-04-05T11:04:48.519864516Z 2017-04-05 11:04:48.519 [services-1,eaf3dbcb2f92091b,95dd9e6082990923,false] [XNIO-2 task-4] INFO  c.l.m.i18n.web.LabelController - Exiting getLabel

As you can see it calls the services-i18n-2 service with a RestTemplate, which produces these logs : 如您所见,它使用RestTemplate调用services-i18n-2服务,该服务生成以下日志:

2017-04-05T11:04:48.514145894Z 2017-04-05 11:04:48.513 [services-2,e0c6495a0a598cff,e0c6495a0a598cff,true] [XNIO-2 task-4] INFO  c.l.m.i18n.web.TestController - Entering test
2017-04-05T11:04:48.516430459Z 2017-04-05 11:04:48.516 [services-2,e0c6495a0a598cff,e0c6495a0a598cff,true] [XNIO-2 task-4] INFO  c.l.m.i18n.web.TestController - Exiting test

As you can see the traceId in service-2 (e0c6495a0a598cff) is different from the service-1 (eaf3dbcb2f92091b). 如您所见,service-2(e0c6495a0a598cff)中的traceId与service-1(eaf3dbcb2f92091b)不同。

And in service-2 the traceId is the same as the spanId. 在service-2中,traceId与spanId相同。

Questions : 问题:

  • Why don't I have the traceId propagated to service-2 to be able to see the full stacktrace in Zipkin ? 为什么我没有将traceId传播到service-2才能看到Zipkin中的完整stacktrace?
  • Why the traceId in service-2 is equal to the spanId ? 为什么service-2中的traceId等于spanId?
  • Why the exportable is false in service-1 ? 为什么在service-1中exportable是false? These logs are not seen by the Zipkin server. Zipkin服务器看不到这些日志。
  • I'm spammed with rxjava spans in Zipkin server. 我在Zipkin服务器中被rxjava span淹没了。 I have tried spring.sleuth.rxjava.schedulers.ignoredthreads=rxjava but it still goes to Zipkin. 我已经尝试过spring.sleuth.rxjava.schedulers.ignoredthreads = rxjava,但它仍然适用于Zipkin。

FYI, I have Hystrix in the dependencies and I have removed the @HystrixCommand to be sure it was not a problem with Hystrix creating a new traceId at each HTTP call. 仅供参考,我在依赖项中包含Hystrix,并删除了@HystrixCommand,以确保Hystrix在每个HTTP调用中创建新的traceId时没有问题。

For 1, 2, 3 it was because I was doing a new RestTemplate. 对于1、2、3,这是因为我正在做一个新的RestTemplate。

The doc says : 医生说:

You have to register RestTemplate as a bean so that the interceptors will get injected. 您必须将RestTemplate注册为Bean,以便注入拦截器。 If you create a RestTemplate instance with a new keyword then the instrumentation WILL NOT work. 如果您使用新关键字创建RestTemplate实例,则检测将不起作用。

So RTFM for myself, and this solved my 3 first problems : 因此,RTFM自己解决了我的第三个问题:

@Bean
public RestTemplate template() {
    return new RestTemplate();
}
@Autowired
private RestTemplate template;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Sleuth traceId 和 spanId 不会传播到 parallelStream 工作线程 - Sleuth traceId and spanId are not propagated to parallelStream worker threads 在 Sleuth 的日志中看不到 traceId 和 spanId - Can't see traceId and spanId in log for Sleuth 哪个记录 X-B3-SpanId 或 SpanId? X-B3-TraceId 还是 TraceId? (春天的侦探) - Which to log X-B3-SpanId or SpanId? X-B3-TraceId or TraceId? (Spring sleuth) Spring-Cloud-Sleuth 在模式布局中启用 MDC 属性以记录 TraceId - Spring-Cloud-Sleuth enable MDC properties in pattern layout to log TraceId Sleuth Header X-B3-TraceId 值会重新生成,即使作为 Bluemix 中的标头传递给服务请求 - Sleuth Header X-B3-TraceId value get Regenerated even if passed to the Service Request as Header In Bluemix Spring Cloud Sleuth v2.2.3 如何将 TraceId 传播到另一个线程 - Spring Cloud Sleuth v2.2.3 how to propagate TraceId to another thread 如何在 SQS 中发送和使用 sleuth traceId - How to send and consume sleuth traceId in SQS 从Kafka消息在春季侦探中注入TraceId - Inject TraceId in spring sleuth from Kafka message 如何使用 opentelemetry 在 Spring sleuth 中设置自定义 traceId - How to set custom traceId in Spring sleuth with opentelemetry Spring Cloud Sleuth:将traceId传播到其他spring应用程序 - Spring Cloud Sleuth: Propagate traceId to other spring application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM