Combines two metrics properties classes into one.

Updates GatewaySampleApplicationTests.java to use configured metrics prefix.
This commit is contained in:
spencergibb
2020-10-16 15:57:18 -04:00
parent 2941652190
commit 6f9a7a8288
6 changed files with 54 additions and 60 deletions

View File

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

View File

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

View File

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

View File

@@ -68,8 +68,6 @@ public class GatewayProperties {
*/
private boolean failOnRouteDefinitionError = true;
private Metrics metrics = new Metrics();
public List<RouteDefinition> 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();
}

View File

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

View File

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