Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get templated values in uri tag for http-client-requests metrics using Spring-Boot's RestTemplate?

We are using Spring Boot 2.1.4 and the micrometer-registry-prometheus dependency to capture metrics.

We have a case where a Spring Boot service uses a RestTemplate to call out to another service. The metrics being generated by this call contain the actual values in the URI instead of the templated values.

For example, in the /actuator/prometheus endpoint, I see entries like this:

http_client_requests_seconds_count{clientName="someClient",method="GET",status="200",uri="/person/lookup?firstName=Tony&lastName=Soprano",} 1.0

Based on the documentation, I would expect to see the variable names rather than the values, like this:

http_client_requests_seconds_count{clientName="someClient",method="GET",status="200",uri="/person/lookup?firstName={firstName}&lastName={lastName}",} 1.0

Is there a way to get the default http.client.requests metric values to use the templated values for the URI tag?

The Spring documentation at https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-metrics-http-clients says this about the uri tag:

Request’s URI template prior to variable substitution, if possible (for example, /api/person/{id})

How do we make it possible for the variable substitution to take place?

like image 404
David D Avatar asked Jan 26 '23 14:01

David D


1 Answers

I assume you are using the RestTemplateBuilder to build your RestTemplate as otherwise you wouldn't be getting the metrics registered.

Are you actually passing a templated url into RestTemplates exchange methods and pass along the params for subsbitution? Works-for-me on 2.1.4.RELEASE and 2.2.1.RELEASE.

    template.getForObject("http://localhost:" + this.serverPort + "/hello/{id}",
            String.class, Collections.singletonMap("id", "loop"));

Results in:

http_client_requests_seconds_count{application="micrometered2",clientName="localhost",method="GET",outcome="SUCCESS",status="200",uri="/hello/{id}",} 23.0
like image 166
mweirauch Avatar answered Jan 28 '23 03:01

mweirauch