Merge pull request #11316 from dkublik:adding-MetricsClientHttpRequestInterceptor-only-once

* pr/11316:
  Polish
  Avoid duplicate Metrics HTTP interceptor registration
pull/11316/merge
Brian Clozel 7 years ago
commit 97c91eee94

@ -59,10 +59,13 @@ public class MetricsRestTemplateCustomizer implements RestTemplateCustomizer {
UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler();
templateHandler = this.interceptor.createUriTemplateHandler(templateHandler);
restTemplate.setUriTemplateHandler(templateHandler);
List<ClientHttpRequestInterceptor> existingInterceptors = restTemplate.getInterceptors();
if (!existingInterceptors.contains(this.interceptor)) {
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(this.interceptor);
interceptors.addAll(restTemplate.getInterceptors());
restTemplate.setInterceptors(interceptors);
interceptors.add(this.interceptor);
interceptors.addAll(existingInterceptors);
restTemplate.setInterceptors(interceptors);
}
}
}

@ -24,6 +24,7 @@ import io.micrometer.core.instrument.Statistic;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.simple.SimpleConfig;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpMethod;
@ -39,35 +40,54 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link MetricsRestTemplateCustomizer}.
*
* @author Jon Schneider
* @author Brian Clozel
*/
public class MetricsRestTemplateCustomizerTests {
private MeterRegistry registry;
private RestTemplate restTemplate;
@Before
public void setup() {
this.registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, new MockClock());
this.restTemplate = new RestTemplate();
}
@Test
public void interceptRestTemplate() {
MeterRegistry registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT,
new MockClock());
RestTemplate restTemplate = new RestTemplate();
MetricsRestTemplateCustomizer customizer = new MetricsRestTemplateCustomizer(
registry, new DefaultRestTemplateExchangeTagsProvider(),
this.registry, new DefaultRestTemplateExchangeTagsProvider(),
"http.client.requests", true);
customizer.customize(restTemplate);
customizer.customize(this.restTemplate);
MockRestServiceServer mockServer = MockRestServiceServer
.createServer(restTemplate);
.createServer(this.restTemplate);
mockServer.expect(MockRestRequestMatchers.requestTo("/test/123"))
.andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
.andRespond(MockRestResponseCreators.withSuccess("OK",
MediaType.APPLICATION_JSON));
String result = restTemplate.getForObject("/test/{id}", String.class, 123);
MockClock.clock(registry).add(SimpleConfig.DEFAULT_STEP);
assertThat(registry.find("http.client.requests")
String result = this.restTemplate.getForObject("/test/{id}", String.class, 123);
MockClock.clock(this.registry).add(SimpleConfig.DEFAULT_STEP);
assertThat(this.registry.find("http.client.requests")
.meters()).anySatisfy((m) -> assertThat(
StreamSupport.stream(m.getId().getTags().spliterator(), false)
.map(Tag::getKey)).doesNotContain("bucket"));
assertThat(registry.find("http.client.requests")
StreamSupport.stream(m.getId().getTags().spliterator(), false)
.map(Tag::getKey)).doesNotContain("bucket"));
assertThat(this.registry.find("http.client.requests")
.tags("method", "GET", "uri", "/test/{id}", "status", "200")
.value(Statistic.Count, 1.0).timer()).isPresent();
assertThat(result).isEqualTo("OK");
mockServer.verify();
}
@Test
public void avoidDuplicateRegistration() {
MetricsRestTemplateCustomizer customizer = new MetricsRestTemplateCustomizer(
this.registry, new DefaultRestTemplateExchangeTagsProvider(),
"http.client.requests", true);
customizer.customize(this.restTemplate);
assertThat(this.restTemplate.getInterceptors()).hasSize(1);
customizer.customize(this.restTemplate);
assertThat(this.restTemplate.getInterceptors()).hasSize(1);
}
}

Loading…
Cancel
Save