RestTemplate opens the scope when handling requests, whereas RestClient does not. This will cause the custom ClientHttpRequestInterceptor for RestClient to be unable to access the currently active scope within the intercept method.
https://github.com/spring-projects/spring-framework/blob/2eff5cb46318eab8e1ed9a7abe27428bc6fb1477/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java#L896
https://github.com/spring-projects/spring-framework/blob/2eff5cb46318eab8e1ed9a7abe27428bc6fb1477/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java#L541
Comment From: prkksh
@KayWu, I reviewed the restClient exchange call and see that observability is opened there just in a different way. Reviewing the code gives an idea that observability is managed in both places in similar manner just implemented differently. Did you find any specific use case that you could explain for better understanding of your issue?
https://github.com/spring-projects/spring-framework/blob/main/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java#L531-L534
Comment From: KayWu
Hi @prkksh , DefaultRestClient starts the observation, but not activate the scope in the current thread, while RestTemplate does both things. We can see that both codes call observation.start(), but only RestTemplate calls observation.openScope.
https://github.com/spring-projects/spring-framework/blob/2eff5cb46318eab8e1ed9a7abe27428bc6fb1477/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java#L892-L904
https://github.com/spring-projects/spring-framework/blob/2eff5cb46318eab8e1ed9a7abe27428bc6fb1477/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java#L533-L544
When observation.openScope() is called, it activates the observation in the current thread, making any code executed later in the same thread aware of this observation. If a new observation is created, the existing observation will be treated as the parent observation.
However, if an observation is started but not activated with openScope(), subsequent code execution will not be aware of this observation.
I have implemented a ClientHttpRequestInterceptor, which performs some operations that create new observations. Since these operations are executed sequentially, the observations should also be linked together in sequence.
This works fine with RestTemplate, but it doesn't work with RestClient because RestClient does not activate the scope, causing these observations to be independent rather than linked.
Comment From: prkksh
PR for this