Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RestClient doesn't open a scope for the processing of the request #33397

Closed
KayWu opened this issue Aug 16, 2024 · 3 comments
Closed

RestClient doesn't open a scope for the processing of the request #33397

KayWu opened this issue Aug 16, 2024 · 3 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@KayWu
Copy link

KayWu commented Aug 16, 2024

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.

try (Observation.Scope scope = observation.openScope()){

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 16, 2024
@bclozel bclozel added in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 16, 2024
@bclozel bclozel self-assigned this Aug 16, 2024
@bclozel bclozel added this to the 6.1.13 milestone Aug 16, 2024
@prkksh
Copy link

prkksh commented Aug 16, 2024

@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

@KayWu
Copy link
Author

KayWu commented Aug 17, 2024

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.

Observation observation = ClientHttpObservationDocumentation.HTTP_CLIENT_EXCHANGES.observation(
this.observationConvention, DEFAULT_OBSERVATION_CONVENTION,
() -> observationContext, this.observationRegistry).start();
ClientHttpResponse response = null;
try (Observation.Scope scope = observation.openScope()){
if (requestCallback != null) {
requestCallback.doWithRequest(request);
}
response = request.execute();
observationContext.setResponse(response);
handleResponse(url, method, response);
return (responseExtractor != null ? responseExtractor.extractData(response) : null);
}

observation = ClientHttpObservationDocumentation.HTTP_CLIENT_EXCHANGES.observation(observationConvention,
DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, observationRegistry).start();
if (this.body != null) {
this.body.writeTo(clientRequest);
}
if (this.httpRequestConsumer != null) {
this.httpRequestConsumer.accept(clientRequest);
}
clientResponse = clientRequest.execute();
observationContext.setResponse(clientResponse);
ConvertibleClientHttpResponse convertibleWrapper = new DefaultConvertibleClientHttpResponse(clientResponse, observation);
return exchangeFunction.exchange(clientRequest, convertibleWrapper);

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.

@prkksh
Copy link

prkksh commented Aug 18, 2024

PR for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants