I think you can solve this inside you Spring-Boot application by manipulating the tags for the http_server_requests
metric.
The http_server_requests
metric is created by a Timer.Sample
in the WebMvcMetricsFilter
and the tags are added by the DefaultWebMvcTagsProvider
.
If you override the DefaultWebMvcTagsProvider
you are able to add any info out from the HttpServletRequest
as a new tag OR override the existing uri tag.
For instance, a new tag with the getRequestURI
value which I think should solve your problem. Then you can apply any filtering of the tag value to get it exactly as you want it.
I did a test with where I have both custom-uri="/rest/customer/1"
and uri="/rest/customer/{id}
as a tag.
http_server_requests_seconds_count{exception="None",custom-uri="/rest/customer/1",host="customer-service",method="GET",outcome="SUCCESS",status="200",uri="/rest/customer/{id}",} 2.0
Example code:
The WebMvcTagsProvider
:
public class CustomWebMvcTagsProvider extends DefaultWebMvcTagsProvider
{
public CustomWebMvcTagsProvider(boolean ignoreTrailingSlash, List<WebMvcTagsContributor> contributors) {
super(ignoreTrailingSlash, contributors);
}
@Override
public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response, Object handler,
Throwable exception) {
var tags = super.getTags(request, response, handler, exception);
// Override the existing uri tag by using "uri" as key name
return Tags.of(tags).and("custom-uri", request.getRequestURI());
}
}
and the Configuration file
@Configuration()
@EnableConfigurationProperties(MetricsProperties.class)
public class CustomWebMvcMetricsAutoConfiguration {
private final MetricsProperties properties;
public CustomWebMvcMetricsAutoConfiguration(MetricsProperties properties) {
this.properties = properties;
}
@Bean
public CustomWebMvcTagsProvider webMvcTagsProvider(
ObjectProvider<WebMvcTagsContributor> contributors) {
return new CustomWebMvcTagsProvider(this.properties.getWeb().getServer().getRequest().isIgnoreTrailingSlash(),
contributors.orderedStream().collect(Collectors.toList()));
}
}