-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Spring Boot 1.5 (webmvc4) example (#25)
- Loading branch information
1 parent
0541db7
commit 1ed6fa6
Showing
8 changed files
with
252 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
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,18 @@ | ||
## WebMVC 4 Boot Example | ||
|
||
Instead of servlet, this uses Spring Boot 1.5 to create a self-contained | ||
application that runs Spring WebMVC 4 controllers. | ||
|
||
* brave.webmvc.Frontend and Backend : Rest controllers with no tracing configuration | ||
* brave.webmvc.TracingConfiguration : This adds tracing by configuring the tracer, server and client tracing interceptors. | ||
|
||
`TracingConfiguration` is automatically loaded due to `META-INF/spring.factories` | ||
This allows the `Frontend` and `Backend` controllers to have no tracing | ||
code. Inside the tracing configuration, you'll notice the rest template | ||
is setup via a `RestTemplateCustomizer`, which ensures application-level | ||
interceptors are not affected. Also, you'll notice layered tracing for | ||
server requests. First, `TracingFilter` creates a span, then later, | ||
`SpanCustomizingAsyncHandlerInterceptor` adds MVC tags to it. | ||
|
||
*Note* This only lightly configures tracing. When doing anything serious, | ||
consider [Spring Cloud Sleuth](https://github.com/spring-cloud/spring-cloud-sleuth) instead. |
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,69 @@ | ||
<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>io.zipkin.brave</groupId> | ||
<artifactId>brave-webmvc4-boot-example</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>brave-webmvc4-boot-example</name> | ||
<description>Example using Brave to trace RPCs from Spring Web MVC</description> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.7</maven.compiler.source> | ||
<maven.compiler.target>1.7</maven.compiler.target> | ||
|
||
<spring-boot.version>1.5.14.RELEASE</spring-boot.version> | ||
<brave.version>5.1.3</brave.version> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-dependencies</artifactId> | ||
<version>${spring-boot.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.zipkin.brave</groupId> | ||
<artifactId>brave-bom</artifactId> | ||
<version>${brave.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.zipkin.brave</groupId> | ||
<artifactId>brave</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.zipkin.reporter2</groupId> | ||
<artifactId>zipkin-sender-okhttp3</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.zipkin.brave</groupId> | ||
<artifactId>brave-context-slf4j</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.zipkin.brave</groupId> | ||
<artifactId>brave-instrumentation-spring-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.zipkin.brave</groupId> | ||
<artifactId>brave-instrumentation-spring-webmvc</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
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,28 @@ | ||
package brave.webmvc; | ||
|
||
import java.util.Date; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@EnableAutoConfiguration | ||
@RestController | ||
public class Backend { | ||
|
||
@RequestMapping("/api") | ||
public String printDate(@RequestHeader(name = "user-name", required = false) String username) { | ||
if (username != null) { | ||
return new Date().toString() + " " + username; | ||
} | ||
return new Date().toString(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Backend.class, | ||
"--spring.application.name=backend", | ||
"--server.port=9000" | ||
); | ||
} | ||
} |
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,33 @@ | ||
package brave.webmvc; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.web.bind.annotation.CrossOrigin; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@EnableAutoConfiguration | ||
@RestController | ||
@CrossOrigin // So that javascript can be hosted elsewhere | ||
public class Frontend { | ||
|
||
@Autowired RestTemplate restTemplate; | ||
|
||
@RequestMapping("/") public String callBackend() { | ||
return restTemplate.getForObject("http://localhost:9000/api", String.class); | ||
} | ||
|
||
@Bean RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Frontend.class, | ||
"--spring.application.name=frontend", | ||
"--server.port=8081" | ||
); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
webmvc4-boot/src/main/java/brave/webmvc/TracingConfiguration.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,86 @@ | ||
package brave.webmvc; | ||
|
||
import brave.Tracing; | ||
import brave.context.slf4j.MDCCurrentTraceContext; | ||
import brave.http.HttpTracing; | ||
import brave.propagation.B3Propagation; | ||
import brave.propagation.ExtraFieldPropagation; | ||
import brave.servlet.TracingFilter; | ||
import brave.spring.web.TracingClientHttpRequestInterceptor; | ||
import brave.spring.webmvc.SpanCustomizingAsyncHandlerInterceptor; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import javax.servlet.Filter; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.web.client.RestTemplateCustomizer; | ||
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.http.client.ClientHttpRequestInterceptor; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | ||
import zipkin2.Span; | ||
import zipkin2.reporter.AsyncReporter; | ||
import zipkin2.reporter.Sender; | ||
import zipkin2.reporter.okhttp3.OkHttpSender; | ||
|
||
/** | ||
* This adds tracing configuration to any web mvc controllers or rest template clients. | ||
*/ | ||
@Configuration | ||
// Importing a class is effectively the same as declaring bean methods | ||
@Import(SpanCustomizingAsyncHandlerInterceptor.class) | ||
public class TracingConfiguration extends WebMvcConfigurerAdapter { | ||
|
||
/** Configuration for how to send spans to Zipkin */ | ||
@Bean Sender sender() { | ||
return OkHttpSender.create("http://127.0.0.1:9411/api/v2/spans"); | ||
} | ||
|
||
/** Configuration for how to buffer spans into messages for Zipkin */ | ||
@Bean AsyncReporter<Span> spanReporter() { | ||
return AsyncReporter.create(sender()); | ||
} | ||
|
||
/** Controls aspects of tracing such as the name that shows up in the UI */ | ||
@Bean Tracing tracing(@Value("${spring.application.name}") String serviceName) { | ||
return Tracing.newBuilder() | ||
.localServiceName(serviceName) | ||
.propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "user-name")) | ||
.currentTraceContext(MDCCurrentTraceContext.create()) // puts trace IDs into logs | ||
.spanReporter(spanReporter()).build(); | ||
} | ||
|
||
/** decides how to name and tag spans. By default they are named the same as the http method. */ | ||
@Bean HttpTracing httpTracing(Tracing tracing) { | ||
return HttpTracing.create(tracing); | ||
} | ||
|
||
/** Creates client spans for http requests */ | ||
@Bean @Order(Ordered.HIGHEST_PRECEDENCE) | ||
RestTemplateCustomizer tracingRestTemplateCustomizer(final HttpTracing httpTracing) { | ||
return new RestTemplateCustomizer() { | ||
@Override public void customize(RestTemplate restTemplate) { | ||
List<ClientHttpRequestInterceptor> interceptors = | ||
new ArrayList<>(restTemplate.getInterceptors()); | ||
interceptors.add(0, TracingClientHttpRequestInterceptor.create(httpTracing)); | ||
} | ||
}; | ||
} | ||
|
||
/** Creates server spans for http requests */ | ||
@Bean Filter tracingFilter(HttpTracing httpTracing) { | ||
return TracingFilter.create(httpTracing); | ||
} | ||
|
||
@Autowired SpanCustomizingAsyncHandlerInterceptor webMvcTracingCustomizer; | ||
|
||
/** Decorates server spans with application-defined web tags */ | ||
@Override public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(webMvcTracingCustomizer); | ||
} | ||
} |
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 @@ | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | ||
brave.webmvc.TracingConfiguration |
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,5 @@ | ||
# spring.application.name and server.port are set in the main methods, | ||
# so not done here | ||
logging.level.org.springframework.web=DEBUG | ||
# Adds trace and span IDs to logs (when a trace is in progress) | ||
logging.pattern.level=%d{ABSOLUTE} [%X{traceId}/%X{spanId}] %-5p [%t] %C{2} - %m%n |