• This repository has been archived on 21/Nov/2020
  • Stars
    star
    176
  • Rank 216,987 (Top 5 %)
  • Language
    Shell
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

See how much time Spring Boot services spend on an http request.

This is replaced by https://github.com/openzipkin/brave-example

Basic example showing distributed tracing across Spring Boot apps

This is an example app where two Spring Boot (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 Zipkin Screenshot

This example was initially made for a Distributed Tracing Webinar on June 30th, 2016. There's probably room to enroll if it hasn't completed, yet, and you are interested in the general topic.

Implementation Overview

Web requests are served by Spring MVC controllers, and tracing is automatically performed for you by Spring Cloud Sleuth.

This example intentionally avoids advanced topics like async and load balancing, even though Spring Cloud Sleuth supports that, too. Once you get familiar with things, you can play with more interesting Spring Cloud components.

Running the example

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/

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

Starting the Services

In a separate tab or window, start each of sleuth.webmvc.Frontend and sleuth.webmvc.Backend:

$ ./mvnw compile exec:java -Dexec.mainClass=sleuth.webmvc.Backend
$ ./mvnw compile exec:java -Dexec.mainClass=sleuth.webmvc.Frontend

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

Configuration tips

  • The service name in the Zipkin UI defaults to the application name
    • spring.application.name=frontend
  • All incoming requests are sampled and that decision is honored downstream.
    • spring.sleuth.sampler.probability=1.0
  • The below pattern adds trace and span identifiers into log output
    • logging.pattern.level=%d{ABSOLUTE} [%X{traceId}/%X{spanId}] %-5p [%t] %C{2} - %m%n
  • If you pass the header user_name Brave will automatically propagate it to the backend!
    • curl -s localhost:8081 -H'user_name: JC'

Going further

A distributed trace will only include connections that are configured (instrumented). You may be using some libraries that aren't automatically configured.

Here are a few small examples that showcase how to stitch-in commonly requested features.

Spring WebFlux Tracing

git checkout add-webflux-tracing

This changes the example to serve requests with reactive Mono controllers, using WebClient instead of RestTemplate to make the call from the frontend to the backend.

Apache Http Client Tracing

git checkout add-apachehc-tracing

This changes the example to use Apache HttpClient instead of RestTemplate to make the call from the frontend to the backend.

https://github.com/openzipkin/brave/tree/master/instrumentation/httpclient

MySQL Tracing

git checkout add-mysql-tracing

This changes the example to read the timestamp from MySQL instead of the Spring Boot Process. It adds a brave tracing interceptor to add details to the existing trace.

https://github.com/openzipkin/brave/tree/master/instrumentation/mysql8

RabbitMQ Tracing

git checkout add-rabbit-tracing

This changes the example to invoke the backend with RabbitMQ instead of WebMVC. Sleuth automatically configures Brave's spring-rabbit to add trace details.

https://github.com/openzipkin/brave/tree/master/instrumentation/spring-rabbit

Kafka Tracing

git checkout add-kafka-tracing

This changes the example to invoke the backend with Kafka instead of WebMVC. Sleuth automatically configures Brave's kafka-clients instrumentation when spring-kafka is present.

https://github.com/openzipkin/brave/tree/master/instrumentation/kafka-clients https://github.com/spring-cloud/spring-cloud-sleuth/blob/v2.1.1.RELEASE/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/TraceMessagingAutoConfiguration.java#L110

JMS Tracing

This changes the example to invoke the backend with ActiveMQ instead of WebMVC. Sleuth automatically configures Spring JmsTemplate and JmsListener to add trace details.

Dubbo Tracing

git checkout add-dubbo-tracing

This changes the example to call a Dubbo backend instead of WebMVC. It uses Brave's RPC filter to add details to the existing trace.

https://github.com/openzipkin/brave/tree/master/instrumentation/dubbo-rpc

Java Flight Recorder

git checkout add-jfr-context

This changes the example to add trace IDs to Java Flight recorder "Zipkin/Scope" events.

https://github.com/openzipkin/brave/tree/master/context/jfr

Customizing with OpenTracing

git checkout add-opentracing

This changes the example to add a lookup tag using the default SpanCustomizer and OpenTracing's Tracer api. Users can choose which api makes most sense for them to expose to business code.

Under the covers, this uses the brave-opentracing bridge:

https://github.com/openzipkin-contrib/brave-opentracing

Docker

A docker image containing the example can be built using

$ docker build -t openzipkin/example-sleuth-webmvc -f docker/Dockerfile .

The backend can be started with a command similar to

$ docker run -it --rm -p 9000:9000 openzipkin/example-sleuth-webmvc backend

The frontend can be started with a command similar to

$ docker run -it --rm -p 8081:8081 openzipkin/example-sleuth-webmvc frontend

Need something else not here?

Sleuth layers on the Brave project, so can re-use any code that works with brave. It can also use transports besides http to send data to a Zipkin compatible service.

Contact us on gitter if you need more help!

More Repositories

1

docker-zipkin

Docker images for OpenZipkin
Shell
689
star
2

zipkin-sparkstreaming

A streaming alternative to Zipkin's collector
Java
70
star
3

zipkin-ui

Experimental new UI for Zipkin
TypeScript
58
star
4

zipkin-csharp

A minimalistic .NET client library for Twitter Zipkin tracing.
C#
29
star
5

docker-zipkin-dependencies

Docker build for zipkin-dependencies.
Shell
22
star
6

zipkin-armeria-example

Example project that shows how to use armeria and zipkin together
Java
13
star
7

zipkin-azure

Reporters and collectors for use in Azure
Java
11
star
8

zipkin-finagle-example

Example project that shows how to use finagle and zipkin together
Java
10
star
9

zipkin-finatra-example

Example project that shows how to use finatra and zipkin together Edit
Scala
9
star
10

docker-zipkin-gcp

Docker build for zipkin-gcp
Shell
8
star
11

docker-zipkin-aws

Docker build for zipkin-aws
Shell
8
star
12

docker-zipkin-azure

Docker build for zipkin-azure
Shell
6
star
13

flume-zipkin-collector-sink

Flume sink that supports sending Zipkin spans to the Zipkin Collector.
Java
5
star
14

zipkin-firefox

JavaScript
4
star
15

zipkin-kubernetes

Kubernetes controller and service definitions for Zipkin
4
star
16

centralsync-maven-plugin

Maven plugin that syncs a Bintray project to Maven Central
Java
3
star
17

wingtips-sparkjava-example

See how much time Spark Web Framework services spend on an http request.
Java
3
star
18

apache-release-verification

Python
3
star
19

zipkin-cpp-opentracing

Zipkin's C++ tracer library
Makefile
2
star
20

zipkin-classic

Source of the classic UI (pre Zipkin Lens)
JavaScript
2
star
21

brave-grpc-example

1
star
22

docker-zipkin-sparkstreaming

Docker build for zipkin-zipkin-sparkstreaming
1
star
23

zipkin-sparkstreaming-example

Example of how to make a custom adjuster for zipkin-sparkstreaming
Java
1
star
24

docker-zipkin-ui

Docker image for the experimental Zipkin UI
1
star