22 Aralık 2019 Pazar

SpringCloud Sleuth - Kullanmayın

Giriş
SpringCloud Sleuth eski bir  proje. Artık kullanmaya gerek yok. Açıklaması şöyle
Spring Cloud Sleuth is a Spring Cloud project that instruments various libraries with an abstraction over Tracers. Its next release will be in version 3.1.0 and it will be the last feature release of that project. For Spring 6 and Spring Boot 3 there will be no Sleuth compatible version.
Açıklaması şöyle
Spring Cloud Sleuth has been discontinued in Spring Boot 3.x, and it can only be used until the end of Spring Boot 2. To send your traces to Jaeger, you need to migrate to the Micrometer library.
Yani SpringCloud Sleuth  + Zipkin yerine artık Micrometer + Jaeger kullanılıyor

SpringCloud Sleuth - Zipkin İle Kullanılabilir
Açıklaması şöyle. Zipkin yazısına bakabilirsiniz
Spring Cloud Sleuth propagates headers compatible with Zipkin - a popular tool for distributed tracing. Its main features are:

- It adds trace (correlating requests) and span IDs to the Slf4J MDC.
- It records timing information to aid in latency analysis.
- It modifies a pattern of log entry to add some information like additional MDC fields.
- It provides integration with other Spring components like OpenFeign, RestTemplate or Spring Cloud Netflix Zuul.

Sleuth ve Loglama Kütüphanesi
Açıklaması şöyle. Yani Sleuth logback vs. ile kullanılabilir.
This integrates well with libraries like logback, slf4j to add identifiers so that we can trace and diagnose issues using logs.
Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
Eğer trace için Zipkin kullanılıyorsak ayrıca Zipkin dependency eklemeye gerek yoktur. Şöyle yaparız
<!--
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
-->
<!--Spring Sleuth3.0 removed spring-cloud-starter-zipkin:
https://docs.spring.io/spring-cloud-sleuth/docs/3.0.0-M3/reference/html /#sleuth-with-zipkin-via-http 
Use the following dependency instead
-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
Gradle
Şöyle yaparız
ext { set('springCloudVersion', "2021.0.0") } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.cloud:spring-cloud-starter-sleuth' implementation 'org.springframework.cloud:spring-cloud-sleuth-zipkin' implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' compileOnly 'org.projectlombok:lombok:1.18.22' annotationProcessor 'org.projectlombok:lombok:1.18.22' testCompileOnly 'org.projectlombok:lombok:1.18.22' testAnnotationProcessor 'org.projectlombok:lombok:1.18.22' } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
Sleuth Çıktısı
Sleuth çıktısında şu alanlar vardır
[application name, traceId, spanId, export]
Şeklen şöyle

Alanların açıklaması şöyle
Application Name is the name we set in the application.properties file in the previous step. This is crucial to effective logging because it can be used to aggregate logs from multiple instances.

TraceId is the id that is assigned to every request.

SpanId is used to track a unit of work. Each request can have multiple steps. And each step can have its unique SpanId.

Export is a flag that indicates whether a particular log should be exported to a log aggregation tool such as Zipkin.
Dolayısıyla uygulamaya bir isim vermek gerekir. Şöyle yaparız
spring.application.name=product-consumer-app
Tablo olarak bakarsak şöyle
.---------------.------------.-----------.
|     type      | request id |  span id  |
:---------------+------------+-----------:
| Single thread | same       | same      |
:---------------+------------+-----------:
| Thread pool   | same       | different |
:---------------+------------+-----------:
| Async         | same       | different |
'---------------'------------'-----------'
Spring Beanleri
Açıklaması şöyle
Sleuth automatically injects the necessary headers to all requests generated by, for example, RestTemplate or Feign, using interceptors. For this to work automatically, you need to declare HTTP clients as Spring beans.

Örnek
Basit bir çıktı şöyledir
2019-06-09 08:38:44.638  INFO [product-consumer-app,9cbe06b35ad945e9,9cbe06b35ad945e9,false] 4689 --- [nio-8086-exec-1] SleuthTestController: Wake Up Sleuth
Sleuth’s baggage field feature
Açıklaması şöyle
In a microservice architecture, it is common to pass a custom HTTP header, let’s say: “Correlation-ID”, from one service to another. It might also be equally common to record that value in the log for debugging later.

If you are using Spring Cloud Sleuth, this task would be quite easy with Sleuth’s baggage field feature. You will just have to add a couple of properties to the configuration file and Voilà!
Örnek
Şöyle yaparız
spring.sleuth.baggage.remote-fields=Correlation-Id (1) spring.sleuth.baggage.correlation-fields=Correlation-Id (2)
Açıklaması şöyle
(1) Tell Sleuth to get the value from this header name(s) and propagate to remote services
(2) Set the baggage value(s) to Slf4j’s MDC
Şöyle yaparız
@GetMapping("/message") public String message(@RequestHeader(value = "Correlation-Id", required = false) String correlationId) { log.info("Service-A is called with Correlation-Id: {}", correlationId); String bMsg = restTemplate.getForObject("http://localhost:8081/b/message", String.class); return "Message from B: " + bMsg; }

Tracer Sınıfı
withSpanInScope metodu
Örnek
Şöyle yaparız
@Service
public class SleuthService {

  Logger logger = Logger.getLogger("SleuthService");

  @Autowired
  private Tracer tracer;

  public void sameSpanWork(){
    logger.info("Doing some work");

  }

  public void newSpanWork(){
    logger.info("Original span going on");

    Span newSpan = tracer.nextSpan().name("new sleuth span").start();

    try(Tracer.SpanInScope span = tracer.withSpanInScope(newSpan.start())){
      logger.info("This work is being done in the new span");
    } finally {
      newSpan.finish();
    }

    logger.info("Back to original span");
  }
}
Çıktı olarak şunu alırız. SpanID alanının değiştiği görülebilir.
2019-06-09 09:16:51.410  INFO [product-consumer-app,0ebf1c258ab7f823,0ebf1c258ab7f823,false] 6125 --- [nio-8086-exec-1] SleuthTestController                     : New span work initiated
2019-06-09 09:16:51.410  INFO [product-consumer-app,0ebf1c258ab7f823,0ebf1c258ab7f823,false] 6125 --- [nio-8086-exec-1] SleuthService                            : Original span going on
2019-06-09 09:16:51.411  INFO [product-consumer-app,0ebf1c258ab7f823,7d4b060ff51d04db,false] 6125 --- [nio-8086-exec-1] SleuthService                            : This work is being done in the new span
2019-06-09 09:16:51.413  INFO [product-consumer-app,0ebf1c258ab7f823,0ebf1c258ab7f823,false] 6125 --- [nio-8086-exec-1] SleuthService                            : Back to original span

Hiç yorum yok:

Yorum Gönder