Skip to content

Client observations URI template missing when using RestTemplateAdapter #31144

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

Closed
felzan opened this issue Aug 31, 2023 · 3 comments
Closed

Client observations URI template missing when using RestTemplateAdapter #31144

felzan opened this issue Aug 31, 2023 · 3 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) theme: observability An issue related to observability and tracing type: bug A general bug
Milestone

Comments

@felzan
Copy link

felzan commented Aug 31, 2023

Affects: 6.1

I think it would be better if RequestEntity was build with uriTemplate when available.
I was playing around with Http Interface, RestTemplate and metrics and could see that the URI was not showing properly in the metric. ex:

http_client_requests_seconds_count{client_name="jsonplaceholder.typicode.com",error="none",exception="none",method="GET",outcome="SUCCESS",status="200",uri="none",} 1.0

When building RequestEntity using uriTemplate it shows correctly. ex:

http_client_requests_seconds_count{client_name="jsonplaceholder.typicode.com",error="none",exception="none",method="GET",outcome="SUCCESS",status="200",uri="/todos/{id}",} 1.0

It seems that it was planned using uriTemplate #30117 (comment)
I'll try to work on this 😀

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 31, 2023
@bclozel
Copy link
Member

bclozel commented Aug 31, 2023

@felzan sorry but I don't understand this issue. Can you show how you are using the http interface client, what you are getting as metrics and what you were expecting instead?

@bclozel bclozel added status: waiting-for-feedback We need additional information before we can continue in: web Issues in web modules (web, webmvc, webflux, websocket) labels Aug 31, 2023
@felzan
Copy link
Author

felzan commented Aug 31, 2023

Sure!
From my understanding, this doc says that using RestTemplateBuilder I'll get ObservationRegistry on the RestTemplate and uri will be set as low cardinality key using the uriTemplate.
But using RestTemplateAdapter when configuring Http Interface will build the RequestEntity using the expanded uri so no uriTemplate will be set. This will provoke uri to be none in the metrics.

This is how I'm configuring http interface

    @Bean
public JsonPlaceholderHttpClient jsonPlaceholderHttpClient(RestTemplateBuilder restTemplateBuilder) {
    RestTemplate restTemplate = restTemplateBuilder.build();
    restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory("https://jsonplaceholder.typicode.com"));
    RestTemplateAdapter adapter = RestTemplateAdapter.create(restTemplate);
    HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();
    return factory.createClient(JsonPlaceholderHttpClient.class);
}
@HttpExchange(value = "todos")
public interface JsonPlaceholderHttpClient {

    @GetExchange(value = "{id}")
    Todo getTodoById(@PathVariable String id);
}

The metrics output from /actuator/metrics/http.client.requests shows uri as none

And if I set http interface using a custom adapter it works

@Bean
public JsonPlaceholderHttpClient jsonPlaceholderHttpClient(RestTemplateBuilder restTemplateBuilder) {
    RestTemplate restTemplate = restTemplateBuilder.build();
    restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory("https://jsonplaceholder.typicode.com"));
    CustomRestTemplateAdapter adapter = CustomRestTemplateAdapter.create(restTemplate);
    //CustomRestTemplateAdapter.newRequest -> RequestEntity.BodyBuilder builder = RequestEntity.method(httpMethod, values.getUriTemplate(), values.getUriVariables());
    HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();
    return factory.createClient(JsonPlaceholderHttpClient.class);
}

Now the metrics output show uri as "/todos/{id}"

If you need further clarification let me know

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Aug 31, 2023
@bclozel bclozel self-assigned this Sep 1, 2023
@bclozel bclozel added type: bug A general bug theme: observability An issue related to observability and tracing and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels Sep 4, 2023
@bclozel bclozel added this to the 6.1.0-RC1 milestone Sep 4, 2023
@bclozel bclozel changed the title Build RequestEntity with uriTemplate when available in RestTemplateAdapter Client observations URI template missing when using RestTemplateAdapter Sep 4, 2023
@bclozel
Copy link
Member

bclozel commented Sep 4, 2023

Thanks @felzan for testing our milestones, this is really helpful!

@bclozel bclozel closed this as completed in 5fb567a Sep 4, 2023
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) theme: observability An issue related to observability and tracing type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants