My spring boot service is giving different trace ids for external API calls for a same request trace. Expectation is these external http API requests are must be under same request trace id.
Please assist on this. I have create a sample reproducible code, here are the details:
Main class
@SpringBootApplication
public class RxjavaAndSpringSleuthApplication {
public static void main(String[] args) {
SpringApplication.run(RxjavaAndSpringSleuthApplication.class, args);
}
@Bean
public ExecutorService appExecutorService() {
return Executors.newCachedThreadPool();
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Service class
@Service
public class TestService {
@Autowired
private RestTemplate restTemplate;
@Autowired
private ExecutorService appExecutorService;
public Object getOutput() {
Single<Object> externalIOBoundApiCallOne = Single.fromSupplier(
() -> {
Future<Object> obj =
appExecutorService.submit(() -> {
return restTemplate.getForObject("https://random-data-api.com/api/address/random_address", Object.class);
});
return obj.get();
}
)
.timeout(5000, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.io())
.onErrorReturn(t -> appExecutorService.submit(() -> "ERROR"));
Single<Object> externalIOBoundApiCallTwo = Single.fromSupplier(
() -> {
Future<Object> obj =
appExecutorService.submit(() -> {
return restTemplate.getForObject("https://random-data-api.com/api/app/random_app", Object.class);
});
return obj.get();
}
)
.timeout(5000, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.io())
.onErrorReturn(t -> appExecutorService.submit(() -> "ERROR"));
Object result = Single.zip(externalIOBoundApiCallOne, externalIOBoundApiCallTwo,
(externalIOBoundApiCallOneResponse, externalIOBoundApiCallTwoResponse) -> externalIOBoundApiCallOneResponse)
.blockingGet();
System.out.println(result);
return result;
}
}
Controller class
@RestController
public class TestController {
private TestService service;
public TestController(TestService service) {
this.service = service;
}
@GetMapping("/get1")
public Object getRandomData() {
return service.getOutput();
}
}
application.properties
server.port=8080
spring.application.name=rxjava-and-spring-sleuth
spring.zipkin.baseUrl=http://localhost:9411/
spring.sleuth.sampler.probability=1.0
loggingpattern.level="%5p [rxjava-and-spring-sleuth, traceId=%X{X-B3-TraceId:-}, spanId=%X{X-B3-SpanId:-}, %X{X-Span-Export:-}, %X{clientName}]"
dependencies used
plugins {
id 'java'
id 'org.springframework.boot' version '2.6.0'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
/*java {
sourceCompatibility = '1.8'
}*/
repositories {
mavenCentral()
}
ext {
set('springCloudVersion', "2021.0.2")
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
dependencies {
implementation 'io.reactivex.rxjava3:rxjava:3.0.0'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
implementation 'org.springframework.cloud:spring-cloud-sleuth-zipkin'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Traces displayed in Grafana Tempo dashboard:
I'm struggling to solve this issue. Can someone please assist on this? thanks in advance!