forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add auto-configuration of Jetty Metrics
This is performed by the new JettyMetricsAutoConfiguration, that is based on the existing TomcatMetricsAutoConfiguration. Fixes spring-projectsgh-12090
- Loading branch information
Showing
5 changed files
with
292 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...framework/boot/actuate/autoconfigure/metrics/web/jetty/JettyMetricsAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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.web.jetty; | ||
|
||
|
||
import java.util.Collections; | ||
|
||
import io.micrometer.core.instrument.binder.jetty.JettyStatisticsMetrics; | ||
import org.eclipse.jetty.server.Connector; | ||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.server.handler.StatisticsHandler; | ||
|
||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; | ||
import org.springframework.boot.web.embedded.jetty.ConfigurableJettyWebServerFactory; | ||
import org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory; | ||
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; | ||
import org.springframework.boot.web.server.WebServerFactoryCustomizer; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for {@link JettyStatisticsMetrics}. | ||
* | ||
* @author Even Holthe | ||
* @since 2.1.0 | ||
*/ | ||
@ConditionalOnWebApplication | ||
@ConditionalOnClass({ JettyStatisticsMetrics.class, Server.class }) | ||
public class JettyMetricsAutoConfiguration { | ||
private volatile StatisticsHandler statsHandler; | ||
|
||
@Bean | ||
@ConditionalOnMissingBean(JettyStatisticsMetrics.class) | ||
public JettyStatisticsMetrics jettyMetrics() { | ||
return new JettyStatisticsMetrics(this.statsHandler, Collections.emptyList()); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnWebApplication(type = Type.SERVLET) | ||
public WebServerFactoryCustomizer<JettyServletWebServerFactory> serverCapturingServletJettyCustomizer() { | ||
return this::statisticsCustomizer; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnWebApplication(type = Type.REACTIVE) | ||
public WebServerFactoryCustomizer<JettyReactiveWebServerFactory> serverCapturingReactiveJettyCustomizer() { | ||
return this::statisticsCustomizer; | ||
} | ||
|
||
/** | ||
* Add a statistics handler to all Jetty's {@link Connector}s. | ||
* | ||
* @param server An already configured Jetty {@link Server}. | ||
*/ | ||
private void configureStatistics(Server server) { | ||
final StatisticsHandler statsHandler = new StatisticsHandler(); | ||
|
||
for (Connector connector : server.getConnectors()) { | ||
connector.addBean(statsHandler); | ||
} | ||
|
||
this.statsHandler = statsHandler; | ||
} | ||
|
||
private void statisticsCustomizer(ConfigurableJettyWebServerFactory jettyFactory) { | ||
jettyFactory.addServerCustomizers(this::configureStatistics); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...n/java/org/springframework/boot/actuate/autoconfigure/metrics/web/jetty/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* Auto-configuration for Jetty actuator metrics. | ||
*/ | ||
package org.springframework.boot.actuate.autoconfigure.metrics.web.jetty; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
...work/boot/actuate/autoconfigure/metrics/web/jetty/JettyMetricsAutoConfigurationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/* | ||
* 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.web.jetty; | ||
|
||
import java.util.Collections; | ||
|
||
import io.micrometer.core.instrument.binder.MeterBinder; | ||
import io.micrometer.core.instrument.binder.jetty.JettyStatisticsMetrics; | ||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||
import org.junit.Test; | ||
|
||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; | ||
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; | ||
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; | ||
import org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory; | ||
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; | ||
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext; | ||
import org.springframework.boot.web.servlet.ServletContextInitializer; | ||
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.server.reactive.HttpHandler; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
/** | ||
* Tests for {@link JettyMetricsAutoConfiguration}. | ||
* | ||
* @author Even Holthe | ||
*/ | ||
public class JettyMetricsAutoConfigurationTests { | ||
@Test | ||
public void autoConfiguresJettyStatisticsMetricsWithEmbeddedServletJetty() { | ||
new WebApplicationContextRunner( | ||
AnnotationConfigServletWebServerApplicationContext::new) | ||
.withConfiguration( | ||
AutoConfigurations.of( | ||
JettyMetricsAutoConfiguration.class, | ||
ServletWebServerFactoryAutoConfiguration.class)) | ||
.withUserConfiguration(JettyMetricsAutoConfigurationTests.ServletWebServerConfiguration.class) | ||
.run((context) -> { | ||
assertThat(context) | ||
.hasSingleBean(JettyStatisticsMetrics.class); | ||
SimpleMeterRegistry registry = new SimpleMeterRegistry(); | ||
context.getBean(JettyStatisticsMetrics.class) | ||
.bindTo(registry); | ||
|
||
assertThat(registry.find("jetty.requests").meter()) | ||
.isNotNull(); | ||
assertThat(registry.find("jetty.requests.active").gauge()) | ||
.isNotNull(); | ||
}); | ||
} | ||
|
||
@Test | ||
public void sessionMetricsAreAvailableWhenEarlyMeterBinderInitializationOccurs() { | ||
new WebApplicationContextRunner( | ||
AnnotationConfigServletWebServerApplicationContext::new) | ||
.withConfiguration(AutoConfigurations.of( | ||
JettyMetricsAutoConfiguration.class, | ||
ServletWebServerFactoryAutoConfiguration.class)) | ||
.withUserConfiguration(JettyMetricsAutoConfigurationTests.ServletWebServerConfiguration.class, | ||
JettyMetricsAutoConfigurationTests.EarlyMeterBinderInitializationConfiguration.class) | ||
.run((context) -> { | ||
assertThat(context).hasSingleBean(JettyStatisticsMetrics.class); | ||
SimpleMeterRegistry registry = new SimpleMeterRegistry(); | ||
context.getBean(JettyStatisticsMetrics.class).bindTo(registry); | ||
|
||
assertThat(registry.find("jetty.requests").meter()) | ||
.isNotNull(); | ||
assertThat(registry.find("jetty.requests.active").gauge()) | ||
.isNotNull(); | ||
}); | ||
} | ||
|
||
@Test | ||
public void autoConfiguresJettyStatisticsMetricsWithEmbeddedReactiveJetty() { | ||
new ReactiveWebApplicationContextRunner( | ||
AnnotationConfigReactiveWebServerApplicationContext::new) | ||
.withConfiguration(AutoConfigurations.of( | ||
JettyMetricsAutoConfiguration.class, | ||
ReactiveWebServerFactoryAutoConfiguration.class)) | ||
.withUserConfiguration(JettyMetricsAutoConfigurationTests.ReactiveWebServerConfiguration.class) | ||
.run((context) -> { | ||
assertThat(context).hasSingleBean(JettyStatisticsMetrics.class); | ||
SimpleMeterRegistry registry = new SimpleMeterRegistry(); | ||
context.getBean(JettyStatisticsMetrics.class).bindTo(registry); | ||
|
||
assertThat(registry.find("jetty.requests").meter()) | ||
.isNotNull(); | ||
assertThat(registry.find("jetty.requests.active").gauge()) | ||
.isNotNull(); | ||
}); | ||
} | ||
|
||
@Test | ||
public void autoConfiguresJettyStatisticsMetricsWithStandaloneJetty() { | ||
new WebApplicationContextRunner() | ||
.withConfiguration( | ||
AutoConfigurations.of(JettyMetricsAutoConfiguration.class)) | ||
.run((context) -> assertThat(context).hasSingleBean(JettyMetricsAutoConfiguration.class)); | ||
} | ||
|
||
@Test | ||
public void allowsCustomJettyStatisticsMetricsToBeUsed() { | ||
new WebApplicationContextRunner() | ||
.withConfiguration( | ||
AutoConfigurations.of(JettyMetricsAutoConfiguration.class)) | ||
.withUserConfiguration(JettyMetricsAutoConfigurationTests.CustomJettyMetrics.class) | ||
.run((context) -> assertThat(context).hasSingleBean(JettyStatisticsMetrics.class) | ||
.hasBean("customJettyMetrics")); | ||
} | ||
|
||
@Configuration | ||
static class ServletWebServerConfiguration { | ||
|
||
@Bean | ||
public JettyServletWebServerFactory jettyFactory() { | ||
return new JettyServletWebServerFactory(0); | ||
} | ||
|
||
} | ||
|
||
@Configuration | ||
static class ReactiveWebServerConfiguration { | ||
|
||
@Bean | ||
public JettyReactiveWebServerFactory tomcatFactory() { | ||
return new JettyReactiveWebServerFactory(0); | ||
} | ||
|
||
@Bean | ||
public HttpHandler httpHandler() { | ||
return mock(HttpHandler.class); | ||
} | ||
|
||
} | ||
|
||
@Configuration | ||
static class CustomJettyMetrics { | ||
|
||
@Bean | ||
public JettyStatisticsMetrics customJettyMetrics() { | ||
return new JettyStatisticsMetrics(null, Collections.emptyList()); | ||
} | ||
|
||
} | ||
|
||
@Configuration | ||
static class EarlyMeterBinderInitializationConfiguration { | ||
|
||
@Bean | ||
public ServletContextInitializer earlyInitializer(ApplicationContext context) { | ||
return (servletContext) -> context.getBeansOfType(MeterBinder.class); | ||
} | ||
|
||
} | ||
} |