Polish "Auto-configure Jetty connection and SSL metrics"
See gh-26418
This commit is contained in:
parent
e6c43a32c8
commit
d21f8df1ad
|
@ -16,10 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.boot.actuate.autoconfigure.metrics.web.jetty;
|
package org.springframework.boot.actuate.autoconfigure.metrics.web.jetty;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
import io.micrometer.core.instrument.MeterRegistry;
|
import io.micrometer.core.instrument.MeterRegistry;
|
||||||
import io.micrometer.core.instrument.Tag;
|
|
||||||
import io.micrometer.core.instrument.Tags;
|
import io.micrometer.core.instrument.Tags;
|
||||||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
@ -198,7 +195,7 @@ class JettyMetricsAutoConfigurationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void doesNotautoConfiguresSslHandshakeMetricsWhenSslEnabledPropertyNotSpecified() {
|
void doesNotAutoConfigureSslHandshakeMetricsWhenSslEnabledPropertyNotSpecified() {
|
||||||
new WebApplicationContextRunner(AnnotationConfigServletWebServerApplicationContext::new)
|
new WebApplicationContextRunner(AnnotationConfigServletWebServerApplicationContext::new)
|
||||||
.withConfiguration(AutoConfigurations.of(JettyMetricsAutoConfiguration.class,
|
.withConfiguration(AutoConfigurations.of(JettyMetricsAutoConfiguration.class,
|
||||||
ServletWebServerFactoryAutoConfiguration.class))
|
ServletWebServerFactoryAutoConfiguration.class))
|
||||||
|
@ -207,7 +204,7 @@ class JettyMetricsAutoConfigurationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void doesNotautoConfiguresSslHandshakeMetricsWhenSslEnabledPropertySetToFalse() {
|
void doesNotAutoConfigureSslHandshakeMetricsWhenSslEnabledPropertySetToFalse() {
|
||||||
new WebApplicationContextRunner(AnnotationConfigServletWebServerApplicationContext::new)
|
new WebApplicationContextRunner(AnnotationConfigServletWebServerApplicationContext::new)
|
||||||
.withConfiguration(AutoConfigurations.of(JettyMetricsAutoConfiguration.class,
|
.withConfiguration(AutoConfigurations.of(JettyMetricsAutoConfiguration.class,
|
||||||
ServletWebServerFactoryAutoConfiguration.class))
|
ServletWebServerFactoryAutoConfiguration.class))
|
||||||
|
|
|
@ -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<ApplicationStartedEvent> {
|
||||||
|
|
||||||
|
@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);
|
||||||
|
|
||||||
|
}
|
|
@ -23,21 +23,13 @@ import io.micrometer.core.instrument.Tag;
|
||||||
import io.micrometer.core.instrument.binder.jetty.JettyConnectionMetrics;
|
import io.micrometer.core.instrument.binder.jetty.JettyConnectionMetrics;
|
||||||
import org.eclipse.jetty.server.Server;
|
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 AbstractJettyMetricsBinder} for {@link JettyConnectionMetrics}.
|
||||||
* {@link ApplicationStartedEvent}.
|
|
||||||
*
|
*
|
||||||
* @author Chris Bono
|
* @author Chris Bono
|
||||||
* @since 2.6.0
|
* @since 2.6.0
|
||||||
*/
|
*/
|
||||||
public class JettyConnectionMetricsBinder implements ApplicationListener<ApplicationStartedEvent> {
|
public class JettyConnectionMetricsBinder extends AbstractJettyMetricsBinder {
|
||||||
|
|
||||||
private final MeterRegistry meterRegistry;
|
private final MeterRegistry meterRegistry;
|
||||||
|
|
||||||
|
@ -53,22 +45,8 @@ public class JettyConnectionMetricsBinder implements ApplicationListener<Applica
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onApplicationEvent(ApplicationStartedEvent event) {
|
protected void bindMetrics(Server server) {
|
||||||
ApplicationContext applicationContext = event.getApplicationContext();
|
JettyConnectionMetrics.addToAllConnectors(server, this.meterRegistry, this.tags);
|
||||||
Server server = findServer(applicationContext);
|
|
||||||
if (server != null) {
|
|
||||||
JettyConnectionMetrics.addToAllConnectors(server, this.meterRegistry, this.tags);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Server findServer(ApplicationContext applicationContext) {
|
|
||||||
if (applicationContext instanceof WebServerApplicationContext) {
|
|
||||||
WebServer webServer = ((WebServerApplicationContext) applicationContext).getWebServer();
|
|
||||||
if (webServer instanceof JettyWebServer) {
|
|
||||||
return ((JettyWebServer) webServer).getServer();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,23 +21,16 @@ import java.util.Collections;
|
||||||
import io.micrometer.core.instrument.MeterRegistry;
|
import io.micrometer.core.instrument.MeterRegistry;
|
||||||
import io.micrometer.core.instrument.Tag;
|
import io.micrometer.core.instrument.Tag;
|
||||||
import io.micrometer.core.instrument.binder.jetty.JettyServerThreadPoolMetrics;
|
import io.micrometer.core.instrument.binder.jetty.JettyServerThreadPoolMetrics;
|
||||||
|
import org.eclipse.jetty.server.Server;
|
||||||
import org.eclipse.jetty.util.thread.ThreadPool;
|
import org.eclipse.jetty.util.thread.ThreadPool;
|
||||||
|
|
||||||
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 JettyServerThreadPoolMetrics} in response to the
|
* {@link AbstractJettyMetricsBinder} for {@link JettyServerThreadPoolMetrics}.
|
||||||
* {@link ApplicationStartedEvent}.
|
|
||||||
*
|
*
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
* @since 2.1.0
|
* @since 2.1.0
|
||||||
*/
|
*/
|
||||||
public class JettyServerThreadPoolMetricsBinder implements ApplicationListener<ApplicationStartedEvent> {
|
public class JettyServerThreadPoolMetricsBinder extends AbstractJettyMetricsBinder {
|
||||||
|
|
||||||
private final MeterRegistry meterRegistry;
|
private final MeterRegistry meterRegistry;
|
||||||
|
|
||||||
|
@ -53,22 +46,11 @@ public class JettyServerThreadPoolMetricsBinder implements ApplicationListener<A
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onApplicationEvent(ApplicationStartedEvent event) {
|
protected void bindMetrics(Server server) {
|
||||||
ApplicationContext applicationContext = event.getApplicationContext();
|
ThreadPool threadPool = server.getThreadPool();
|
||||||
ThreadPool threadPool = findThreadPool(applicationContext);
|
|
||||||
if (threadPool != null) {
|
if (threadPool != null) {
|
||||||
new JettyServerThreadPoolMetrics(threadPool, this.tags).bindTo(this.meterRegistry);
|
new JettyServerThreadPoolMetrics(threadPool, this.tags).bindTo(this.meterRegistry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ThreadPool findThreadPool(ApplicationContext applicationContext) {
|
|
||||||
if (applicationContext instanceof WebServerApplicationContext) {
|
|
||||||
WebServer webServer = ((WebServerApplicationContext) applicationContext).getWebServer();
|
|
||||||
if (webServer instanceof JettyWebServer) {
|
|
||||||
return ((JettyWebServer) webServer).getServer().getThreadPool();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,21 +23,13 @@ import io.micrometer.core.instrument.Tag;
|
||||||
import io.micrometer.core.instrument.binder.jetty.JettySslHandshakeMetrics;
|
import io.micrometer.core.instrument.binder.jetty.JettySslHandshakeMetrics;
|
||||||
import org.eclipse.jetty.server.Server;
|
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 JettySslHandshakeMetrics} in response to the
|
* {@link AbstractJettyMetricsBinder} for {@link JettySslHandshakeMetrics}.
|
||||||
* {@link ApplicationStartedEvent}.
|
|
||||||
*
|
*
|
||||||
* @author Chris Bono
|
* @author Chris Bono
|
||||||
* @since 2.6.0
|
* @since 2.6.0
|
||||||
*/
|
*/
|
||||||
public class JettySslHandshakeMetricsBinder implements ApplicationListener<ApplicationStartedEvent> {
|
public class JettySslHandshakeMetricsBinder extends AbstractJettyMetricsBinder {
|
||||||
|
|
||||||
private final MeterRegistry meterRegistry;
|
private final MeterRegistry meterRegistry;
|
||||||
|
|
||||||
|
@ -53,22 +45,8 @@ public class JettySslHandshakeMetricsBinder implements ApplicationListener<Appli
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onApplicationEvent(ApplicationStartedEvent event) {
|
protected void bindMetrics(Server server) {
|
||||||
ApplicationContext applicationContext = event.getApplicationContext();
|
JettySslHandshakeMetrics.addToAllConnectors(server, this.meterRegistry, this.tags);
|
||||||
Server server = findServer(applicationContext);
|
|
||||||
if (server != null) {
|
|
||||||
JettySslHandshakeMetrics.addToAllConnectors(server, this.meterRegistry, this.tags);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Server findServer(ApplicationContext applicationContext) {
|
|
||||||
if (applicationContext instanceof WebServerApplicationContext) {
|
|
||||||
WebServer webServer = ((WebServerApplicationContext) applicationContext).getWebServer();
|
|
||||||
if (webServer instanceof JettyWebServer) {
|
|
||||||
return ((JettyWebServer) webServer).getServer();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -954,6 +954,7 @@ To disable the auto-configured connection pool metrics, set the following proper
|
||||||
[[actuator.metrics.supported.jetty]]
|
[[actuator.metrics.supported.jetty]]
|
||||||
==== Jetty Metrics
|
==== Jetty Metrics
|
||||||
Auto-configuration will bind metrics for Jetty's `ThreadPool` using Micrometer's `JettyServerThreadPoolMetrics`.
|
Auto-configuration will bind metrics for Jetty's `ThreadPool` using Micrometer's `JettyServerThreadPoolMetrics`.
|
||||||
|
Metrics for Jetty's `Connector`s are bound using Micrometer's `JettyConnectionMetrics` and, in addition when configprop:server.ssl.enabled[] is set to `true`, Micrometer's `JettySslHandshakeMetrics`.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue