I'm trying to monitor rest template metrics for my spring boot application via micrometer & prometheus. When I use Resttemplate built with ResttemplateBuilder and use to call for another api, it did get the expected http.client.requests metrics. But for AsyncResttemplate when I create with AsyncResttemplate and use it to call for another api, It didn't provide any http.client.requests metrics.
This is the code when i create the AsyncResttemplate bean
@Bean
public AsyncRestTemplate asyncRestTemplate(){
return new AsyncRestTemplate();
}
This is the code when I call another api with async
public ListenableFuture async() {
ListenableFuture<ResponseEntity<AccountResponse>> accountResponseList = asyncRestTemplate.exchange(accountUrl, HttpMethod.GET, new HttpEntity<>(new HttpHeaders()), AccountResponse.class);
accountResponseList.addCallback(new ListenableFutureCallback<ResponseEntity<AccountResponse>>() {
@Override
public void onSuccess(ResponseEntity<AccountResponse> accountResponseResponseEntity) {
System.out.println("Success");
}
@Override
public void onFailure(Throwable throwable) {
System.out.println("Failure");
}
});
return accountResponseList;
}
And these are the relevant dependencies imported in pom.xml
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
This is what i expect to get from micrometer-prometheus metrics
# HELP http_client_requests_seconds Timer of RestTemplate operation
# TYPE http_client_requests_seconds summary
http_client_requests_seconds_count{application="${spring.application.name}",clientName="localhost",method="GET",status="200",uri="/getAllAccount",} 1.0
http_client_requests_seconds_sum{application="${spring.application.name}",clientName="localhost",method="GET",status="200",uri="/getAllAccount",} 0.0242929
# HELP http_client_requests_seconds_max Timer of RestTemplate operation
# TYPE http_client_requests_seconds_max gauge
http_client_requests_seconds_max{application="${spring.application.name}",clientName="localhost",method="GET",status="200",uri="/getAllAccount",} 0.0242929
The metrics above are from ResttemplateBuilder, is there a way that these can be obtained for AsyncRestTemplate too?
Update: From M. Deinum's advice I change the bean to
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.setConnectTimeout(Duration.ofSeconds(500)).build();
}
@Bean
public AsyncRestTemplate asyncRestTemplate(RestTemplate restTemplate){
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
SimpleAsyncTaskExecutor asyncTaskExecutor = new SimpleAsyncTaskExecutor();
asyncTaskExecutor.setConcurrencyLimit(10);
requestFactory.setTaskExecutor(asyncTaskExecutor);
return new AsyncRestTemplate(requestFactory, restTemplate);
}
But still not get any http.client.requests from async call