Polish "Allow configuration of auto-timed metrics"
Closes gh-15988
This commit is contained in:
@@ -16,13 +16,13 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.actuate.metrics.Autotime;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
|
||||
import org.springframework.boot.context.properties.NestedConfigurationProperty;
|
||||
|
||||
/**
|
||||
* {@link ConfigurationProperties @ConfigurationProperties} for configuring
|
||||
@@ -107,8 +107,12 @@ public class MetricsProperties {
|
||||
*/
|
||||
private int maxUriTags = 100;
|
||||
|
||||
public ClientRequest getRequest() {
|
||||
return this.request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get name of the metric for received requests.
|
||||
* Return the name of the metric for client requests.
|
||||
* @return request metric name
|
||||
* @deprecated since 2.2.0 in favor of {@link ClientRequest#getMetricName()}
|
||||
*/
|
||||
@@ -119,7 +123,7 @@ public class MetricsProperties {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set name of the metric for received requests.
|
||||
* Set the name of the metric for client requests.
|
||||
* @param requestsMetricName request metric name
|
||||
* @deprecated since 2.2.0 in favor of
|
||||
* {@link ClientRequest#setMetricName(String)}
|
||||
@@ -128,10 +132,6 @@ public class MetricsProperties {
|
||||
this.request.setMetricName(requestsMetricName);
|
||||
}
|
||||
|
||||
public ClientRequest getRequest() {
|
||||
return this.request;
|
||||
}
|
||||
|
||||
public int getMaxUriTags() {
|
||||
return this.maxUriTags;
|
||||
}
|
||||
@@ -148,11 +148,12 @@ public class MetricsProperties {
|
||||
private String metricName = "http.client.requests";
|
||||
|
||||
/**
|
||||
* Automatically time requests.
|
||||
* Auto-timed request settings.
|
||||
*/
|
||||
private final AutoTime autoTime = new AutoTime();
|
||||
@NestedConfigurationProperty
|
||||
private final Autotime autoTime = new Autotime();
|
||||
|
||||
public AutoTime getAutoTime() {
|
||||
public Autotime getAutotime() {
|
||||
return this.autoTime;
|
||||
}
|
||||
|
||||
@@ -179,8 +180,12 @@ public class MetricsProperties {
|
||||
*/
|
||||
private int maxUriTags = 100;
|
||||
|
||||
public ServerRequest getRequest() {
|
||||
return this.request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get name of the metric for received requests.
|
||||
* Return name of the metric for server requests.
|
||||
* @return request metric name
|
||||
* @deprecated since 2.2.0 in favor of {@link ServerRequest#getMetricName()}
|
||||
*/
|
||||
@@ -191,7 +196,7 @@ public class MetricsProperties {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set name of the metric for received requests.
|
||||
* Set the name of the metric for server requests.
|
||||
* @param requestsMetricName request metric name
|
||||
* @deprecated since 2.2.0 in favor of
|
||||
* {@link ServerRequest#setMetricName(String)}
|
||||
@@ -200,10 +205,6 @@ public class MetricsProperties {
|
||||
this.request.setMetricName(requestsMetricName);
|
||||
}
|
||||
|
||||
public ServerRequest getRequest() {
|
||||
return this.request;
|
||||
}
|
||||
|
||||
public int getMaxUriTags() {
|
||||
return this.maxUriTags;
|
||||
}
|
||||
@@ -220,12 +221,13 @@ public class MetricsProperties {
|
||||
private String metricName = "http.server.requests";
|
||||
|
||||
/**
|
||||
* Automatically time requests.
|
||||
* Auto-timed request settings.
|
||||
*/
|
||||
private final AutoTime autoTime = new AutoTime();
|
||||
@NestedConfigurationProperty
|
||||
private final Autotime autotime = new Autotime();
|
||||
|
||||
public AutoTime getAutoTime() {
|
||||
return this.autoTime;
|
||||
public Autotime getAutotime() {
|
||||
return this.autotime;
|
||||
}
|
||||
|
||||
public String getMetricName() {
|
||||
@@ -240,56 +242,6 @@ public class MetricsProperties {
|
||||
|
||||
}
|
||||
|
||||
public static class AutoTime {
|
||||
|
||||
/**
|
||||
* Whether requests handled by Spring MVC, WebFlux or Jersey should be
|
||||
* automatically timed. If the number of time series emitted grows too large
|
||||
* on account of request mapping timings, disable this and use 'Timed' on a
|
||||
* per request mapping basis as needed.
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
|
||||
/**
|
||||
* Default percentiles when @Timed annotation is not presented on the
|
||||
* corresponding request handler. Any @Timed annotation presented will have
|
||||
* precedence.
|
||||
*/
|
||||
private List<Double> defaultPercentiles = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Default histogram when @Timed annotation is not presented on the
|
||||
* corresponding request handler. Any @Timed annotation presented will have
|
||||
* precedence.
|
||||
*/
|
||||
private boolean defaultHistogram;
|
||||
|
||||
public boolean isEnabled() {
|
||||
return this.enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public List<Double> getDefaultPercentiles() {
|
||||
return this.defaultPercentiles;
|
||||
}
|
||||
|
||||
public void setDefaultPercentiles(List<Double> defaultPercentiles) {
|
||||
this.defaultPercentiles = defaultPercentiles;
|
||||
}
|
||||
|
||||
public boolean isDefaultHistogram() {
|
||||
return this.defaultHistogram;
|
||||
}
|
||||
|
||||
public void setDefaultHistogram(boolean defaultHistogram) {
|
||||
this.defaultHistogram = defaultHistogram;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Distribution {
|
||||
|
||||
@@ -80,7 +80,7 @@ public class JerseyServerMetricsAutoConfiguration {
|
||||
Server server = this.properties.getWeb().getServer();
|
||||
return (config) -> config.register(new MetricsApplicationEventListener(
|
||||
meterRegistry, tagsProvider, server.getRequest().getMetricName(),
|
||||
server.getRequest().getAutoTime().isEnabled(),
|
||||
server.getRequest().getAutotime().isEnabled(),
|
||||
new AnnotationUtilsAnnotationFinder()));
|
||||
}
|
||||
|
||||
|
||||
@@ -19,8 +19,7 @@ package org.springframework.boot.actuate.autoconfigure.metrics.web.client;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties.Web.AutoTime;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties.Web.Client;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties.Web.Client.ClientRequest;
|
||||
import org.springframework.boot.actuate.metrics.web.client.DefaultRestTemplateExchangeTagsProvider;
|
||||
import org.springframework.boot.actuate.metrics.web.client.MetricsRestTemplateCustomizer;
|
||||
import org.springframework.boot.actuate.metrics.web.client.RestTemplateExchangeTagsProvider;
|
||||
@@ -55,13 +54,10 @@ class RestTemplateMetricsConfiguration {
|
||||
MeterRegistry meterRegistry,
|
||||
RestTemplateExchangeTagsProvider restTemplateExchangeTagsProvider,
|
||||
MetricsProperties properties) {
|
||||
|
||||
Client client = properties.getWeb().getClient();
|
||||
AutoTime autoTime = client.getRequest().getAutoTime();
|
||||
ClientRequest request = properties.getWeb().getClient().getRequest();
|
||||
return new MetricsRestTemplateCustomizer(meterRegistry,
|
||||
restTemplateExchangeTagsProvider, client.getRequest().getMetricName(),
|
||||
autoTime.isEnabled(), autoTime.getDefaultPercentiles(),
|
||||
autoTime.isDefaultHistogram());
|
||||
restTemplateExchangeTagsProvider, request.getMetricName(),
|
||||
request.getAutotime());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.boot.actuate.autoconfigure.metrics.web.client;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties.Web.Client.ClientRequest;
|
||||
import org.springframework.boot.actuate.metrics.web.reactive.client.DefaultWebClientExchangeTagsProvider;
|
||||
import org.springframework.boot.actuate.metrics.web.reactive.client.MetricsWebClientCustomizer;
|
||||
import org.springframework.boot.actuate.metrics.web.reactive.client.WebClientExchangeTagsProvider;
|
||||
@@ -48,8 +49,9 @@ class WebClientMetricsConfiguration {
|
||||
public MetricsWebClientCustomizer metricsWebClientCustomizer(
|
||||
MeterRegistry meterRegistry, WebClientExchangeTagsProvider tagsProvider,
|
||||
MetricsProperties properties) {
|
||||
ClientRequest request = properties.getWeb().getClient().getRequest();
|
||||
return new MetricsWebClientCustomizer(meterRegistry, tagsProvider,
|
||||
properties.getWeb().getClient().getRequest().getMetricName());
|
||||
request.getMetricName(), request.getAutotime());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,8 +21,7 @@ import io.micrometer.core.instrument.config.MeterFilter;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties.Web.AutoTime;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties.Web.Server;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties.Web.Server.ServerRequest;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.OnlyOnceLoggingDenyMeterFilter;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
|
||||
import org.springframework.boot.actuate.metrics.web.reactive.server.DefaultWebFluxTagsProvider;
|
||||
@@ -67,11 +66,9 @@ public class WebFluxMetricsAutoConfiguration {
|
||||
@Bean
|
||||
public MetricsWebFilter webfluxMetrics(MeterRegistry registry,
|
||||
WebFluxTagsProvider tagConfigurer) {
|
||||
Server serverProperties = this.properties.getWeb().getServer();
|
||||
AutoTime autotime = serverProperties.getRequest().getAutoTime();
|
||||
return new MetricsWebFilter(registry, tagConfigurer,
|
||||
serverProperties.getRequest().getMetricName(), autotime.isEnabled(),
|
||||
autotime.getDefaultPercentiles(), autotime.isDefaultHistogram());
|
||||
ServerRequest request = this.properties.getWeb().getServer().getRequest();
|
||||
return new MetricsWebFilter(registry, tagConfigurer, request.getMetricName(),
|
||||
request.getAutotime());
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -23,8 +23,7 @@ import io.micrometer.core.instrument.config.MeterFilter;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties.Web.AutoTime;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties.Web.Server;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties.Web.Server.ServerRequest;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.OnlyOnceLoggingDenyMeterFilter;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
|
||||
import org.springframework.boot.actuate.metrics.web.servlet.DefaultWebMvcTagsProvider;
|
||||
@@ -79,11 +78,9 @@ public class WebMvcMetricsAutoConfiguration {
|
||||
@Bean
|
||||
public FilterRegistrationBean<WebMvcMetricsFilter> webMvcMetricsFilter(
|
||||
MeterRegistry registry, WebMvcTagsProvider tagsProvider) {
|
||||
Server serverProperties = this.properties.getWeb().getServer();
|
||||
AutoTime autotime = serverProperties.getRequest().getAutoTime();
|
||||
ServerRequest request = this.properties.getWeb().getServer().getRequest();
|
||||
WebMvcMetricsFilter filter = new WebMvcMetricsFilter(registry, tagsProvider,
|
||||
serverProperties.getRequest().getMetricName(), autotime.isEnabled(),
|
||||
autotime.getDefaultPercentiles(), autotime.isDefaultHistogram());
|
||||
request.getMetricName(), request.getAutotime());
|
||||
FilterRegistrationBean<WebMvcMetricsFilter> registration = new FilterRegistrationBean<>(
|
||||
filter);
|
||||
registration.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
|
||||
|
||||
@@ -253,6 +253,34 @@
|
||||
"level": "error"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "management.metrics.web.client.request.autotime.enabled",
|
||||
"description": "Whether to automatically time web client requests.",
|
||||
"defaultValue": true
|
||||
},
|
||||
{
|
||||
"name": "management.metrics.web.client.request.autotime.percentiles",
|
||||
"description": "Computed non-aggregable percentiles to publish."
|
||||
},
|
||||
{
|
||||
"name": "management.metrics.web.client.request.autotime.percentiles-histogram",
|
||||
"description": "Whether percentile histograms should be published.",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"name": "management.metrics.web.server.request.autotime.enabled",
|
||||
"description": "Whether to automatically time web server requests.",
|
||||
"defaultValue": true
|
||||
},
|
||||
{
|
||||
"name": "management.metrics.web.server.request.autotime.percentiles",
|
||||
"description": "Computed non-aggregable percentiles to publish."
|
||||
},
|
||||
{
|
||||
"name": "management.metrics.web.server.request.autotime.percentiles-histogram",
|
||||
"description": "Whether percentile histograms should be published.",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"name": "management.server.ssl.ciphers",
|
||||
"description": "Supported SSL ciphers."
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.web.client;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.Timer;
|
||||
import io.micrometer.core.instrument.distribution.HistogramSnapshot;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -100,6 +102,24 @@ public class RestTemplateMetricsConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void autoTimeRequestsCanBeConfigured() {
|
||||
this.contextRunner.withPropertyValues(
|
||||
"management.metrics.web.client.request.autotime.enabled=true",
|
||||
"management.metrics.web.client.request.autotime.percentiles=0.5,0.7",
|
||||
"management.metrics.web.client.request.autotime.percentiles-histogram=true")
|
||||
.run((context) -> {
|
||||
MeterRegistry registry = getInitializedMeterRegistry(context);
|
||||
Timer timer = registry.get("http.client.requests").timer();
|
||||
HistogramSnapshot snapshot = timer.takeSnapshot();
|
||||
assertThat(snapshot.percentileValues()).hasSize(2);
|
||||
assertThat(snapshot.percentileValues()[0].percentile())
|
||||
.isEqualTo(0.5);
|
||||
assertThat(snapshot.percentileValues()[1].percentile())
|
||||
.isEqualTo(0.7);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void backsOffWhenRestTemplateBuilderIsMissing() {
|
||||
new ApplicationContextRunner().with(MetricsRun.simple())
|
||||
|
||||
@@ -19,6 +19,8 @@ package org.springframework.boot.actuate.autoconfigure.metrics.web.client;
|
||||
import java.time.Duration;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.Timer;
|
||||
import io.micrometer.core.instrument.distribution.HistogramSnapshot;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -101,6 +103,24 @@ public class WebClientMetricsConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void autoTimeRequestsCanBeConfigured() {
|
||||
this.contextRunner.withPropertyValues(
|
||||
"management.metrics.web.client.request.autotime.enabled=true",
|
||||
"management.metrics.web.client.request.autotime.percentiles=0.5,0.7",
|
||||
"management.metrics.web.client.request.autotime.percentiles-histogram=true")
|
||||
.run((context) -> {
|
||||
MeterRegistry registry = getInitializedMeterRegistry(context);
|
||||
Timer timer = registry.get("http.client.requests").timer();
|
||||
HistogramSnapshot snapshot = timer.takeSnapshot();
|
||||
assertThat(snapshot.percentileValues()).hasSize(2);
|
||||
assertThat(snapshot.percentileValues()[0].percentile())
|
||||
.isEqualTo(0.5);
|
||||
assertThat(snapshot.percentileValues()[1].percentile())
|
||||
.isEqualTo(0.7);
|
||||
});
|
||||
}
|
||||
|
||||
private MeterRegistry getInitializedMeterRegistry(
|
||||
AssertableApplicationContext context) {
|
||||
WebClient webClient = mockWebClient(context.getBean(WebClient.Builder.class));
|
||||
|
||||
@@ -102,7 +102,7 @@ public class WebFluxMetricsAutoConfigurationTests {
|
||||
.withConfiguration(AutoConfigurations.of(WebFluxAutoConfiguration.class))
|
||||
.withUserConfiguration(TestController.class)
|
||||
.withPropertyValues(
|
||||
"management.metrics.web.server.request.auto-time.enabled=false")
|
||||
"management.metrics.web.server.request.autotime.enabled=false")
|
||||
.run((context) -> {
|
||||
MeterRegistry registry = getInitializedMeterRegistry(context);
|
||||
assertThat(registry.find("http.server.requests").meter()).isNull();
|
||||
|
||||
@@ -141,14 +141,14 @@ public class WebMvcMetricsAutoConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void autoTimeRequestsDefaultValues() {
|
||||
public void autoTimeRequestsCanBeConfigured() {
|
||||
this.contextRunner.withUserConfiguration(TestController.class)
|
||||
.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class,
|
||||
WebMvcAutoConfiguration.class))
|
||||
.withPropertyValues(
|
||||
"management.metrics.web.server.request.auto-time.enabled=true",
|
||||
"management.metrics.web.server.request.auto-time.default-percentiles=0.5,0.7",
|
||||
"management.metrics.web.server.request.auto-time.default-histogram=true")
|
||||
"management.metrics.web.server.request.autotime.enabled=true",
|
||||
"management.metrics.web.server.request.autotime.percentiles=0.5,0.7",
|
||||
"management.metrics.web.server.request.autotime.percentiles-histogram=true")
|
||||
.run((context) -> {
|
||||
MeterRegistry registry = getInitializedMeterRegistry(context);
|
||||
Timer timer = registry.get("http.server.requests").timer();
|
||||
|
||||
Reference in New Issue
Block a user