-
Notifications
You must be signed in to change notification settings - Fork 714
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separates MVC AsyncHandler as it requires Spring 3+; lowers java levels
This separates out the async MVC functionality and tests the synchronous part works with Spring 2.5. This also lowers the minimum java level of instrumentation likely to be used in Spring 2.5 applications.
- Loading branch information
Adrian Cole
committed
Dec 12, 2017
1 parent
5c51589
commit 78fe610
Showing
13 changed files
with
370 additions
and
13 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
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
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
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
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
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,2 @@ | ||
# nozipkin1 | ||
This tests that Tracing can be initialized without a runtime dependency on `io.zipkin.java:zipkin` |
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,74 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>@project.groupId@</groupId> | ||
<artifactId>spring-webmvc-spring25</artifactId> | ||
<version>@project.version@</version> | ||
<name>spring-webmvc-spring25</name> | ||
|
||
<properties> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<version>2.5</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-webmvc</artifactId> | ||
<version>2.5.6</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.eclipse.jetty</groupId> | ||
<artifactId>jetty-servlet</artifactId> | ||
<version>@jetty-servlet25.version@</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>brave-instrumentation-spring-webmvc</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>brave-instrumentation-http-tests</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>@junit.version@</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>@assertj.version@</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-failsafe-plugin</artifactId> | ||
<version>@maven-failsafe-plugin.version@</version> | ||
<configuration> | ||
<failIfNoTests>true</failIfNoTests> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
90 changes: 90 additions & 0 deletions
90
...bmvc/src/it/spring25/src/test/java/brave/spring/webmvc25/ITTracingHandlerInterceptor.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,90 @@ | ||
package brave.spring.webmvc25; | ||
|
||
import brave.Tracer; | ||
import brave.http.HttpTracing; | ||
import brave.http.ITServletContainer; | ||
import brave.spring.webmvc.TracingHandlerInterceptor; | ||
import java.io.IOException; | ||
import javax.servlet.http.HttpServletResponse; | ||
import org.eclipse.jetty.servlet.ServletContextHandler; | ||
import org.eclipse.jetty.servlet.ServletHolder; | ||
import org.junit.Test; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.context.WebApplicationContext; | ||
import org.springframework.web.context.support.StaticWebApplicationContext; | ||
import org.springframework.web.servlet.DispatcherServlet; | ||
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter; | ||
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping; | ||
|
||
import static org.springframework.web.servlet.DispatcherServlet.HANDLER_ADAPTER_BEAN_NAME; | ||
import static org.springframework.web.servlet.DispatcherServlet.HANDLER_MAPPING_BEAN_NAME; | ||
|
||
public class ITTracingHandlerInterceptor extends ITServletContainer { | ||
|
||
@Controller | ||
public static class TestController { | ||
final Tracer tracer; | ||
|
||
@Autowired public TestController(HttpTracing httpTracing) { | ||
this.tracer = httpTracing.tracing().tracer(); | ||
} | ||
|
||
@RequestMapping(value = "/foo") | ||
public void foo(HttpServletResponse response) throws IOException { | ||
response.getWriter().write("foo"); | ||
} | ||
|
||
@RequestMapping(value = "/badrequest") | ||
public void badrequest(HttpServletResponse response) throws IOException { | ||
response.sendError(400); | ||
response.flushBuffer(); | ||
} | ||
|
||
@RequestMapping(value = "/child") | ||
public void child() { | ||
tracer.nextSpan().name("child").start().finish(); | ||
} | ||
|
||
@RequestMapping(value = "/exception") | ||
public void disconnect() throws IOException { | ||
throw new IOException(); | ||
} | ||
} | ||
|
||
// TODO: investigate why the status isn't being added as a tag | ||
@Override @Test(expected = AssertionError.class) | ||
public void addsStatusCode_badRequest() throws Exception { | ||
super.addsStatusCode_badRequest(); | ||
} | ||
|
||
@Override public void init(ServletContextHandler handler) { | ||
StaticWebApplicationContext wac = new StaticWebApplicationContext(); | ||
wac.getBeanFactory() | ||
.registerSingleton("testController", new TestController(httpTracing)); // the test resource | ||
|
||
DefaultAnnotationHandlerMapping mapping = new DefaultAnnotationHandlerMapping(); | ||
mapping.setInterceptors(new Object[] {TracingHandlerInterceptor.create(httpTracing)}); | ||
mapping.setApplicationContext(wac); | ||
|
||
wac.getBeanFactory().registerSingleton(HANDLER_MAPPING_BEAN_NAME, mapping); | ||
wac.getBeanFactory().registerSingleton(HANDLER_ADAPTER_BEAN_NAME, new AnnotationMethodHandlerAdapter()); | ||
|
||
handler.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); | ||
handler.addServlet(new ServletHolder(new DispatcherServlet() { | ||
{ | ||
wac.refresh(); | ||
setDetectAllHandlerMappings(false); | ||
setDetectAllHandlerAdapters(false); | ||
setPublishEvents(false); | ||
} | ||
|
||
@Override protected WebApplicationContext initWebApplicationContext() throws BeansException { | ||
onRefresh(wac); | ||
return wac; | ||
} | ||
}), "/*"); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...ation/spring-webmvc/src/main/java/brave/spring/webmvc/TracingAsyncHandlerInterceptor.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,42 @@ | ||
package brave.spring.webmvc; | ||
|
||
import brave.Tracing; | ||
import brave.http.HttpTracing; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.servlet.AsyncHandlerInterceptor; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | ||
|
||
/** | ||
* Tracing interceptor for Spring Web MVC, which can be used as both an {@link | ||
* AsyncHandlerInterceptor} or a normal {@link HandlerInterceptor}. | ||
*/ | ||
public final class TracingAsyncHandlerInterceptor extends HandlerInterceptorAdapter { | ||
public static AsyncHandlerInterceptor create(Tracing tracing) { | ||
return new TracingAsyncHandlerInterceptor(HttpTracing.create(tracing)); | ||
} | ||
|
||
public static AsyncHandlerInterceptor create(HttpTracing httpTracing) { | ||
return new TracingAsyncHandlerInterceptor(httpTracing); | ||
} | ||
|
||
final HandlerInterceptor delegate; | ||
|
||
@Autowired TracingAsyncHandlerInterceptor(HttpTracing httpTracing) { // internal | ||
delegate = TracingHandlerInterceptor.create(httpTracing); | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) | ||
throws Exception { | ||
return delegate.preHandle(request, response, o); | ||
} | ||
|
||
@Override | ||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, | ||
Object o, Exception ex) throws Exception { | ||
delegate.afterCompletion(request, response, o, ex); | ||
} | ||
} |
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
Oops, something went wrong.