Added support for Tomcat's Valve; fixes gh-1329
This commit is contained in:
@@ -26,10 +26,14 @@ import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
|
||||
import org.apache.catalina.Valve;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;
|
||||
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.cloud.sleuth.CurrentTraceContext;
|
||||
import org.springframework.cloud.sleuth.SpanNamer;
|
||||
@@ -38,9 +42,12 @@ import org.springframework.cloud.sleuth.http.HttpServerHandler;
|
||||
import org.springframework.cloud.sleuth.instrument.web.TraceWebAspect;
|
||||
import org.springframework.cloud.sleuth.instrument.web.mvc.SpanCustomizingAsyncHandlerInterceptor;
|
||||
import org.springframework.cloud.sleuth.instrument.web.servlet.TracingFilter;
|
||||
import org.springframework.cloud.sleuth.instrument.web.tomcat.TraceValve;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
||||
|
||||
@@ -57,74 +64,84 @@ import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
||||
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
|
||||
@ConditionalOnClass(HandlerInterceptorAdapter.class)
|
||||
@Import(SpanCustomizingAsyncHandlerInterceptor.class)
|
||||
@ConditionalOnProperty(value = "spring.sleuth.web.servlet.enabled", matchIfMissing = true)
|
||||
class TraceWebServletConfiguration {
|
||||
|
||||
@Bean
|
||||
TraceWebAspect traceWebAspect(Tracer tracer, CurrentTraceContext currentTraceContext, SpanNamer spanNamer) {
|
||||
return new TraceWebAspect(tracer, currentTraceContext, spanNamer);
|
||||
}
|
||||
|
||||
@Bean
|
||||
FilterRegistrationBean traceWebFilter(BeanFactory beanFactory, SleuthWebProperties webProperties) {
|
||||
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new LazyTracingFilter(beanFactory));
|
||||
filterRegistrationBean.setDispatcherTypes(DispatcherType.ASYNC, DispatcherType.ERROR, DispatcherType.FORWARD,
|
||||
DispatcherType.INCLUDE, DispatcherType.REQUEST);
|
||||
filterRegistrationBean.setOrder(webProperties.getFilterOrder());
|
||||
return filterRegistrationBean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Nested config that configures Web MVC if it's present (without adding a runtime
|
||||
* dependency to it).
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnProperty(value = "spring.sleuth.web.servlet.enabled", matchIfMissing = true)
|
||||
static class ServletConfiguration {
|
||||
@ConditionalOnClass(WebMvcConfigurer.class)
|
||||
@Import(TraceWebMvcConfigurer.class)
|
||||
protected static class TraceWebMvcAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
TraceWebAspect traceWebAspect(Tracer tracer, CurrentTraceContext currentTraceContext, SpanNamer spanNamer) {
|
||||
return new TraceWebAspect(tracer, currentTraceContext, spanNamer);
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass({ Valve.class, ConfigurableTomcatWebServerFactory.class })
|
||||
@ConditionalOnProperty(value = "spring.sleuth.web.tomcat.enabled", matchIfMissing = true)
|
||||
protected static class TraceTomcatConfiguration {
|
||||
|
||||
static final String CUSTOMIZER_NAME = "traceTomcatWebServerFactoryCustomizer";
|
||||
|
||||
@Bean(name = CUSTOMIZER_NAME)
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
WebServerFactoryCustomizer<ConfigurableTomcatWebServerFactory> traceTomcatWebServerFactoryCustomizer(
|
||||
HttpServerHandler httpServerHandler, CurrentTraceContext currentTraceContext) {
|
||||
return factory -> factory.addEngineValves(new TraceValve(httpServerHandler, currentTraceContext));
|
||||
}
|
||||
|
||||
@Bean
|
||||
FilterRegistrationBean traceWebFilter(BeanFactory beanFactory, SleuthWebProperties webProperties) {
|
||||
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(
|
||||
new LazyTracingFilter(beanFactory));
|
||||
filterRegistrationBean.setDispatcherTypes(DispatcherType.ASYNC, DispatcherType.ERROR,
|
||||
DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.REQUEST);
|
||||
filterRegistrationBean.setOrder(webProperties.getFilterOrder());
|
||||
return filterRegistrationBean;
|
||||
}
|
||||
|
||||
static final class LazyTracingFilter implements Filter {
|
||||
|
||||
private final BeanFactory beanFactory;
|
||||
|
||||
private Filter tracingFilter;
|
||||
|
||||
LazyTracingFilter(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Nested config that configures Web MVC if it's present (without adding a runtime
|
||||
* dependency to it).
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(WebMvcConfigurer.class)
|
||||
@Import(TraceWebMvcConfigurer.class)
|
||||
protected static class TraceWebMvcAutoConfiguration {
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
tracingFilter().init(filterConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
tracingFilter().doFilter(request, response, chain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
tracingFilter().destroy();
|
||||
}
|
||||
|
||||
private Filter tracingFilter() {
|
||||
if (this.tracingFilter == null) {
|
||||
this.tracingFilter = TracingFilter.create(this.beanFactory.getBean(CurrentTraceContext.class),
|
||||
this.beanFactory.getBean(HttpServerHandler.class));
|
||||
}
|
||||
return this.tracingFilter;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
final class LazyTracingFilter implements Filter {
|
||||
|
||||
private final BeanFactory beanFactory;
|
||||
|
||||
private Filter tracingFilter;
|
||||
|
||||
LazyTracingFilter(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
tracingFilter().init(filterConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
tracingFilter().doFilter(request, response, chain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
tracingFilter().destroy();
|
||||
}
|
||||
|
||||
private Filter tracingFilter() {
|
||||
if (this.tracingFilter == null) {
|
||||
this.tracingFilter = TracingFilter.create(this.beanFactory.getBean(CurrentTraceContext.class),
|
||||
this.beanFactory.getBean(HttpServerHandler.class));
|
||||
}
|
||||
return this.tracingFilter;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -131,6 +131,12 @@
|
||||
"description": "Enable tracing instrumentation for WebClient.",
|
||||
"defaultValue": true
|
||||
},
|
||||
{
|
||||
"name": "spring.sleuth.web.tomcat.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "Enable tracing instrumentation for Tomcat.",
|
||||
"defaultValue": true
|
||||
},
|
||||
{
|
||||
"name": "spring.sleuth.integration.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2013-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.cloud.sleuth.autoconfig.instrument.web;
|
||||
|
||||
import org.assertj.core.api.BDDAssertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.test.context.FilteredClassLoader;
|
||||
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
||||
import org.springframework.cloud.sleuth.autoconfig.TraceNoOpAutoConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
class TraceTomcatConfigurationTests {
|
||||
|
||||
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
|
||||
.withPropertyValues("spring.sleuth.noop.enabled=true").withUserConfiguration(TestConfig.class)
|
||||
.withConfiguration(
|
||||
AutoConfigurations.of(TraceWebServletConfiguration.class, TraceNoOpAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
public void should_not_register_customizer_when_tomcat_not_present() throws Exception {
|
||||
contextRunner.withClassLoader(new FilteredClassLoader("org.apache.catalina.Valve")).run(context -> BDDAssertions
|
||||
.then(context).doesNotHaveBean(TraceWebServletConfiguration.TraceTomcatConfiguration.CUSTOMIZER_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_not_register_customizer_when_tomcat_customizer_not_present() throws Exception {
|
||||
contextRunner
|
||||
.withClassLoader(new FilteredClassLoader(
|
||||
"org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory"))
|
||||
.run(context -> BDDAssertions.then(context)
|
||||
.doesNotHaveBean(TraceWebServletConfiguration.TraceTomcatConfiguration.CUSTOMIZER_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_not_register_customizer_when_tomcat_disabled() throws Exception {
|
||||
contextRunner.withPropertyValues("spring.sleuth.web.tomcat.enabled=false").run(context -> BDDAssertions
|
||||
.then(context).doesNotHaveBean(TraceWebServletConfiguration.TraceTomcatConfiguration.CUSTOMIZER_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_not_register_customizer_when_servlet_disabled() throws Exception {
|
||||
contextRunner.withPropertyValues("spring.sleuth.web.servlet.enabled=false").run(context -> BDDAssertions
|
||||
.then(context).doesNotHaveBean(TraceWebServletConfiguration.TraceTomcatConfiguration.CUSTOMIZER_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_not_register_customizer_when_web_disabled() throws Exception {
|
||||
contextRunner.withPropertyValues("spring.sleuth.web.enabled=false").run(context -> BDDAssertions.then(context)
|
||||
.doesNotHaveBean(TraceWebServletConfiguration.TraceTomcatConfiguration.CUSTOMIZER_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_register_customizer_when_tomcat_present() throws Exception {
|
||||
contextRunner.run(context -> BDDAssertions.then(context)
|
||||
.hasBean(TraceWebServletConfiguration.TraceTomcatConfiguration.CUSTOMIZER_NAME));
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableConfigurationProperties(SleuthWebProperties.class)
|
||||
static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user