From 44dd4d17eb0194e3edfa37c49002e2101dd4496a Mon Sep 17 00:00:00 2001 From: buildmaster Date: Sat, 17 Jul 2021 17:34:57 +0000 Subject: [PATCH] Bumping versions --- .../web/TraceWebServletConfiguration.java | 294 +++++++++--------- .../instrument/web/tomcat/TraceValve.java | 178 +++++------ .../web/tomcat/TraceValveTests.java | 224 ++++++------- 3 files changed, 348 insertions(+), 348 deletions(-) diff --git a/spring-cloud-sleuth-autoconfigure/src/main/java/org/springframework/cloud/sleuth/autoconfig/instrument/web/TraceWebServletConfiguration.java b/spring-cloud-sleuth-autoconfigure/src/main/java/org/springframework/cloud/sleuth/autoconfig/instrument/web/TraceWebServletConfiguration.java index 3a187fcd7..abb21771a 100644 --- a/spring-cloud-sleuth-autoconfigure/src/main/java/org/springframework/cloud/sleuth/autoconfig/instrument/web/TraceWebServletConfiguration.java +++ b/spring-cloud-sleuth-autoconfigure/src/main/java/org/springframework/cloud/sleuth/autoconfig/instrument/web/TraceWebServletConfiguration.java @@ -1,147 +1,147 @@ -/* - * 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 java.io.IOException; - -import javax.servlet.DispatcherType; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -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; -import org.springframework.cloud.sleuth.Tracer; -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; - -/** - * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration - * Auto-configuration} enables tracing to HTTP requests. - * - * @author Marcin Grzejszczak - * @author Spencer Gibb - * @since 1.0.0 - */ -@Configuration(proxyBeanMethods = false) -@ConditionalOnSleuthWeb -@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) - @ConditionalOnClass(WebMvcConfigurer.class) - @Import(TraceWebMvcConfigurer.class) - protected static class TraceWebMvcAutoConfiguration { - - } - - @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 traceTomcatWebServerFactoryCustomizer( - HttpServerHandler httpServerHandler, CurrentTraceContext currentTraceContext) { - return factory -> factory.addEngineValves(new TraceValve(httpServerHandler, currentTraceContext)); - } - - } - - static 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; - } - - } - -} +/* + * 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 java.io.IOException; + +import javax.servlet.DispatcherType; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +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; +import org.springframework.cloud.sleuth.Tracer; +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; + +/** + * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration + * Auto-configuration} enables tracing to HTTP requests. + * + * @author Marcin Grzejszczak + * @author Spencer Gibb + * @since 1.0.0 + */ +@Configuration(proxyBeanMethods = false) +@ConditionalOnSleuthWeb +@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) + @ConditionalOnClass(WebMvcConfigurer.class) + @Import(TraceWebMvcConfigurer.class) + protected static class TraceWebMvcAutoConfiguration { + + } + + @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 traceTomcatWebServerFactoryCustomizer( + HttpServerHandler httpServerHandler, CurrentTraceContext currentTraceContext) { + return factory -> factory.addEngineValves(new TraceValve(httpServerHandler, currentTraceContext)); + } + + } + + static 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; + } + + } + +} diff --git a/spring-cloud-sleuth-instrumentation/src/main/java/org/springframework/cloud/sleuth/instrument/web/tomcat/TraceValve.java b/spring-cloud-sleuth-instrumentation/src/main/java/org/springframework/cloud/sleuth/instrument/web/tomcat/TraceValve.java index cfc675603..4472c8557 100644 --- a/spring-cloud-sleuth-instrumentation/src/main/java/org/springframework/cloud/sleuth/instrument/web/tomcat/TraceValve.java +++ b/spring-cloud-sleuth-instrumentation/src/main/java/org/springframework/cloud/sleuth/instrument/web/tomcat/TraceValve.java @@ -1,89 +1,89 @@ -/* - * 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.instrument.web.tomcat; - -import java.io.IOException; - -import javax.servlet.ServletException; - -import org.apache.catalina.Valve; -import org.apache.catalina.connector.Request; -import org.apache.catalina.connector.Response; -import org.apache.catalina.valves.ValveBase; - -import org.springframework.cloud.sleuth.CurrentTraceContext; -import org.springframework.cloud.sleuth.Span; -import org.springframework.cloud.sleuth.SpanCustomizer; -import org.springframework.cloud.sleuth.TraceContext; -import org.springframework.cloud.sleuth.http.HttpServerHandler; -import org.springframework.cloud.sleuth.instrument.web.servlet.HttpServletRequestWrapper; -import org.springframework.cloud.sleuth.instrument.web.servlet.HttpServletResponseWrapper; -import org.springframework.core.log.LogAccessor; - -/** - * A trace representation of a {@link Valve}. - * - * @author Marcin Grzejszczak - * @since 3.1.0 - */ -public class TraceValve extends ValveBase { - - private static final LogAccessor log = new LogAccessor(TraceValve.class); - - private final HttpServerHandler httpServerHandler; - - private final CurrentTraceContext currentTraceContext; - - public TraceValve(HttpServerHandler httpServerHandler, CurrentTraceContext currentTraceContext) { - this.httpServerHandler = httpServerHandler; - this.currentTraceContext = currentTraceContext; - setAsyncSupported(true); - } - - @Override - public void invoke(Request request, Response response) throws IOException, ServletException { - Exception ex = null; - Span handleReceive = this.httpServerHandler - .handleReceive(HttpServletRequestWrapper.create(request.getRequest())); - if (log.isDebugEnabled()) { - log.debug("Created a server receive span [" + handleReceive + "]"); - } - request.setAttribute(SpanCustomizer.class.getName(), handleReceive); - request.setAttribute(TraceContext.class.getName(), handleReceive.context()); - request.setAttribute(Span.class.getName(), handleReceive); - try (CurrentTraceContext.Scope ws = this.currentTraceContext.maybeScope(handleReceive.context())) { - Valve next = getNext(); - if (null == next) { - // no next valve - return; - } - next.invoke(request, response); - } - catch (Exception exception) { - ex = exception; - throw exception; - } - finally { - this.httpServerHandler.handleSend( - HttpServletResponseWrapper.create(request.getRequest(), response.getResponse(), ex), handleReceive); - if (log.isDebugEnabled()) { - log.debug("Handled send of span [" + handleReceive + "]"); - } - } - } - -} +/* + * 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.instrument.web.tomcat; + +import java.io.IOException; + +import javax.servlet.ServletException; + +import org.apache.catalina.Valve; +import org.apache.catalina.connector.Request; +import org.apache.catalina.connector.Response; +import org.apache.catalina.valves.ValveBase; + +import org.springframework.cloud.sleuth.CurrentTraceContext; +import org.springframework.cloud.sleuth.Span; +import org.springframework.cloud.sleuth.SpanCustomizer; +import org.springframework.cloud.sleuth.TraceContext; +import org.springframework.cloud.sleuth.http.HttpServerHandler; +import org.springframework.cloud.sleuth.instrument.web.servlet.HttpServletRequestWrapper; +import org.springframework.cloud.sleuth.instrument.web.servlet.HttpServletResponseWrapper; +import org.springframework.core.log.LogAccessor; + +/** + * A trace representation of a {@link Valve}. + * + * @author Marcin Grzejszczak + * @since 3.1.0 + */ +public class TraceValve extends ValveBase { + + private static final LogAccessor log = new LogAccessor(TraceValve.class); + + private final HttpServerHandler httpServerHandler; + + private final CurrentTraceContext currentTraceContext; + + public TraceValve(HttpServerHandler httpServerHandler, CurrentTraceContext currentTraceContext) { + this.httpServerHandler = httpServerHandler; + this.currentTraceContext = currentTraceContext; + setAsyncSupported(true); + } + + @Override + public void invoke(Request request, Response response) throws IOException, ServletException { + Exception ex = null; + Span handleReceive = this.httpServerHandler + .handleReceive(HttpServletRequestWrapper.create(request.getRequest())); + if (log.isDebugEnabled()) { + log.debug("Created a server receive span [" + handleReceive + "]"); + } + request.setAttribute(SpanCustomizer.class.getName(), handleReceive); + request.setAttribute(TraceContext.class.getName(), handleReceive.context()); + request.setAttribute(Span.class.getName(), handleReceive); + try (CurrentTraceContext.Scope ws = this.currentTraceContext.maybeScope(handleReceive.context())) { + Valve next = getNext(); + if (null == next) { + // no next valve + return; + } + next.invoke(request, response); + } + catch (Exception exception) { + ex = exception; + throw exception; + } + finally { + this.httpServerHandler.handleSend( + HttpServletResponseWrapper.create(request.getRequest(), response.getResponse(), ex), handleReceive); + if (log.isDebugEnabled()) { + log.debug("Handled send of span [" + handleReceive + "]"); + } + } + } + +} diff --git a/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/instrument/web/tomcat/TraceValveTests.java b/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/instrument/web/tomcat/TraceValveTests.java index 825dce77b..44eb67bc1 100644 --- a/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/instrument/web/tomcat/TraceValveTests.java +++ b/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/instrument/web/tomcat/TraceValveTests.java @@ -1,112 +1,112 @@ -/* - * 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.instrument.web.tomcat; - -import java.io.IOException; - -import javax.servlet.ServletException; - -import org.apache.catalina.Valve; -import org.apache.catalina.connector.Connector; -import org.apache.catalina.connector.Request; -import org.apache.catalina.connector.Response; -import org.apache.catalina.valves.ValveBase; -import org.assertj.core.api.BDDAssertions; -import org.junit.jupiter.api.Test; - -import org.springframework.cloud.sleuth.Span; -import org.springframework.cloud.sleuth.TraceContext; -import org.springframework.cloud.sleuth.http.HttpServerHandler; -import org.springframework.cloud.sleuth.http.HttpServerRequest; -import org.springframework.cloud.sleuth.http.HttpServerResponse; -import org.springframework.cloud.sleuth.tracer.SimpleCurrentTraceContext; -import org.springframework.cloud.sleuth.tracer.SimpleSpan; - -import static org.assertj.core.api.BDDAssertions.then; - -class TraceValveTests { - - SimpleSpan simpleSpan = new SimpleSpan(); - - HttpServerHandler httpServerHandler = new HttpServerHandler() { - @Override - public SimpleSpan handleReceive(HttpServerRequest request) { - return simpleSpan.start(); - } - - @Override - public void handleSend(HttpServerResponse response, Span span) { - span.end(); - } - }; - - TraceValve traceValve = new TraceValve(this.httpServerHandler, new SimpleCurrentTraceContext()); - - @Test - void should_populate_tracecontext_attribute_for_tracing_filter_to_reuse() throws ServletException, IOException { - Request request = request(); - - this.traceValve.invoke(request, new Response()); - - then(request.getAttribute(TraceContext.class.getName())).isNotNull(); - thenSpanIsStartedAndStopped(); - } - - @Test - void should_have_async_supported_by_default() throws ServletException, IOException { - TraceValve traceValve = new TraceValve(null, null); - - BDDAssertions.then(traceValve.isAsyncSupported()).isTrue(); - } - - private void thenSpanIsStartedAndStopped() { - then(simpleSpan.started).isTrue(); - then(simpleSpan.ended).isTrue(); - } - - @Test - void should_populate_tracecontext_attribute_for_tracing_filter_to_reuse_when_there_is_another_valve_in_chain() - throws ServletException, IOException { - Request request = request(); - - new TraceValve(this.httpServerHandler, new SimpleCurrentTraceContext()) { - @Override - public Valve getNext() { - return new MyValve(); - } - }.invoke(request, new Response()); - - then(request.getAttribute(TraceContext.class.getName())).isNotNull(); - thenSpanIsStartedAndStopped(); - } - - private Request request() { - Request request = new Request(new Connector()); - request.setCoyoteRequest(new org.apache.coyote.Request()); - return request; - } - -} - -class MyValve extends ValveBase { - - @Override - public void invoke(Request request, Response response) throws IOException, ServletException { - - } - -} +/* + * 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.instrument.web.tomcat; + +import java.io.IOException; + +import javax.servlet.ServletException; + +import org.apache.catalina.Valve; +import org.apache.catalina.connector.Connector; +import org.apache.catalina.connector.Request; +import org.apache.catalina.connector.Response; +import org.apache.catalina.valves.ValveBase; +import org.assertj.core.api.BDDAssertions; +import org.junit.jupiter.api.Test; + +import org.springframework.cloud.sleuth.Span; +import org.springframework.cloud.sleuth.TraceContext; +import org.springframework.cloud.sleuth.http.HttpServerHandler; +import org.springframework.cloud.sleuth.http.HttpServerRequest; +import org.springframework.cloud.sleuth.http.HttpServerResponse; +import org.springframework.cloud.sleuth.tracer.SimpleCurrentTraceContext; +import org.springframework.cloud.sleuth.tracer.SimpleSpan; + +import static org.assertj.core.api.BDDAssertions.then; + +class TraceValveTests { + + SimpleSpan simpleSpan = new SimpleSpan(); + + HttpServerHandler httpServerHandler = new HttpServerHandler() { + @Override + public SimpleSpan handleReceive(HttpServerRequest request) { + return simpleSpan.start(); + } + + @Override + public void handleSend(HttpServerResponse response, Span span) { + span.end(); + } + }; + + TraceValve traceValve = new TraceValve(this.httpServerHandler, new SimpleCurrentTraceContext()); + + @Test + void should_populate_tracecontext_attribute_for_tracing_filter_to_reuse() throws ServletException, IOException { + Request request = request(); + + this.traceValve.invoke(request, new Response()); + + then(request.getAttribute(TraceContext.class.getName())).isNotNull(); + thenSpanIsStartedAndStopped(); + } + + @Test + void should_have_async_supported_by_default() throws ServletException, IOException { + TraceValve traceValve = new TraceValve(null, null); + + BDDAssertions.then(traceValve.isAsyncSupported()).isTrue(); + } + + private void thenSpanIsStartedAndStopped() { + then(simpleSpan.started).isTrue(); + then(simpleSpan.ended).isTrue(); + } + + @Test + void should_populate_tracecontext_attribute_for_tracing_filter_to_reuse_when_there_is_another_valve_in_chain() + throws ServletException, IOException { + Request request = request(); + + new TraceValve(this.httpServerHandler, new SimpleCurrentTraceContext()) { + @Override + public Valve getNext() { + return new MyValve(); + } + }.invoke(request, new Response()); + + then(request.getAttribute(TraceContext.class.getName())).isNotNull(); + thenSpanIsStartedAndStopped(); + } + + private Request request() { + Request request = new Request(new Connector()); + request.setCoyoteRequest(new org.apache.coyote.Request()); + return request; + } + +} + +class MyValve extends ValveBase { + + @Override + public void invoke(Request request, Response response) throws IOException, ServletException { + + } + +}