Adding Sleuth to Your Classpath:
This section details how to add Sleuth to your class path for both Maven and Gradle
Maven
To add Sleuth to your classpath with Maven, add the following elements
to your pom.xml file:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth</artifactId>
<version>${spring-cloud-sleuth.version}</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>
Gradle
To add Sleuth to your classpath with Gradle, add the following
to your build.gradle file:
buildscript {
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE"
}
}
apply plugin: "io.spring.dependency-management"
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-sleuth:${springCloudSleuthVersion}"
}
}
dependencies {
compile 'org.springframework.cloud:spring-cloud-starter-sleuth'
}
As long as Spring Cloud Sleuth is on the classpath, any Spring Boot application can generate trace data. The following example shows how to do so:
@SpringBootApplication
@RestController
public class Application {
private static Logger log = LoggerFactory.getLogger(DemoController.class);
@RequestMapping("/")
public String home() {
log.info("Handling home");
return "Hello World";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Now you can run this application and visit the home page. In the logs, you can see
traceId and spanId populated. If this application calls out to another one (for
example, with RestTemplate), it sends the trace data in headers, and, if the receiver is
another Sleuth application, you can see the trace continue there.
instead of logging the request in the handler explicitly, you could set
logging.level.org.springframework.web.servlet.DispatcherServlet=DEBUG
|
If you use Zipkin, you can configure the probability of spans being exported by
setting (for 2.0.x) spring.sleuth.sampler.probability or (up till 2.0.x)
spring.sleuth.sampler.percentage (default: 0.1, which is 10 percent). Otherwise, you
might think that Sleuth is not working because it omits some spans.
|
Set spring.application.name=bar (for instance) to see the service name as
well as the trace and span IDs.
|