What is Sleuth in Spring Boot and how to use it

Spring Boot is a very popular Java web development framework that provides many handy features, one of which is Sleuth. Sleuth is a distributed tracing system for tracing requests and operations in applications. In this article, we'll explore what Sleuth is in Spring Boot and how you can use it to trace requests and operations in your application.

insert image description here

What is Sleuth?

Sleuth is a distributed tracing system for tracing requests and operations in applications. It can help us understand the structure and performance of the application and locate the root cause of the problem. Sleuth can be used with distributed tracing systems such as Zipkin to provide more comprehensive application tracing and analysis capabilities.

Sleuth provides a convenient way to integrate with Spring Boot. It makes it easy to trace requests and operations in your application, and logs trace information to log files. This allows us to more easily understand the structure and performance of the application and quickly locate the root cause of problems.

Sleuth's core concepts

Before using Sleuth, we need to understand some core concepts:

  • Trace: A Trace represents a complete call chain of a request or operation, starting from the client initiating the request and ending with the server responding to the request.
  • Span (span): A Span represents a part of a request or operation, which contains some useful information, such as start time, end time, operation name, etc.
  • Trace ID (tracking ID): A Trace ID is a unique identifier, which is used to associate a group of Spans together to form a complete Trace.
  • Span ID (span ID): A Span ID is a unique identifier used to identify a Span.

In Sleuth, each request or operation generates a Trace, and each Trace contains multiple Spans. Each Span contains a unique Span ID and is associated with a Trace ID. Through Trace ID and Span ID, we can associate multiple Spans together to form a complete Trace.

Use Sleuth

To use Sleuth, we need to add the Sleuth dependency to the project. In a Maven project, the following dependencies can be added to the pom.xml file:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

In a Gradle project, the following dependencies can be added to the build.gradle file:

implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'

Once the dependency is added, Sleuth is automatically enabled and starts tracking requests and operations in the application. Sleuth will automatically generate a Trace for each request or operation, and generate a unique Span ID for each Span in each Trace.

By default, Sleuth will add Trace ID and Span ID to logs so that we can easily trace requests and actions in our application. For example, here is a log containing Trace ID and Span ID:

2021-09-01 12:00:00.000 [my-service,0123456789abcdef,0123456789abcdef,true] TRACE [MyController] - Handling request

In the example above, the log contains a Trace ID (0123456789abcdef) and a Span ID (0123456789abcdef), which are each associated with a my-serviceservice named .

Custom Sleuth configuration

When using Sleuth, we can customize the configuration through configuration files or programmatically. The following are some commonly used custom configuration methods:

Configure the names of Trace and Span

By default, Sleuth will use the application's name and a randomly generated ID for each Trace and Span respectively. If you need to customize the names of Trace and Span, you can add the following configuration to the application.properties (or application.yaml) file:

spring.sleuth.sampler.probability=1.0
spring.application.name=my-custom-app-name
spring.sleuth.span-name-regex=(?<controller>MyController)

The above configuration will set the name of the Trace to my-custom-app-name, and all MyControllerspans containing the MyController. Also, spring.sleuth.sampler.probability=1.0make sure Sleuth keeps track of all requests and operations.

Configure Sleuth's Sampler

In an actual production environment, we may need to sample requests and operations to avoid excessive trace data generated by Sleuth. In Sleuth, sampling is achieved through Sampler. By default, Sleuth uses a random sampler that samples 10% of requests.

If you need a custom sampler, you can add the following configuration to the configuration file:

spring.sleuth.sampler.type=RATE_LIMITED
spring.sleuth.sampler.rate=0.5

The configuration above will set the sampler to RATE_LIMITED which will limit the sample rate based on the request rate. Specifically, it will sample 50% of the requests.

Use MDC (Mapped Diagnostic Context)

In an actual production environment, we may need to pass Trace and Span information to logging frameworks such as Logback or Log4j2. In order to achieve this, Sleuth provides a MDC (Mapped Diagnostic Context) implementation, which can add Trace and Span information to the log.

To use MDC, we need to add the following configuration in the application.properties (or application.yaml) file:

logging.pattern.level=%X{traceId} %X{spanId} %5p [${spring.zipkin.service.name:-},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}] [%t] %logger{5} - %msg%n

The above configuration will add Trace and Span information in the log mode. This way, we can easily trace requests and operations in the application and add trace information to the logs.

Summarize

This article introduces Sleuth in Spring Boot, a distributed tracing system for tracing requests and operations in your application. We learned about the core concepts of Sleuth, including Trace, Span, Trace ID, and Span ID, and explored how to use Sleuth.

In the actual production environment, we can use Sleuth through custom configuration. For example, we can configure Trace and Span names, sampler types and rates, and use MDC to add Trace and Span information to logs. These features can help us more easily understand the structure and performance of the application, and quickly locate the root cause of the problem.

Guess you like

Origin blog.csdn.net/2302_77835532/article/details/131552092