Skip to content

Commit

Permalink
Add auto-configuration of Jetty Metrics
Browse files Browse the repository at this point in the history
This is performed by the new JettyMetricsAutoConfiguration, that is
based on the existing TomcatMetricsAutoConfiguration.

Fixes spring-projectsgh-12090
  • Loading branch information
evenh committed Feb 20, 2018
1 parent 482ecc6 commit 8a40186
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spring-boot-project/spring-boot-actuator-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@
<artifactId>aspectjweaver</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
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;
}

private void wrapExistingHandlersInStatisticsHandler(Server server) {
final Handler[] existingHandlers = server.getHandlers();
final StatisticsHandler statsHandler = new StatisticsHandler();

statsHandler.setHandler(new HandlerCollection(existingHandlers));
server.setHandler(statsHandler);

this.statsHandler = statsHandler;
}

private void statisticsCustomizer(ConfigurableJettyWebServerFactory jettyFactory) {
jettyFactory.addServerCustomizers(this::wrapExistingHandlersInStatisticsHandler);
}
}
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;
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ org.springframework.boot.actuate.autoconfigure.metrics.web.client.RestTemplateMe
org.springframework.boot.actuate.autoconfigure.metrics.web.reactive.WebFluxMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.web.servlet.WebMvcMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.web.tomcat.TomcatMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.web.jetty.JettyMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.mongo.MongoHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.neo4j.Neo4jHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.redis.RedisHealthIndicatorAutoConfiguration,\
Expand Down
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);
}

}
}

0 comments on commit 8a40186

Please sign in to comment.