Allows gateway metrics prefix to be customized.

Fixes gh-1981
This commit is contained in:
spencergibb
2020-10-09 15:51:20 -04:00
parent 7e77a9110a
commit f09d8b2c5d
5 changed files with 92 additions and 22 deletions

View File

@@ -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<GatewayTagsProvider> tagsProviders) {
return new GatewayMetricsFilter(meterRegistry, tagsProviders);
List<GatewayTagsProvider> tagsProviders, GatewayProperties properties) {
return new GatewayMetricsFilter(meterRegistry, tagsProviders,
properties.getMetrics().getPrefix());
}
}

View File

@@ -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<RouteDefinition> 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();
}
}

View File

@@ -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<GatewayTagsProvider> tagsProviders) {
this(meterRegistry, tagsProviders, GatewayProperties.Metrics.DEFAULT_PREFIX);
}
public GatewayMetricsFilter(MeterRegistry meterRegistry,
List<GatewayTagsProvider> 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));
}
}

View File

@@ -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",

View File

@@ -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);
}