Technologie

Tracing v CodeNOW pro Java Spring Boot

Jak implementovat a používat distribuovaný tracing v CodeNOW pro aplikace v Java Spring Boot.

Tracing in Java Spring Boot

Overview

Distributed tracing provides visibility into request flows across application components. According to the article, "A unique trace ID is generated for every new request. As some component receives the request a new span is assigned for that component and the span is added to the trace."

Implementation Steps

Dependencies

Add these libraries to your Maven configuration:

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

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-sleuth-zipkin</artifactId>
    <version>3.0.2</version>
</dependency>

Configuration

Update application.yaml with tracing settings:

For CodeNOW deployments:

spring:
  application:
    name: demo-app-spring-bl
  zipkin:
    enabled: true
    baseUrl: http://tracing-jaeger-collector.tracing-system:9411
  sleuth:
    propagation:
      type: B3
      tag:
        enabled: true

For local development:
Replace the collector URL with http://localhost:9411

For Kafka integration:
Enable messaging tracing by adding:

spring:
  sleuth:
    messaging:
      kafka:
        enabled: true

Database Query Tracing

The guide recommends creating a TracingHelper component and an AOP aspect to automatically generate spans for repository calls, eliminating manual span management for database operations.

Custom Spans

Developers can manually create spans using the provided helper class, wrapping operations in try-finally blocks to ensure proper lifecycle management.

Napsal/a CodeNOW