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 e3be59fb..a13e994c 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 @@ -39,7 +39,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.web.reactive.DispatcherHandler; @Configuration(proxyBeanMethods = false) -@ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true) +@ConditionalOnProperty(name = GatewayProperties.PREFIX + ".enabled", + matchIfMissing = true) @EnableConfigurationProperties(GatewayMetricsProperties.class) @AutoConfigureBefore(HttpHandlerAutoConfiguration.class) @AutoConfigureAfter({ MetricsAutoConfiguration.class, @@ -66,12 +67,14 @@ public class GatewayMetricsAutoConfiguration { @Bean @ConditionalOnBean(MeterRegistry.class) - @ConditionalOnProperty(name = "spring.cloud.gateway.metrics.enabled", + @ConditionalOnProperty(name = GatewayProperties.PREFIX + ".metrics.enabled", matchIfMissing = true) - // @ConditionalOnEnabledGlobalFilter + // don't use @ConditionalOnEnabledGlobalFilter as the above property may + // encompass more than just the filter public GatewayMetricsFilter gatewayMetricFilter(MeterRegistry meterRegistry, - List tagsProviders) { - return new GatewayMetricsFilter(meterRegistry, tagsProviders); + List tagsProviders, GatewayProperties properties) { + return new GatewayMetricsFilter(meterRegistry, tagsProviders, + properties.getMetrics().getPrefix()); } } 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 1d3a438e..9d63027d 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 @@ -36,10 +36,15 @@ import org.springframework.validation.annotation.Validated; /** * @author Spencer Gibb */ -@ConfigurationProperties("spring.cloud.gateway") +@ConfigurationProperties(GatewayProperties.PREFIX) @Validated public class GatewayProperties { + /** + * Properties prefix. + */ + public static final String PREFIX = "spring.cloud.gateway"; + private final Log logger = LogFactory.getLog(getClass()); /** @@ -63,6 +68,8 @@ public class GatewayProperties { */ private boolean failOnRouteDefinitionError = true; + private Metrics metrics = new Metrics(); + public List getRoutes() { return routes; } @@ -98,13 +105,63 @@ 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) - .toString(); + .append("metrics", metrics).toString(); + + } + + public static class Metrics { + + /** + * Default metrics prefix. + */ + public static final String DEFAULT_PREFIX = "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(); + + } } diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/GatewayMetricsFilter.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/GatewayMetricsFilter.java index 4d794623..e67eacd3 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/GatewayMetricsFilter.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/GatewayMetricsFilter.java @@ -27,6 +27,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import reactor.core.publisher.Mono; +import org.springframework.cloud.gateway.config.GatewayProperties; import org.springframework.cloud.gateway.support.tagsprovider.GatewayHttpTagsProvider; import org.springframework.cloud.gateway.support.tagsprovider.GatewayRouteTagsProvider; import org.springframework.cloud.gateway.support.tagsprovider.GatewayTagsProvider; @@ -46,11 +47,25 @@ public class GatewayMetricsFilter implements GlobalFilter, Ordered { private GatewayTagsProvider compositeTagsProvider; + private final String metricsPrefix; + + @Deprecated public GatewayMetricsFilter(MeterRegistry meterRegistry, List tagsProviders) { + this(meterRegistry, tagsProviders, GatewayProperties.Metrics.DEFAULT_PREFIX); + } + + public GatewayMetricsFilter(MeterRegistry meterRegistry, + List tagsProviders, String metricsPrefix) { this.meterRegistry = meterRegistry; this.compositeTagsProvider = tagsProviders.stream() .reduce(exchange -> Tags.empty(), GatewayTagsProvider::and); + if (metricsPrefix.endsWith(".")) { + this.metricsPrefix = metricsPrefix.substring(0, metricsPrefix.length() - 1); + } + else { + this.metricsPrefix = metricsPrefix; + } } @Deprecated @@ -59,6 +74,10 @@ public class GatewayMetricsFilter implements GlobalFilter, Ordered { new GatewayRouteTagsProvider())); } + public String getMetricsPrefix() { + return metricsPrefix; + } + @Override public int getOrder() { // start the timer as soon as possible and report the metric event before we write @@ -93,9 +112,9 @@ public class GatewayMetricsFilter implements GlobalFilter, Ordered { Tags tags = compositeTagsProvider.apply(exchange); if (log.isTraceEnabled()) { - log.trace("gateway.requests tags: " + tags); + log.trace(metricsPrefix + ".requests tags: " + tags); } - sample.stop(meterRegistry.timer("gateway.requests", tags)); + sample.stop(meterRegistry.timer(metricsPrefix + ".requests", tags)); } } diff --git a/spring-cloud-gateway-server/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-gateway-server/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 7a95ae3c..7a2c9729 100644 --- a/spring-cloud-gateway-server/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-gateway-server/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -258,12 +258,6 @@ "description": "Enables the load-balancer-client global filter.", "defaultValue": "true" }, - { - "name": "spring.cloud.gateway.global-filter.metrics.enabled", - "type": "java.lang.Boolean", - "description": "Enables the metrics global filter.", - "defaultValue": "true" - }, { "name": "spring.cloud.gateway.predicate.after.enabled", "type": "java.lang.Boolean", @@ -354,12 +348,6 @@ "description": "Enables the ForwardedHeadersFilter.", "defaultValue": "true" }, - { - "name": "spring.cloud.gateway.metrics.enabled", - "type": "java.lang.Boolean", - "description": "Enables the collection of metrics data.", - "defaultValue": "true" - }, { "name": "spring.cloud.gateway.httpserver.wiretap", "type": "java.lang.Boolean", diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/config/GatewayMetricsAutoConfigurationTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/config/GatewayMetricsAutoConfigurationTests.java index 50864961..dfd6633e 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/config/GatewayMetricsAutoConfigurationTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/config/GatewayMetricsAutoConfigurationTests.java @@ -54,6 +54,7 @@ public class GatewayMetricsAutoConfigurationTests { @Test public void gatewayMetricsBeansExists() { assertThat(filter).isNotNull(); + assertThat(filter.getMetricsPrefix()).isEqualTo("gateway"); assertThat(tagsProviders).isNotEmpty(); } @@ -75,7 +76,8 @@ public class GatewayMetricsAutoConfigurationTests { } @RunWith(SpringRunner.class) - @SpringBootTest(classes = CustomTagsProviderConfig.class) + @SpringBootTest(classes = CustomTagsProviderConfig.class, + properties = "spring.cloud.gateway.metrics.prefix=myprefix.") public static class AddCustomTagsProvider { @Autowired(required = false) @@ -87,6 +89,7 @@ public class GatewayMetricsAutoConfigurationTests { @Test public void gatewayMetricsBeansExists() { assertThat(filter).isNotNull(); + assertThat(filter.getMetricsPrefix()).isEqualTo("myprefix"); assertThat(tagsProviders).extracting("class") .contains(CustomTagsProviderConfig.EmptyTagsProvider.class); }