Commit 4634811c authored by Stephane Nicoll's avatar Stephane Nicoll

Polish

parent 7bee9dfc
...@@ -55,9 +55,8 @@ public class RestTemplateMetricsConfigurationTests { ...@@ -55,9 +55,8 @@ public class RestTemplateMetricsConfigurationTests {
public void restTemplateCreatedWithBuilderIsInstrumented() { public void restTemplateCreatedWithBuilderIsInstrumented() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
RestTemplate restTemplate = context.getBean(RestTemplateBuilder.class) RestTemplateBuilder builder = context.getBean(RestTemplateBuilder.class);
.build(); validateRestTemplate(builder, registry);
validateRestTemplate(restTemplate, registry);
}); });
} }
...@@ -65,11 +64,10 @@ public class RestTemplateMetricsConfigurationTests { ...@@ -65,11 +64,10 @@ public class RestTemplateMetricsConfigurationTests {
public void restTemplateCanBeCustomizedManually() { public void restTemplateCanBeCustomizedManually() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
assertThat(context).hasSingleBean(MetricsRestTemplateCustomizer.class); assertThat(context).hasSingleBean(MetricsRestTemplateCustomizer.class);
RestTemplate restTemplate = new RestTemplateBuilder() RestTemplateBuilder customBuilder = new RestTemplateBuilder()
.customizers(context.getBean(MetricsRestTemplateCustomizer.class)) .customizers(context.getBean(MetricsRestTemplateCustomizer.class));
.build();
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
validateRestTemplate(restTemplate, registry); validateRestTemplate(customBuilder, registry);
}); });
} }
...@@ -96,8 +94,8 @@ public class RestTemplateMetricsConfigurationTests { ...@@ -96,8 +94,8 @@ public class RestTemplateMetricsConfigurationTests {
assertThat(registry.get("http.client.requests").meters()).hasSize(3); assertThat(registry.get("http.client.requests").meters()).hasSize(3);
assertThat(this.out.toString()).doesNotContain( assertThat(this.out.toString()).doesNotContain(
"Reached the maximum number of URI tags for 'http.client.requests'."); "Reached the maximum number of URI tags for 'http.client.requests'.");
assertThat(this.out.toString()).doesNotContain( assertThat(this.out.toString())
"Are you using 'uriVariables' on RestTemplate calls?"); .doesNotContain("Are you using 'uriVariables'?");
}); });
} }
...@@ -115,13 +113,23 @@ public class RestTemplateMetricsConfigurationTests { ...@@ -115,13 +113,23 @@ public class RestTemplateMetricsConfigurationTests {
return registry; return registry;
} }
private void validateRestTemplate(RestTemplate restTemplate, MeterRegistry registry) { private void validateRestTemplate(RestTemplateBuilder builder,
MockRestServiceServer server = MockRestServiceServer.createServer(restTemplate); MeterRegistry registry) {
server.expect(requestTo("/test")).andRespond(withStatus(HttpStatus.OK)); RestTemplate restTemplate = mockRestTemplate(builder);
assertThat(registry.find("http.client.requests").meter()).isNull(); assertThat(registry.find("http.client.requests").meter()).isNull();
assertThat(restTemplate.getForEntity("/test", Void.class).getStatusCode()) assertThat(restTemplate
.isEqualTo(HttpStatus.OK); .getForEntity("/projects/{project}", Void.class, "spring-boot")
registry.get("http.client.requests").meter(); .getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(registry.get("http.client.requests").tags("uri", "/projects/{project}")
.meter()).isNotNull();
}
private RestTemplate mockRestTemplate(RestTemplateBuilder builder) {
RestTemplate restTemplate = builder.build();
MockRestServiceServer server = MockRestServiceServer.createServer(restTemplate);
server.expect(requestTo("/projects/spring-boot"))
.andRespond(withStatus(HttpStatus.OK));
return restTemplate;
} }
} }
...@@ -17,16 +17,15 @@ ...@@ -17,16 +17,15 @@
package org.springframework.boot.actuate.autoconfigure.metrics.web.client; package org.springframework.boot.actuate.autoconfigure.metrics.web.client;
import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MeterRegistry;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties;
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun; import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
import org.springframework.boot.actuate.metrics.web.reactive.client.WebClientExchangeTagsProvider; import org.springframework.boot.actuate.metrics.web.reactive.client.WebClientExchangeTagsProvider;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration; import org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.rule.OutputCapture; import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -45,36 +44,23 @@ import static org.mockito.Mockito.mock; ...@@ -45,36 +44,23 @@ import static org.mockito.Mockito.mock;
* Tests for {@link WebClientMetricsConfiguration} * Tests for {@link WebClientMetricsConfiguration}
* *
* @author Brian Clozel * @author Brian Clozel
* @author Stephane Nicoll
*/ */
public class WebClientMetricsConfigurationTests { public class WebClientMetricsConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner() private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.with(MetricsRun.simple()) .with(MetricsRun.simple())
.withConfiguration(AutoConfigurations.of(WebClientAutoConfiguration.class)); .withConfiguration(AutoConfigurations.of(WebClientAutoConfiguration.class));
private ClientHttpConnector connector;
@Rule @Rule
public OutputCapture out = new OutputCapture(); public OutputCapture out = new OutputCapture();
@Before
public void setup() {
this.connector = mock(ClientHttpConnector.class);
given(this.connector.connect(any(), any(), any()))
.willReturn(Mono.just(new MockClientHttpResponse(HttpStatus.OK)));
}
@Test @Test
public void webClientCreatedWithBuilderIsInstrumented() { public void webClientCreatedWithBuilderIsInstrumented() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
WebClient.Builder builder = context.getBean(WebClient.Builder.class);
WebClient webClient = builder.clientConnector(this.connector).build();
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.find("http.client.requests").meter()).isNull(); WebClient.Builder builder = context.getBean(WebClient.Builder.class);
webClient.get().uri("http://example.org/projects/{project}", "spring-boot") validateWebClient(builder, registry);
.exchange().block();
assertThat(registry.find("http.client.requests")
.tags("uri", "/projects/{project}").meter()).isNotNull();
}); });
} }
...@@ -89,20 +75,10 @@ public class WebClientMetricsConfigurationTests { ...@@ -89,20 +75,10 @@ public class WebClientMetricsConfigurationTests {
@Test @Test
public void afterMaxUrisReachedFurtherUrisAreDenied() { public void afterMaxUrisReachedFurtherUrisAreDenied() {
this.contextRunner this.contextRunner
.withPropertyValues("management.metrics.web.client.max-uri-tags=10") .withPropertyValues("management.metrics.web.client.max-uri-tags=2")
.run((context) -> { .run((context) -> {
WebClient.Builder builder = context.getBean(WebClient.Builder.class); MeterRegistry registry = getInitializedMeterRegistry(context);
WebClient webClient = builder.clientConnector(this.connector).build(); assertThat(registry.get("http.client.requests").meters()).hasSize(2);
MetricsProperties properties = context
.getBean(MetricsProperties.class);
int maxUriTags = properties.getWeb().getClient().getMaxUriTags();
MeterRegistry registry = context.getBean(MeterRegistry.class);
for (int i = 0; i < maxUriTags + 10; i++) {
webClient.get().uri("http://example.org/projects/" + i).exchange()
.block();
}
assertThat(registry.get("http.client.requests").meters())
.hasSize(maxUriTags);
assertThat(this.out.toString()).contains( assertThat(this.out.toString()).contains(
"Reached the maximum number of URI tags for 'http.client.requests'."); "Reached the maximum number of URI tags for 'http.client.requests'.");
assertThat(this.out.toString()) assertThat(this.out.toString())
...@@ -110,8 +86,48 @@ public class WebClientMetricsConfigurationTests { ...@@ -110,8 +86,48 @@ public class WebClientMetricsConfigurationTests {
}); });
} }
@Test
public void shouldNotDenyNorLogIfMaxUrisIsNotReached() {
this.contextRunner
.withPropertyValues("management.metrics.web.client.max-uri-tags=5")
.run((context) -> {
MeterRegistry registry = getInitializedMeterRegistry(context);
assertThat(registry.get("http.client.requests").meters()).hasSize(3);
assertThat(this.out.toString()).doesNotContain(
"Reached the maximum number of URI tags for 'http.client.requests'.");
assertThat(this.out.toString())
.doesNotContain("Are you using 'uriVariables'?");
});
}
private MeterRegistry getInitializedMeterRegistry(
AssertableApplicationContext context) {
WebClient webClient = mockWebClient(context.getBean(WebClient.Builder.class));
MeterRegistry registry = context.getBean(MeterRegistry.class);
for (int i = 0; i < 3; i++) {
webClient.get().uri("http://example.org/projects/" + i).exchange().block();
}
return registry;
}
private void validateWebClient(WebClient.Builder builder, MeterRegistry registry) {
WebClient webClient = mockWebClient(builder);
assertThat(registry.find("http.client.requests").meter()).isNull();
webClient.get().uri("http://example.org/projects/{project}", "spring-boot")
.exchange().block();
assertThat(registry.find("http.client.requests")
.tags("uri", "/projects/{project}").meter()).isNotNull();
}
private WebClient mockWebClient(WebClient.Builder builder) {
ClientHttpConnector connector = mock(ClientHttpConnector.class);
given(connector.connect(any(), any(), any()))
.willReturn(Mono.just(new MockClientHttpResponse(HttpStatus.OK)));
return builder.clientConnector(connector).build();
}
@Configuration @Configuration
protected static class CustomTagsProviderConfig { static class CustomTagsProviderConfig {
@Bean @Bean
public WebClientExchangeTagsProvider customTagsProvider() { public WebClientExchangeTagsProvider customTagsProvider() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment