diff --git a/spring-cloud-gateway-sample/src/test/java/org/springframework/cloud/gateway/sample/GatewaySampleApplicationTests.java b/spring-cloud-gateway-sample/src/test/java/org/springframework/cloud/gateway/sample/GatewaySampleApplicationTests.java index d1b87295..2927d061 100644 --- a/spring-cloud-gateway-sample/src/test/java/org/springframework/cloud/gateway/sample/GatewaySampleApplicationTests.java +++ b/spring-cloud-gateway-sample/src/test/java/org/springframework/cloud/gateway/sample/GatewaySampleApplicationTests.java @@ -28,9 +28,11 @@ import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.cloud.gateway.config.GatewayMetricsProperties; import org.springframework.cloud.gateway.test.HttpBinCompatibleController; import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient; import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; @@ -55,6 +57,9 @@ public class GatewaySampleApplicationTests { protected static int managementPort; + @Autowired + GatewayMetricsProperties metricsProperties; + @LocalServerPort protected int port = 0; @@ -162,7 +167,8 @@ public class GatewaySampleApplicationTests { @Test public void actuatorMetrics() { contextLoads(); - webClient.get().uri("http://localhost:" + managementPort + "/actuator/metrics/gateway.requests").exchange() + String metricName = metricsProperties.getPrefix() + ".requests"; + webClient.get().uri("http://localhost:" + managementPort + "/actuator/metrics/" + metricName).exchange() .expectStatus().isOk().expectBody().consumeWith(i -> { String body = new String(i.getResponseBodyContent()); ObjectMapper mapper = new ObjectMapper(); @@ -170,7 +176,7 @@ public class GatewaySampleApplicationTests { JsonNode actualObj = mapper.readTree(body); JsonNode findValue = actualObj.findValue("name"); assertThat(findValue.asText()).as("Expected to find metric with name gateway.requests") - .isEqualTo("gateway.requests"); + .isEqualTo(metricName); } catch (IOException e) { throw new IllegalStateException(e); diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayMetricsAutoConfiguration.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayMetricsAutoConfiguration.java index 496f8757..8874a756 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayMetricsAutoConfiguration.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayMetricsAutoConfiguration.java @@ -57,8 +57,8 @@ public class GatewayMetricsAutoConfiguration { } @Bean - public PropertiesTagsProvider propertiesTagsProvider(GatewayMetricsProperties gatewayMetricsProperties) { - return new PropertiesTagsProvider(gatewayMetricsProperties.getTags()); + public PropertiesTagsProvider propertiesTagsProvider(GatewayMetricsProperties properties) { + return new PropertiesTagsProvider(properties.getTags()); } @Bean @@ -67,8 +67,8 @@ public class GatewayMetricsAutoConfiguration { // don't use @ConditionalOnEnabledGlobalFilter as the above property may // encompass more than just the filter public GatewayMetricsFilter gatewayMetricFilter(MeterRegistry meterRegistry, - List tagsProviders, GatewayProperties properties) { - return new GatewayMetricsFilter(meterRegistry, tagsProviders, properties.getMetrics().getPrefix()); + List tagsProviders, GatewayMetricsProperties properties) { + return new GatewayMetricsFilter(meterRegistry, tagsProviders, properties.getPrefix()); } } diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayMetricsProperties.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayMetricsProperties.java index 81d50319..1f2a94ee 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayMetricsProperties.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayMetricsProperties.java @@ -22,6 +22,7 @@ import java.util.Map; import javax.validation.constraints.NotNull; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.core.style.ToStringCreator; import org.springframework.validation.annotation.Validated; /** @@ -31,12 +32,43 @@ import org.springframework.validation.annotation.Validated; @Validated public class GatewayMetricsProperties { + /** + * Default metrics prefix. + */ + public static final String DEFAULT_PREFIX = "spring.cloud.gateway"; + + /** + * Enables the collection of metrics data. + */ + private boolean enabled; + + /** + * The prefix of all metrics emitted by gateway. + */ + private String prefix = DEFAULT_PREFIX; + /** * Tags map that added to metrics. */ @NotNull private Map tags = new HashMap<>(); + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public String getPrefix() { + return prefix; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + } + public Map getTags() { return tags; } @@ -45,4 +77,11 @@ public class GatewayMetricsProperties { this.tags = tags; } + @Override + public String toString() { + return new ToStringCreator(this).append("enabled", enabled).append("prefix", prefix).append("tags", tags) + .toString(); + + } + } diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayProperties.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayProperties.java index 757454f4..594f6d6e 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayProperties.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayProperties.java @@ -68,8 +68,6 @@ public class GatewayProperties { */ private boolean failOnRouteDefinitionError = true; - private Metrics metrics = new Metrics(); - public List getRoutes() { return routes; } @@ -105,60 +103,11 @@ public class GatewayProperties { this.failOnRouteDefinitionError = failOnRouteDefinitionError; } - public Metrics getMetrics() { - return metrics; - } - - public void setMetrics(Metrics metrics) { - this.metrics = metrics; - } - @Override public String toString() { return new ToStringCreator(this).append("routes", routes).append("defaultFilters", defaultFilters) .append("streamingMediaTypes", streamingMediaTypes) - .append("failOnRouteDefinitionError", failOnRouteDefinitionError).append("metrics", metrics).toString(); - - } - - public static class Metrics { - - /** - * Default metrics prefix. - */ - public static final String DEFAULT_PREFIX = "spring.cloud.gateway"; - - /** - * Enables the collection of metrics data. - */ - private boolean enabled; - - /** - * The prefix of all metrics emitted by gateway. - */ - private String prefix = DEFAULT_PREFIX; - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - public String getPrefix() { - return prefix; - } - - public void setPrefix(String prefix) { - this.prefix = prefix; - } - - @Override - public String toString() { - return new ToStringCreator(this).append("enabled", enabled).append("prefix", prefix).toString(); - - } + .append("failOnRouteDefinitionError", failOnRouteDefinitionError).toString(); } diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/GatewayMetricsFilterCustomTagsTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/GatewayMetricsFilterCustomTagsTests.java index eaee8576..51e71c38 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/GatewayMetricsFilterCustomTagsTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/GatewayMetricsFilterCustomTagsTests.java @@ -37,7 +37,7 @@ import org.springframework.test.context.junit4.SpringRunner; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; -import static org.springframework.cloud.gateway.config.GatewayProperties.Metrics.DEFAULT_PREFIX; +import static org.springframework.cloud.gateway.config.GatewayMetricsProperties.DEFAULT_PREFIX; /** * @author Ingyu Hwang diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/GatewayMetricsFilterTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/GatewayMetricsFilterTests.java index d76a2da6..e2ed2ab7 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/GatewayMetricsFilterTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/GatewayMetricsFilterTests.java @@ -43,7 +43,7 @@ import org.springframework.web.bind.annotation.RestController; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; -import static org.springframework.cloud.gateway.config.GatewayProperties.Metrics.DEFAULT_PREFIX; +import static org.springframework.cloud.gateway.config.GatewayMetricsProperties.DEFAULT_PREFIX; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = RANDOM_PORT)