Search

Sunday, March 18, 2018

Distributed tracing with Spring Cloud Sleuth

Spring Cloud Sleuth

How to use it

For simple use-case all we need to do is to have Sleuth as dependency in Spring boot project
Maven
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-sleuth</artifactId>
    </dependency>
</dependencies>
Or 
Gradle
dependencyManagement {
     imports {
          mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Edgware.RELEASE'
     }
}
dependencies {
    compile 'org.springframework.cloud:spring-cloud-starter-sleuth'
}

Log format

Since we only output the log to the console then default Spring boot log format and configuration is good enough.
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
    <property name="CONSOLE_LOG_PATTERN"
              value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
With Sleuth the log looks like bellow
2017-12-01 12:06:42.651  INFO [jpa.demo,0000001234567879,0000000123456789,true34765 --- [nio-8080-exec-1] com.springjpa.controller.WebController   : Done
[jpa.demo,0000001234567879,0000000123456789,true] part is added by Sleuth, the format is [appname,traceId,spanId,exportable] 
  • spanId - the id of a specific operation that took place
  • appname - the name of the application that logged the span
  • traceId - the id of the latency graph that contains the span
  • exportable - whether the log should be exported to Zipkin or not. When would you like the span not to be exportable? In the case in which you want to wrap some operation in a Span and have it written to the logs only.

Within Spring Boot projects, adding the Spring Cloud Sleuth library to the classpath will automatically add 2 HTTP headers to all calls:
  • X-B3-Traceid Shared by all HTTP calls of a single transaction i.e. the wished-for transaction identifier
  • X-B3-Spanid Identifies the work of a single microservice during a transaction
Non-Spring Boot app may need to handle these 2 headers itself.

No comments:

Post a Comment

Business blockchain use cases