简体   繁体   English

Spring Cloud Sleuth:将traceId传播到其他spring应用程序

[英]Spring Cloud Sleuth: Propagate traceId to other spring application

I have some spring service that can submit some AWS batch job. 我有一些Spring服务可以提交一些AWS批处理作业。 This is simple spring batch job that invoke requst to external service. 这是一个简单的spring批处理作业,它调用对外部服务的请求。 And i want to propagate traceId that generated in my service by including "org.springframework.cloud:spring-cloud-starter-sleuth" lib into classpath , to this job and add "TraceRestTemplateInterceptor" interceptor to external request initilaized with this traceId. 我想通过将“ org.springframework.cloud:spring-cloud-starter-sleuth” lib包含到classpath中,从而在服务中生成的traceId传播到此作业,并将“ TraceRestTemplateInterceptor”拦截器添加到由此traceId初始化的外部请求中。

How can i do that? 我怎样才能做到这一点? How can i initilaze interceptor which will put existing traceId from application parameter, environment, properties? 我如何初始化拦截器,该拦截器将从应用程序参数,环境,属性中放入现有的traceId? Or may be need to create some configuration beans? 还是可能需要创建一些配置bean?

UPDATE: 更新:

Simplified example: 简化示例:

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
   Logger logger = LoggerFactory.getLogger(DemoApplication.class);

   public static void main(String[] args) {
       SpringApplication.run(DemoApplication.class, args);
   }

   @Bean
   public RestTemplate restTemplate() {
     return new RestTemplate();
   }

   //@Autowired
   //RestTemplate restTemplate;

   @Override
   public void run(String... args) {
       logger.info("Hello, world!");
       //restTemplate.getForObject("some_url", String.class);
   }
}

File application.properties: 文件application.properties:

 x-b3-traceId=98519d97ce87553d

File build.gradle: 文件build.gradle:

 dependencies {
    implementation('org.springframework.cloud:spring-cloud-starter-sleuth')
 }

Output: 输出:

INFO [-,,,] 15048 --- [           main] com.example.demo.DemoApplication         : Hello, world!

First of all, I want to see here traceId which initilized in application.properties. 首先,我想在这里看到在application.properties中初始化的traceId。 Secondly, when uncomment resttemplate clause, this traceId propagated into request. 其次,当取消注释resttemplate子句时,此traceId传播到请求中。

Is it possible? 可能吗?

Resolved this issue only by manually putting into request HEADER key "X-B3-TRACEID" with corresponding value, which is inserted by external application as system property when submits target spring boot application. 仅通过手动将具有相应值的请求HEADER键“ X-B3-TRACEID”放入外部应用程序作为提交目标Spring Boot应用程序时的系统属性,即可解决此问题。 And manually inserting this key in MDC. 并手动将此密钥插入MDC。 Example, this snipet from spring boot application that must get traceId and propagate: 例如,来自Spring Boot应用程序的此代码段必须获取traceId并传播:

@Bean
public void setTraceIdToMDC(@Value("${x.b3.traceid}") String traceId) {
  MDC.put("x-b3-traceId", traceId);
}

@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}

@Bean
public CommandLineRunner commandLineRunnerer(RestTemplate restTemplate, @Value("${x.b3.traceid}") String traceId) {
    return args -> {
        MultiValueMap<String, String> header = new LinkedMultiValueMap<>();
        header.add("X-B3-TRACEID", traceId);

        HttpEntity httpEntity = new HttpEntity(header);

        logger.info("Execute some request"); //<-- prints expected traceId
        restTemplate.exchange("some_url", HttpMethod.GET, httpEntity, String.class);
    };
}

Just add the dependency to the classpath and set rest template as a bean. 只需将依赖项添加到类路径并将rest模板设置为bean。 That's enough. 够了

You can get the bean: 您可以获取bean:

@Autowired private Tracer tracer;

And get the traceId with 并获得traceId与

tracer.getCurrentSpan().traceIdString();

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

相关问题 Spring Cloud Sleuth v2.2.3 如何将 TraceId 传播到另一个线程 - Spring Cloud Sleuth v2.2.3 how to propagate TraceId to another thread Spring Cloud Sleuth中每个新的HTTP请求都具有不同的traceId - different traceId every new http request in spring cloud sleuth Spring 云侦探 3.0.1 使用 logback 在日志中生成 traceid 和 spanid - Spring cloud sleuth 3.0.1 generate traceid & spanid in logs using logback Sleuth 不会在 Spring Boot 应用程序中打印 spanId、traceId - Sleuth doesnt print spanId,traceId in spring boot application 从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 在模式布局中启用 MDC 属性以记录 TraceId - Spring-Cloud-Sleuth enable MDC properties in pattern layout to log TraceId Spring 侦探没有在 spring 调度程序内创建新的 traceid - Spring sleuth not creating new traceid inside a spring scheduler 使用Jetty服务器将Spring Cloud Sleuth添加到Spring应用程序 - Add spring cloud sleuth to Spring application with Jetty server 哪个记录 X-B3-SpanId 或 SpanId? X-B3-TraceId 还是 TraceId? (春天的侦探) - Which to log X-B3-SpanId or SpanId? X-B3-TraceId or TraceId? (Spring sleuth)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM