diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/jetty/JettyMetricsAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/jetty/JettyMetricsAutoConfigurationTests.java index f0cd52d604..6af671e950 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/jetty/JettyMetricsAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/jetty/JettyMetricsAutoConfigurationTests.java @@ -16,10 +16,7 @@ package org.springframework.boot.actuate.autoconfigure.metrics.web.jetty; -import java.util.Collections; - import io.micrometer.core.instrument.MeterRegistry; -import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Tags; import io.micrometer.core.instrument.simple.SimpleMeterRegistry; import org.junit.jupiter.api.Test; @@ -198,7 +195,7 @@ class JettyMetricsAutoConfigurationTests { } @Test - void doesNotautoConfiguresSslHandshakeMetricsWhenSslEnabledPropertyNotSpecified() { + void doesNotAutoConfigureSslHandshakeMetricsWhenSslEnabledPropertyNotSpecified() { new WebApplicationContextRunner(AnnotationConfigServletWebServerApplicationContext::new) .withConfiguration(AutoConfigurations.of(JettyMetricsAutoConfiguration.class, ServletWebServerFactoryAutoConfiguration.class)) @@ -207,7 +204,7 @@ class JettyMetricsAutoConfigurationTests { } @Test - void doesNotautoConfiguresSslHandshakeMetricsWhenSslEnabledPropertySetToFalse() { + void doesNotAutoConfigureSslHandshakeMetricsWhenSslEnabledPropertySetToFalse() { new WebApplicationContextRunner(AnnotationConfigServletWebServerApplicationContext::new) .withConfiguration(AutoConfigurations.of(JettyMetricsAutoConfiguration.class, ServletWebServerFactoryAutoConfiguration.class)) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/jetty/AbstractJettyMetricsBinder.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/jetty/AbstractJettyMetricsBinder.java new file mode 100644 index 0000000000..93b8b41fb5 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/jetty/AbstractJettyMetricsBinder.java @@ -0,0 +1,56 @@ +/* + * Copyright 2012-2021 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 + * + * https://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.metrics.web.jetty; + +import org.eclipse.jetty.server.Server; + +import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.boot.web.context.WebServerApplicationContext; +import org.springframework.boot.web.embedded.jetty.JettyWebServer; +import org.springframework.boot.web.server.WebServer; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationListener; + +/** + * Base class for binding Jetty metrics in response to an {@link ApplicationStartedEvent}. + * + * @author Andy Wilkinson + * @since 2.6.0 + */ +public abstract class AbstractJettyMetricsBinder implements ApplicationListener { + + @Override + public void onApplicationEvent(ApplicationStartedEvent event) { + Server server = findServer(event.getApplicationContext()); + if (server != null) { + bindMetrics(server); + } + } + + private Server findServer(ApplicationContext applicationContext) { + if (applicationContext instanceof WebServerApplicationContext) { + WebServer webServer = ((WebServerApplicationContext) applicationContext).getWebServer(); + if (webServer instanceof JettyWebServer) { + return ((JettyWebServer) webServer).getServer(); + } + } + return null; + } + + protected abstract void bindMetrics(Server server); + +} diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/jetty/JettyConnectionMetricsBinder.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/jetty/JettyConnectionMetricsBinder.java index eca35a17bd..17e7a67b7a 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/jetty/JettyConnectionMetricsBinder.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/jetty/JettyConnectionMetricsBinder.java @@ -23,21 +23,13 @@ import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.binder.jetty.JettyConnectionMetrics; import org.eclipse.jetty.server.Server; -import org.springframework.boot.context.event.ApplicationStartedEvent; -import org.springframework.boot.web.context.WebServerApplicationContext; -import org.springframework.boot.web.embedded.jetty.JettyWebServer; -import org.springframework.boot.web.server.WebServer; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationListener; - /** - * Binds {@link JettyConnectionMetrics} in response to the - * {@link ApplicationStartedEvent}. + * {@link AbstractJettyMetricsBinder} for {@link JettyConnectionMetrics}. * * @author Chris Bono * @since 2.6.0 */ -public class JettyConnectionMetricsBinder implements ApplicationListener { +public class JettyConnectionMetricsBinder extends AbstractJettyMetricsBinder { private final MeterRegistry meterRegistry; @@ -53,22 +45,8 @@ public class JettyConnectionMetricsBinder implements ApplicationListener { +public class JettyServerThreadPoolMetricsBinder extends AbstractJettyMetricsBinder { private final MeterRegistry meterRegistry; @@ -53,22 +46,11 @@ public class JettyServerThreadPoolMetricsBinder implements ApplicationListener { +public class JettySslHandshakeMetricsBinder extends AbstractJettyMetricsBinder { private final MeterRegistry meterRegistry; @@ -53,22 +45,8 @@ public class JettySslHandshakeMetricsBinder implements ApplicationListener