Skip to content

Properties to set default configuration for auto-timed controller metrics #15988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@

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.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;

/**
* {@link ConfigurationProperties} for configuring Micrometer-based metrics.
*
* @author Jon Schneider
* @author Alexander Abramov
* @author Tadaya Tsuyukubo
* @since 2.0.0
*/
@ConfigurationProperties("management.metrics")
Expand Down Expand Up @@ -93,10 +97,79 @@ public Server getServer() {

public static class Client {

private final ClientRequest request = new ClientRequest();

/**
* Maximum number of unique URI tag values allowed. After the max number of
* tag values is reached, metrics with additional tag values are denied by
* filter.
*/
private int maxUriTags = 100;

/**
* Name of the metric for sent requests.
* Get name of the metric for received requests.
* @return request metric name
* @deprecated since 2.2.0 in favor of {@link ClientRequest#getMetricName()}
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have a @DeprecatedConfigurationProperty with the replacement key

private String requestsMetricName = "http.client.requests";
@DeprecatedConfigurationProperty(
replacement = "management.metrics.web.client.request.metric-name")
public String getRequestsMetricName() {
return this.request.getMetricName();
}

/**
* Set name of the metric for received requests.
* @param requestsMetricName request metric name
* @deprecated since 2.2.0 in favor of
* {@link ClientRequest#setMetricName(String)}
*/
public void setRequestsMetricName(String requestsMetricName) {
this.request.setMetricName(requestsMetricName);
}

public ClientRequest getRequest() {
return this.request;
}

public int getMaxUriTags() {
return this.maxUriTags;
}

public void setMaxUriTags(int maxUriTags) {
this.maxUriTags = maxUriTags;
}

public static class ClientRequest {

/**
* Name of the metric for sent requests.
*/
private String metricName = "http.client.requests";

/**
* Automatically time requests.
*/
private final AutoTime autoTime = new AutoTime();

public AutoTime getAutoTime() {
return this.autoTime;
}

public String getMetricName() {
return this.metricName;
}

public void setMetricName(String metricName) {
this.metricName = metricName;
}

}

}

public static class Server {

private final ServerRequest request = new ServerRequest();

/**
* Maximum number of unique URI tag values allowed. After the max number of
Expand All @@ -105,12 +178,29 @@ public static class Client {
*/
private int maxUriTags = 100;

/**
* Get name of the metric for received requests.
* @return request metric name
* @deprecated since 2.2.0 in favor of {@link ServerRequest#getMetricName()}
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have a @DeprecatedConfigurationProperty with the replacement key

@DeprecatedConfigurationProperty(
replacement = "management.metrics.web.server.request.metric-name")
public String getRequestsMetricName() {
return this.requestsMetricName;
return this.request.getMetricName();
}

/**
* Set name of the metric for received requests.
* @param requestsMetricName request metric name
* @deprecated since 2.2.0 in favor of
* {@link ServerRequest#setMetricName(String)}
*/
public void setRequestsMetricName(String requestsMetricName) {
this.requestsMetricName = requestsMetricName;
this.request.setMetricName(requestsMetricName);
}

public ServerRequest getRequest() {
return this.request;
}

public int getMaxUriTags() {
Expand All @@ -121,52 +211,80 @@ public void setMaxUriTags(int maxUriTags) {
this.maxUriTags = maxUriTags;
}

public static class ServerRequest {

/**
* Name of the metric for received requests.
*/
private String metricName = "http.server.requests";

/**
* Automatically time requests.
*/
private final AutoTime autoTime = new AutoTime();

public AutoTime getAutoTime() {
return this.autoTime;
}

public String getMetricName() {
return this.metricName;
}

public void setMetricName(String metricName) {
this.metricName = metricName;
}

}

}

public static class Server {
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 autoTimeRequests = true;
private boolean enabled = true;

/**
* Name of the metric for received requests.
* Default percentiles when @Timed annotation is not presented on the
* corresponding request handler. Any @Timed annotation presented will have
* precedence.
*/
private String requestsMetricName = "http.server.requests";
private List<Double> defaultPercentiles = new ArrayList<>();

/**
* Maximum number of unique URI tag values allowed. After the max number of
* tag values is reached, metrics with additional tag values are denied by
* filter.
* Default histogram when @Timed annotation is not presented on the
* corresponding request handler. Any @Timed annotation presented will have
* precedence.
*/
private int maxUriTags = 100;
private boolean defaultHistogram;

public boolean isAutoTimeRequests() {
return this.autoTimeRequests;
public boolean isEnabled() {
return this.enabled;
}

public void setAutoTimeRequests(boolean autoTimeRequests) {
this.autoTimeRequests = autoTimeRequests;
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public String getRequestsMetricName() {
return this.requestsMetricName;
public List<Double> getDefaultPercentiles() {
return this.defaultPercentiles;
}

public void setRequestsMetricName(String requestsMetricName) {
this.requestsMetricName = requestsMetricName;
public void setDefaultPercentiles(List<Double> defaultPercentiles) {
this.defaultPercentiles = defaultPercentiles;
}

public int getMaxUriTags() {
return this.maxUriTags;
public boolean isDefaultHistogram() {
return this.defaultHistogram;
}

public void setMaxUriTags(int maxUriTags) {
this.maxUriTags = maxUriTags;
public void setDefaultHistogram(boolean defaultHistogram) {
this.defaultHistogram = defaultHistogram;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,16 @@ public ResourceConfigCustomizer jerseyServerMetricsResourceConfigCustomizer(
MeterRegistry meterRegistry, JerseyTagsProvider tagsProvider) {
Server server = this.properties.getWeb().getServer();
return (config) -> config.register(new MetricsApplicationEventListener(
meterRegistry, tagsProvider, server.getRequestsMetricName(),
server.isAutoTimeRequests(), new AnnotationUtilsAnnotationFinder()));
meterRegistry, tagsProvider, server.getRequest().getMetricName(),
server.getRequest().getAutoTime().isEnabled(),
new AnnotationUtilsAnnotationFinder()));
}

@Bean
@Order(0)
public MeterFilter jerseyMetricsUriTagFilter() {
String metricName = this.properties.getWeb().getServer().getRequestsMetricName();
String metricName = this.properties.getWeb().getServer().getRequest()
.getMetricName();
MeterFilter filter = new OnlyOnceLoggingDenyMeterFilter(() -> String
.format("Reached the maximum number of URI tags for '%s'.", metricName));
return MeterFilter.maximumAllowableTags(metricName, "uri",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class HttpClientMetricsAutoConfiguration {
@Bean
@Order(0)
public MeterFilter metricsHttpClientUriTagFilter(MetricsProperties properties) {
String metricName = properties.getWeb().getClient().getRequestsMetricName();
String metricName = properties.getWeb().getClient().getRequest().getMetricName();
MeterFilter denyFilter = new OnlyOnceLoggingDenyMeterFilter(() -> String
.format("Reached the maximum number of URI tags for '%s'. Are you using "
+ "'uriVariables'?", metricName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
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.metrics.web.client.DefaultRestTemplateExchangeTagsProvider;
import org.springframework.boot.actuate.metrics.web.client.MetricsRestTemplateCustomizer;
import org.springframework.boot.actuate.metrics.web.client.RestTemplateExchangeTagsProvider;
Expand Down Expand Up @@ -53,9 +55,13 @@ public MetricsRestTemplateCustomizer metricsRestTemplateCustomizer(
MeterRegistry meterRegistry,
RestTemplateExchangeTagsProvider restTemplateExchangeTagsProvider,
MetricsProperties properties) {

Client client = properties.getWeb().getClient();
AutoTime autoTime = client.getRequest().getAutoTime();
return new MetricsRestTemplateCustomizer(meterRegistry,
restTemplateExchangeTagsProvider,
properties.getWeb().getClient().getRequestsMetricName());
restTemplateExchangeTagsProvider, client.getRequest().getMetricName(),
autoTime.isEnabled(), autoTime.getDefaultPercentiles(),
autoTime.isDefaultHistogram());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public MetricsWebClientCustomizer metricsWebClientCustomizer(
MeterRegistry meterRegistry, WebClientExchangeTagsProvider tagsProvider,
MetricsProperties properties) {
return new MetricsWebClientCustomizer(meterRegistry, tagsProvider,
properties.getWeb().getClient().getRequestsMetricName());
properties.getWeb().getClient().getRequest().getMetricName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

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.OnlyOnceLoggingDenyMeterFilter;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.actuate.metrics.web.reactive.server.DefaultWebFluxTagsProvider;
Expand Down Expand Up @@ -65,15 +67,18 @@ public DefaultWebFluxTagsProvider webfluxTagConfigurer() {
@Bean
public MetricsWebFilter webfluxMetrics(MeterRegistry registry,
WebFluxTagsProvider tagConfigurer) {
Server serverProperties = this.properties.getWeb().getServer();
AutoTime autotime = serverProperties.getRequest().getAutoTime();
return new MetricsWebFilter(registry, tagConfigurer,
this.properties.getWeb().getServer().getRequestsMetricName(),
this.properties.getWeb().getServer().isAutoTimeRequests());
serverProperties.getRequest().getMetricName(), autotime.isEnabled(),
autotime.getDefaultPercentiles(), autotime.isDefaultHistogram());
}

@Bean
@Order(0)
public MeterFilter metricsHttpServerUriTagFilter() {
String metricName = this.properties.getWeb().getServer().getRequestsMetricName();
String metricName = this.properties.getWeb().getServer().getRequest()
.getMetricName();
MeterFilter filter = new OnlyOnceLoggingDenyMeterFilter(() -> String
.format("Reached the maximum number of URI tags for '%s'.", metricName));
return MeterFilter.maximumAllowableTags(metricName, "uri",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

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.OnlyOnceLoggingDenyMeterFilter;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
Expand Down Expand Up @@ -79,9 +80,10 @@ public DefaultWebMvcTagsProvider webMvcTagsProvider() {
public FilterRegistrationBean<WebMvcMetricsFilter> webMvcMetricsFilter(
MeterRegistry registry, WebMvcTagsProvider tagsProvider) {
Server serverProperties = this.properties.getWeb().getServer();
AutoTime autotime = serverProperties.getRequest().getAutoTime();
WebMvcMetricsFilter filter = new WebMvcMetricsFilter(registry, tagsProvider,
serverProperties.getRequestsMetricName(),
serverProperties.isAutoTimeRequests());
serverProperties.getRequest().getMetricName(), autotime.isEnabled(),
autotime.getDefaultPercentiles(), autotime.isDefaultHistogram());
FilterRegistrationBean<WebMvcMetricsFilter> registration = new FilterRegistrationBean<>(
filter);
registration.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
Expand All @@ -92,7 +94,8 @@ public FilterRegistrationBean<WebMvcMetricsFilter> webMvcMetricsFilter(
@Bean
@Order(0)
public MeterFilter metricsHttpServerUriTagFilter() {
String metricName = this.properties.getWeb().getServer().getRequestsMetricName();
String metricName = this.properties.getWeb().getServer().getRequest()
.getMetricName();
MeterFilter filter = new OnlyOnceLoggingDenyMeterFilter(() -> String
.format("Reached the maximum number of URI tags for '%s'.", metricName));
return MeterFilter.maximumAllowableTags(metricName, "uri",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void metricsAreNotRecordedIfAutoTimeRequestsIsDisabled() {
.withConfiguration(AutoConfigurations.of(WebFluxAutoConfiguration.class))
.withUserConfiguration(TestController.class)
.withPropertyValues(
"management.metrics.web.server.auto-time-requests=false")
"management.metrics.web.server.request.auto-time.enabled=false")
.run((context) -> {
MeterRegistry registry = getInitializedMeterRegistry(context);
assertThat(registry.find("http.server.requests").meter()).isNull();
Expand Down
Loading