Top Banner
Microservices tracing with Spring Cloud and Zipkin Marcin Grzejszczak Marcin Grzejszczak @mgrzejszczak, 24 June 2016
70

Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Apr 12, 2017

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Microservices tracing with Spring Cloud and Zipkin

Marcin Grzejszczak

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Page 2: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

About meDeveloper at Pivotal

Part of Spring Cloud Team

Working with OSS:● Accurest - Consumer Driven Contracts verifier for Java● JSON Assert - fluent JSON assertions● Spock Subjects Collaborators Extension● Gradle Test Profiler● Up To Date Gradle Plugin

TWITTER: @MGrzejszczakBLOG: http://TOOMUCHCODING.COM

Page 3: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Page 4: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

AgendaWhat is distributed tracing?

How to correlate logs with Spring Cloud Sleuth?

How to visualize latency with Spring Cloud Sleuth and Zipkin?

Page 5: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

An ordinary system...

Page 6: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

UI calls backend

UI -> BACKEND

Page 7: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Everything is awesome

CLICK 200

Page 8: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Until it’s not

CLICK 500

Page 9: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Page 10: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Time to debug

https://tonysbologna.files.wordpress.com/2015/09/mario-and-luigi.jpg?w=468&h=578&crop=1

Page 11: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

It doesn’t look like this

Page 12: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

More like this

Page 13: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

On which server / instance was the exception thrown?

Page 14: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

SSH and grep for ERROR to find it?

Page 15: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Distributed tracing - terminologySpan

Trace

Logs (annotations)

Tags (binary annotations)

Page 16: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Distributed tracing - terminologySpan

Trace

Logs (annotations)

Tags (binary annotations)

Page 17: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

SpanThe basic unit of work (e.g. sending RPC)

● Spans are started and stopped

● They keep track of their timing information

● Once you create a span, you must stop it at some point in the future

● Has a parent and can have multiple children

Page 18: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

TraceA set of spans forming a tree-like structure.

● For example, if you are running a book store then

○ Trace could be retriving a list of available books

○ Assuming that to retrive the books you have to send 3 requests to 3 services then you could have at least 3 spans (1 for each hop) forming 1 trace

Page 19: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

SERVICE 1

REQUEST

No Trace IdNo Span Id

RESPONSE

SERVICE 2

SERVICE 3

Trace Id = XSpan Id = A

Trace Id = XSpan Id = A

Trace Id = XSpan Id = A

REQUEST

RESPONSE

Trace Id = XSpan Id = BClient Send

Trace Id = XSpan Id = B

Client Received

Trace Id = XSpan Id = B

Server Received

Trace Id = XSpan Id = C

Trace Id = XSpan Id = BServer Sent

REQUEST

RESPONSE

Trace Id = XSpan Id = DClient Send

Trace Id = XSpan Id = D

Client Received

Trace Id = XSpan Id = D

Server Received

Trace Id = XSpan Id = E

Trace Id = XSpan Id = DServer Sent

Trace Id = XSpan Id = E

SERVICE 4

REQUEST

RESPONSE

Trace Id = XSpan Id = FClient Send

Trace Id = XSpan Id = F

Client Received

Trace Id = XSpan Id = F

Server Received

Trace Id = XSpan Id = G

Trace Id = XSpan Id = FServer Sent

Trace Id = XSpan Id = G

Trace Id = XSpan Id = C

Page 20: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Span Id = AParent Id = null

Span Id = BParent Id = A

Span Id = CParent Id = B

Span Id = DParent Id = C

Span Id = EParent Id = D

Span Id = FParent Id = C

Span Id = GParent Id = F

Page 21: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Is it that simple?

Page 22: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Is it that simple?How do you pass tracing information (incl. Trace ID) between:

● different libraries?

● thread pools?

● asynchronous communication?

● …?

Page 23: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

What if you forget about a thread pool?

SERVICE 1

REQUEST

NO TRACE

RESPONSE

SERVICE 2

SERVICE 3

A

A

A

REQUEST

RESPONSE

A

A

A B

A

REQUEST

RESPONSE

B

B

C C

C C

SERVICE 4

REQUEST

RESPONSE

B

B

D D

D DB

Page 24: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Log correlation with Spring Cloud SleuthWe take care of passing tracing information between threads / libraries / contexts for● Hystrix● RxJava● Rest Template● Feign● Messaging with Spring Integration● Zuul● ...

If you don’t do anything unexpected there’s nothing you need to do to make Sleuth work. Check the docs for more info.

Page 25: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Now let’s aggregate the logs!Instead of SSHing to the machines aggregate the logs!

● With Cloud Foundry’s (CF) Loggergator the logs from different instances are streamed into a single place

● You can harvest your logs with Logstash Forwarder / FileBeat

● You can use ELK stack to stream and visualize the logs

Page 26: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Spring Cloud Sleuth with Maven<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Brixton.SR1</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-sleuth</artifactId>

</dependency>

Page 27: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Spring Cloud Sleuth with Gradle

dependencies {

compile "org.springframework.cloud:spring-cloud-starter-sleuth"

}

dependencyManagement {

imports {

mavenBom "org.springframework.cloud:spring-cloud-dependencies:Brixton.SR1"

}

}

Page 28: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

SERVICE 1/start

REQUEST

RESPONSE

SERVICE 2

SERVICE 3

REQUEST

RESPONSE

REQUEST

RESPONSE

SERVICE 4

REQUEST

RESPONSE

“Hello from service3”

“Hello from service4”

“Hello from service2, response from service3 [Hello from service3] and from service4 [Hello from service4]”

Page 29: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

SERVICE 1/readtimeout

REQUEST

BOOM!

SERVICE 2

REQUEST

BOOM!

REQUEST

BOOM!

Page 30: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Log correlation with Spring Cloud SleuthDEMO

Page 31: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Page 32: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Page 33: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Page 34: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Great! We’ve found the exception!But meanwhile....

Page 35: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

The system is slow...

CLICK 200

Page 36: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

One of the services is slow?

Page 37: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Which one?How to measure that?

Page 38: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

● Client Send (CS) - The client has made a request

● Server Received (SR) - The server side got the request and will start processing

● Server Send (SS) - Annotated upon completion of request processing

● Client Received (CR) - The client has successfully received the response from the server side

Let’s log events!

Page 39: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

CS 0 ms SR 100 ms

SS 300 msCR 450 ms

Page 40: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

● The request started at T=0ms

● It took 450 ms for the client to receive a response

● Server side received the request at T=100 ms

● The request got processed on the server side in 200 ms

ConclusionsCS 0 ms SR 100 ms

SS 300 msCR 450 ms

Page 41: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Why is there a delay between sending and receiving messages?!!11!one!?!1!

ConclusionsCS 0 ms SR 100 ms

SS 300 msCR 450 ms

Page 42: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

https://blogs.oracle.com/jag/resource/Fallacies.html

Page 43: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Distributed tracing - terminologySpan

Trace

Logs (annotations)

Tags (binary annotations)

Page 44: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

LogsRepresents an event in time associated with a span

● Every span has zero or more logs

● Each log is a timestamped event name

● Event should be the stable name of some notable moment in the lifetime of a span

● For instance, a span representing a browser page load might add an event for each of the Performance.timing moments (check https://developer.mozilla.org/en-US/docs/Web/API/PerformanceTiming)

Page 45: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Page 46: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Main logs

● Client Send (CS)○ The client has made a request - the span was started

● Server Received (SR)○ The server side got the request and will start processing it

○ SR timestamp - CS timestamp = NETWORK LATENCY

CS 0 ms SR 100 ms

Page 47: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Main logs

● Server Send (SS)○ Annotated upon completion of request processing

○ SS timestamp - SR timestamp = SERVER SIDE PROCESSING TIME

● Client Received (CR)○ The client has successfully received the response from the server side

○ CR timestamp - CS timestamp = TIME NEEDED TO RECEIVE RESPONSE

○ CR timestamp - SS timestamp = NETWORK LATENCY

CS 0 ms SR 100 ms

SS 300 msCR 450 ms

Page 48: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Key-value pair

● Every span may have zero or more key/value Tags

● They do not have timestamps and simply annotate the spans.

● Example of default tags in Sleuth○ message/payload-size○ http.method○ commandKey for Hystrix

Tag

Page 49: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

How to visualise latency in a distributed system?

Page 50: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

● Zipkin is a distributed tracing system

● It runs as a separate process (you can run it as a Spring Boot application)

● It helps gather timing data needed to troubleshoot latency problems in microservice architectures

● The front end is a "waterfall" style graph of service calls showing call durations as horizontal bars

The answer is: Zipkin

Page 51: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

How does Zipkin work?

SPANS SENT TO COLLECTORS

SPANS SENT TO COLLECTORS

STORE IN DB

APP

APP

UI QUERIES FOR TRACE INFO VIA API

Page 52: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Spring Cloud Sleuth and Zipkin integration● We take care of passing tracing information between threads / libraries /

contexts

● Upon closing of a Span we will send it to Zipkin○ either via HTTP (spring-cloud-sleuth-zipkin)○ or via Spring Cloud Stream (spring-cloud-sleuth-stream)

● You can run Zipkin Spring Cloud Stream Collector as a Spring Boot app (spring-cloud-sleuth-zipkin-stream)○ you can add the dependency to Zipkin UI!

Page 53: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Spring Cloud Sleuth Zipkin with Maven<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Brixton.SR1</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-zipkin</artifactId>

</dependency>

Page 54: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Spring Cloud Sleuth Zipkin with Gradle

dependencies {

compile "org.springframework.cloud:spring-cloud-starter-zipkin"

}

dependencyManagement {

imports {

mavenBom "org.springframework.cloud:spring-cloud-dependencies:Brixton.SR1"

}

}

Page 55: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

HOLD IT!● If I have billion services that emit gazillion spans - won’t I kill Zipkin?

Page 56: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Sampling to the rescue!● By default Spring Cloud Sleuth sends only 10% of requests to Zipkin

● You can change that by changing the property

spring.sleuth.sampler.percentage (for 100% pass 1.0)

● Or register a custom org.springframework.cloud.sleuth.Sampler

implementation

Page 57: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

SERVICE 1/start

REQUEST

RESPONSE

SERVICE 2/foo

SERVICE 3/bar

REQUEST

RESPONSE

REQUEST

RESPONSE

SERVICE 4/baz

REQUEST

RESPONSE

DEVOXXSERVICE/devoxx

REQUEST

RESPONSE

Page 58: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

DEMO

Page 59: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Traced call

Page 60: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Traced call

TOTAL DURATION

EN

D

STA

RT

Page 61: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Traced call

CLI

EN

TS

EN

T

CLI

EN

TR

EC

EIV

ED

SERVICE 2CLIENT

Page 62: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Traced call

SE

RV

ER

RE

CE

IVE

D

SE

RV

ER

SE

NT

SERVICE 4SERVER

Page 63: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Traced call

LATE

NC

Y

SE

RV

ER

RE

CE

IVE

D

CLI

EN

TS

EN

T

Page 64: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Traced call

SE

RV

ER

RE

CE

IVE

DC

LIE

NT

SE

NT

DIFF IS LATENCY

Page 65: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Zipkin for Brewery● A test app for Spring Cloud end to end tests

● Source code: https://github.com/spring-cloud-samples/brewery

● Around 10 applications involved

Page 66: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Page 67: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Page 68: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Summary● Log correlation allows you to match logs for a given trace

● Distributed tracing allows you to quickly see latency issues in your system

● Zipkin is a great tool to visualize the latency graph and system dependencies

● Spring Cloud Sleuth integrates with Zipkin and grants you log correlation

Page 69: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

Page 70: Microservices Tracing with Spring Cloud and Zipkin (devoxx)

Marcin Grzejszczak @mgrzejszczak, 24 June 2016

THANK YOU● https://github.com/marcingrzejszczak/vagrant-elk-box/tree/presentation - code for this presentation (clone

and run getReadyForConference.sh - NOTE: you need Vagrant!)

● https://github.com/spring-cloud/spring-cloud-sleuth - Spring Cloud Sleuth repository

● http://cloud.spring.io/spring-cloud-sleuth/spring-cloud-sleuth.html - Sleuth’s documentation

● http://toomuchcoding.com/blog/2016/03/25/spring-cloud-sleuth-rc1-deployed/ - article about RC1 release

● https://github.com/openzipkin/zipkin-java - Repo with Spring Boot Zipkin server

● http://docssleuth-service1.cfapps.io/start - The service1 app from this presentation deployed to Pivotal Cloud

Foundry - point of entry to the app

● http://docssleuth-zipkin-server.cfapps.io/ - Zipkin deployed to Pivotal Cloud Foundry

● http://docsbrewing-zipkin-server.cfapps.io - Zipkin deployed to PCF for Brewery Sample app