This is an example app where two Servlet (Java) services collaborate on an http request. Notably, timing of these requests are recorded into Zipkin, a distributed tracing system. This allows you to see the how long the whole operation took, as well how much time was spent in each service.
Here's an example of what it looks like:
This example was ported from https://github.com/openzipkin/sleuth-webmvc-example
Web requests are served by Spring MVC controllers, and tracing is automatically performed for you by Brave.
This example intentionally avoids advanced topics like async and messaging, eventhough Brave supports that, too. Once you get familiar with things, you can play with more interesting Brave instrumentation.
This example has two services: frontend and backend. They both report trace data to zipkin. To setup the demo, you need to start Frontend, Backend and Zipkin.
Once the services are started, open http://localhost:8081/
- This will call the backend (http://localhost:9000/api) and show the result, which defaults to a formatted date.
Next, you can view traces that went through the backend via http://localhost:9411/?serviceName=backend
- This is a locally run zipkin service which keeps traces in memory
In a separate tab or window, start each of brave.webmvc.Frontend and brave.webmvc.Backend:
# choose webmvc25 webmvc3 or webmvc4
$ cd webmvc4
$ mvn jetty:run -Pfrontend
$ mvn jetty:run -Pbackend
Next, run Zipkin, which stores and queries traces reported by the above services.
curl -sSL https://zipkin.io/quickstart.sh | bash -s
java -jar zipkin.jar
To show how wiring works, we have three copies of the same project
- WebMVC 2.5 - Spring XML on Servlet 2.5 container
- WebMVC 3 - Spring XML on Servlet 3 container
- WebMVC 4 - Spring Java Config on Servlet 3 container
There are some interesting details that apply to both
- If you pass the header
user-name
Brave will automatically propagate it to the backend!curl -s localhost:8081 -H'user-name: JC'
- The below pattern adds trace and span identifiers into log output
%d{ABSOLUTE} [%X{traceId}/%X{spanId}] %-5p [%t] %C{2} (%F:%L) - %m%n