Normalize micrometer client tag URIs
Update `MetricsClientHttpRequestInterceptor` so that captured URIs are normalize to always contain a leading slash. Fixes gh-11798
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -97,10 +97,15 @@ class MetricsClientHttpRequestInterceptor implements ClientHttpRequestIntercepto
|
||||
|
||||
private Timer.Builder getTimeBuilder(HttpRequest request,
|
||||
ClientHttpResponse response) {
|
||||
String url = ensureLeadingSlash(urlTemplate.get());
|
||||
return Timer.builder(this.metricName)
|
||||
.tags(this.tagProvider.getTags(urlTemplate.get(), request, response))
|
||||
.tags(this.tagProvider.getTags(url, request, response))
|
||||
.description("Timer of RestTemplate operation")
|
||||
.publishPercentileHistogram(this.recordPercentiles);
|
||||
}
|
||||
|
||||
private String ensureLeadingSlash(String url) {
|
||||
return (url.startsWith("/") ? url : "/" + url);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -47,18 +47,23 @@ public class MetricsRestTemplateCustomizerTests {
|
||||
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
private MockRestServiceServer mockServer;
|
||||
|
||||
private MetricsRestTemplateCustomizer customizer;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, new MockClock());
|
||||
this.restTemplate = new RestTemplate();
|
||||
this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
|
||||
this.customizer = new MetricsRestTemplateCustomizer(this.registry,
|
||||
new DefaultRestTemplateExchangeTagsProvider(), "http.client.requests",
|
||||
true);
|
||||
this.customizer.customize(this.restTemplate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void interceptRestTemplate() {
|
||||
MetricsRestTemplateCustomizer customizer = new MetricsRestTemplateCustomizer(
|
||||
this.registry, new DefaultRestTemplateExchangeTagsProvider(),
|
||||
"http.client.requests", true);
|
||||
customizer.customize(this.restTemplate);
|
||||
MockRestServiceServer mockServer = MockRestServiceServer
|
||||
.createServer(this.restTemplate);
|
||||
mockServer.expect(MockRestRequestMatchers.requestTo("/test/123"))
|
||||
@@ -79,13 +84,22 @@ public class MetricsRestTemplateCustomizerTests {
|
||||
|
||||
@Test
|
||||
public void avoidDuplicateRegistration() {
|
||||
MetricsRestTemplateCustomizer customizer = new MetricsRestTemplateCustomizer(
|
||||
this.registry, new DefaultRestTemplateExchangeTagsProvider(),
|
||||
"http.client.requests", true);
|
||||
customizer.customize(this.restTemplate);
|
||||
this.customizer.customize(this.restTemplate);
|
||||
assertThat(this.restTemplate.getInterceptors()).hasSize(1);
|
||||
customizer.customize(this.restTemplate);
|
||||
this.customizer.customize(this.restTemplate);
|
||||
assertThat(this.restTemplate.getInterceptors()).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void normalizeUriToContainLeadingSlash() {
|
||||
this.mockServer.expect(MockRestRequestMatchers.requestTo("/test/123"))
|
||||
.andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
|
||||
.andRespond(MockRestResponseCreators.withSuccess("OK",
|
||||
MediaType.APPLICATION_JSON));
|
||||
String result = this.restTemplate.getForObject("test/{id}", String.class, 123);
|
||||
this.registry.get("http.client.requests").tags("uri", "/test/{id}").timer();
|
||||
assertThat(result).isEqualTo("OK");
|
||||
this.mockServer.verify();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user