RKodakandla
RKodakandla

Reputation: 3494

Set new trace Id for each loop iteration instead of using existing trace id

I have a spring boot 3.1.x application with dependency on micrometer-tracing and micrometer-tracing-bridge-otel libraries.

There is a service bean that sends multiple http requests to a different service. With RestTemplate bean created using RestTemplateBuilder, I can see that the traceId from my service is sent to service#2 in the traceparent request header.

Lets consider this code.

@Configuration
public class AppConfig {

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder){
        return builder.build();
    }
}




@RestController
public class MyController {

    private final RestTemplate restTemplate;
  
    @RequestMapping("/hello")
    public String sayHello(){
        log.info("received request");

        for (int i =0; i < 3; i++){
            log.info("submitting request");
            restTemplate.getForObject("http://localhost:8080/hello", String.class );
            log.info("submitted request");
        }

        log.info("done with all requests");

        return "hello";
    }
}

Issue is that the same traceId value is used for all 3 requests sent from service#1 to service#2. This is causing issues with uniquely identifying all the logs from a specific request on service#2 and it's downstream dependencies.

I would like to send a different traceId for each request from service#1. In essence, I want to start a new trace within each iteration of the for loop. So any logs inside the loop and the traceparent header sent to service#2 will be new value. And once the for loop ends, remaining logs in the execution should havee the original trace context.

Upvotes: 0

Views: 110

Answers (0)

Related Questions