Commit 72e2313f authored by Andy Wilkinson's avatar Andy Wilkinson

Polish "Auto-configure Micrometer's Jersey 2 server instrumentation"

Closes gh-12482
parent dd126faf
...@@ -14,12 +14,13 @@ ...@@ -14,12 +14,13 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.actuate.autoconfigure.metrics.jersey2.server; package org.springframework.boot.actuate.autoconfigure.metrics.jersey;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement; import java.lang.reflect.AnnotatedElement;
import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.jersey2.server.AnnotationFinder; import io.micrometer.jersey2.server.AnnotationFinder;
import io.micrometer.jersey2.server.DefaultJerseyTagsProvider; import io.micrometer.jersey2.server.DefaultJerseyTagsProvider;
import io.micrometer.jersey2.server.JerseyTagsProvider; import io.micrometer.jersey2.server.JerseyTagsProvider;
...@@ -27,6 +28,9 @@ import io.micrometer.jersey2.server.MetricsApplicationEventListener; ...@@ -27,6 +28,9 @@ import io.micrometer.jersey2.server.MetricsApplicationEventListener;
import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration; 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.Server;
import org.springframework.boot.actuate.autoconfigure.metrics.OnlyOnceLoggingDenyMeterFilter;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
...@@ -39,12 +43,14 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties ...@@ -39,12 +43,14 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for Jersey server instrumentation. * {@link EnableAutoConfiguration Auto-configuration} for Jersey server instrumentation.
* *
* @author Michael Weirauch * @author Michael Weirauch
* @author Michael Simons * @author Michael Simons
* @author Andy Wilkinson
* @since 2.1.0 * @since 2.1.0
*/ */
@Configuration @Configuration
...@@ -53,9 +59,15 @@ import org.springframework.core.annotation.AnnotationUtils; ...@@ -53,9 +59,15 @@ import org.springframework.core.annotation.AnnotationUtils;
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnClass({ ResourceConfig.class, MetricsApplicationEventListener.class }) @ConditionalOnClass({ ResourceConfig.class, MetricsApplicationEventListener.class })
@ConditionalOnBean({ MeterRegistry.class, ResourceConfig.class }) @ConditionalOnBean({ MeterRegistry.class, ResourceConfig.class })
@EnableConfigurationProperties(JerseyServerMetricsProperties.class) @EnableConfigurationProperties(MetricsProperties.class)
public class JerseyServerMetricsAutoConfiguration { public class JerseyServerMetricsAutoConfiguration {
private final MetricsProperties properties;
public JerseyServerMetricsAutoConfiguration(MetricsProperties properties) {
this.properties = properties;
}
@Bean @Bean
@ConditionalOnMissingBean(JerseyTagsProvider.class) @ConditionalOnMissingBean(JerseyTagsProvider.class)
public DefaultJerseyTagsProvider jerseyTagsProvider() { public DefaultJerseyTagsProvider jerseyTagsProvider() {
...@@ -64,18 +76,36 @@ public class JerseyServerMetricsAutoConfiguration { ...@@ -64,18 +76,36 @@ public class JerseyServerMetricsAutoConfiguration {
@Bean @Bean
public ResourceConfigCustomizer jerseyServerMetricsResourceConfigCustomizer( public ResourceConfigCustomizer jerseyServerMetricsResourceConfigCustomizer(
MeterRegistry meterRegistry, JerseyServerMetricsProperties properties, MeterRegistry meterRegistry, JerseyTagsProvider tagsProvider) {
JerseyTagsProvider tagsProvider) { Server server = this.properties.getWeb().getServer();
return (config) -> config.register(new MetricsApplicationEventListener( return (config) -> {
meterRegistry, tagsProvider, properties.getRequestsMetricName(), config.register(new MetricsApplicationEventListener(meterRegistry,
properties.isAutoTimeRequests(), new AnnotationFinder() { tagsProvider, server.getRequestsMetricName(),
@Override server.isAutoTimeRequests(), new AnnotationUtilsAnnotationFinder()));
public <A extends Annotation> A findAnnotation( };
AnnotatedElement annotatedElement, Class<A> annotationType) { }
return AnnotationUtils.findAnnotation(annotatedElement,
annotationType); @Bean
} @Order(0)
})); public MeterFilter jerseyMetricsUriTagFilter() {
String metricName = this.properties.getWeb().getServer().getRequestsMetricName();
MeterFilter filter = new OnlyOnceLoggingDenyMeterFilter(() -> String
.format("Reached the maximum number of URI tags for '%s'.", metricName));
return MeterFilter.maximumAllowableTags(metricName, "uri",
this.properties.getWeb().getServer().getMaxUriTags(), filter);
}
/**
* An {@link AnnotationFinder} that uses {@link AnnotationUtils}.
*/
private static class AnnotationUtilsAnnotationFinder implements AnnotationFinder {
@Override
public <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement,
Class<A> annotationType) {
return AnnotationUtils.findAnnotation(annotatedElement, annotationType);
}
} }
} }
...@@ -15,6 +15,6 @@ ...@@ -15,6 +15,6 @@
*/ */
/** /**
* Auto-configuration for Jersey server actuator metrics. * Auto-configuration for Jersey actuator metrics.
*/ */
package org.springframework.boot.actuate.autoconfigure.metrics.jersey2.server; package org.springframework.boot.actuate.autoconfigure.metrics.jersey;
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.autoconfigure.metrics.jersey2.server;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Configuration for Jersey server instrumentation.
*
* @author Michael Weirauch
* @since 2.1.0
*/
@ConfigurationProperties(prefix = "management.metrics.jersey2.server")
public class JerseyServerMetricsProperties {
private String requestsMetricName = "http.server.requests";
private boolean autoTimeRequests = true;
public String getRequestsMetricName() {
return this.requestsMetricName;
}
public void setRequestsMetricName(String requestsMetricName) {
this.requestsMetricName = requestsMetricName;
}
public boolean isAutoTimeRequests() {
return this.autoTimeRequests;
}
public void setAutoTimeRequests(boolean autoTimeRequests) {
this.autoTimeRequests = autoTimeRequests;
}
}
...@@ -61,7 +61,7 @@ org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetri ...@@ -61,7 +61,7 @@ org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetri
org.springframework.boot.actuate.autoconfigure.metrics.export.statsd.StatsdMetricsExportAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.export.statsd.StatsdMetricsExportAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront.WavefrontMetricsExportAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront.WavefrontMetricsExportAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.jdbc.DataSourcePoolMetricsAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.jdbc.DataSourcePoolMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.jersey2.server.JerseyServerMetricsAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.jersey.JerseyServerMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.orm.jpa.HibernateMetricsAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.orm.jpa.HibernateMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.web.client.HttpClientMetricsAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.web.client.HttpClientMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.web.reactive.WebFluxMetricsAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.metrics.web.reactive.WebFluxMetricsAutoConfiguration,\
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.actuate.autoconfigure.metrics.jersey2.server; package org.springframework.boot.actuate.autoconfigure.metrics.jersey;
import java.net.URI; import java.net.URI;
...@@ -97,7 +97,6 @@ public class JerseyServerMetricsAutoConfigurationTests { ...@@ -97,7 +97,6 @@ public class JerseyServerMetricsAutoConfigurationTests {
public void httpRequestsAreTimed() { public void httpRequestsAreTimed() {
this.webContextRunner.run((context) -> { this.webContextRunner.run((context) -> {
doRequest(context); doRequest(context);
MeterRegistry registry = context.getBean(MeterRegistry.class); MeterRegistry registry = context.getBean(MeterRegistry.class);
Timer timer = registry.get("http.server.requests").tag("uri", "/users/{id}") Timer timer = registry.get("http.server.requests").tag("uri", "/users/{id}")
.timer(); .timer();
......
...@@ -1835,9 +1835,9 @@ To customize the tags, provide a `@Bean` that implements `WebFluxTagsProvider`. ...@@ -1835,9 +1835,9 @@ To customize the tags, provide a `@Bean` that implements `WebFluxTagsProvider`.
[[production-ready-metrics-jersey-server]] [[production-ready-metrics-jersey-server]]
==== Jersey Server Metrics ==== Jersey Server Metrics
Auto-configuration enables the instrumentation of requests handled by the Jersey JAX-RS Auto-configuration enables the instrumentation of requests handled by the Jersey JAX-RS
implementation. When `management.metrics.jersey2.server.auto-time-requests` is `true`, implementation. When `management.metrics.web.server.auto-time-requests` is `true`, this
this instrumentation occurs for all requests. Alternatively, when set to `false`, you instrumentation occurs for all requests. Alternatively, when set to `false`, you can
can enable instrumentation by adding `@Timed` to a request-handling method: enable instrumentation by adding `@Timed` to a request-handling method:
[source,java,indent=0] [source,java,indent=0]
---- ----
...@@ -1858,7 +1858,7 @@ the class, but can be used to further customize the timer for this particular en ...@@ -1858,7 +1858,7 @@ the class, but can be used to further customize the timer for this particular en
timers require a separate metric name, and can be stacked with a short task timer. timers require a separate metric name, and can be stacked with a short task timer.
By default, metrics are generated with the name, `http.server.requests`. The name can be By default, metrics are generated with the name, `http.server.requests`. The name can be
customized by setting the `management.metrics.jersey2.server.requests-metric-name` property. customized by setting the `management.metrics.web.server.requests-metric-name` property.
By default, Jersey server metrics are tagged with the following information: By default, Jersey server metrics are tagged with the following information:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment