[BAEL-7321] Introduction to Brave (#16306)
This commit is contained in:
parent
e5f9a53f7d
commit
849c771ca5
|
@ -114,6 +114,7 @@
|
|||
<module>spring-boot-graalvm-docker</module>
|
||||
<module>spring-boot-validations</module>
|
||||
<module>spring-boot-openapi</module>
|
||||
<module>spring-boot-brave</module>
|
||||
</modules>
|
||||
|
||||
<dependencyManagement>
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-boot-brave</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>spring-boot-brave</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.zipkin.brave</groupId>
|
||||
<artifactId>brave</artifactId>
|
||||
<version>6.0.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.zipkin.reporter2</groupId>
|
||||
<artifactId>zipkin-reporter</artifactId>
|
||||
<version>3.3.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.zipkin.reporter2</groupId>
|
||||
<artifactId>zipkin-sender-okhttp3</artifactId>
|
||||
<version>3.3.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.zipkin.reporter2</groupId>
|
||||
<artifactId>zipkin-reporter-brave</artifactId>
|
||||
<version>3.3.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.brave;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.brave.configuration;
|
||||
|
||||
import brave.Tracer;
|
||||
import brave.Tracing;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import zipkin2.reporter.BytesMessageSender;
|
||||
import zipkin2.reporter.brave.AsyncZipkinSpanHandler;
|
||||
import zipkin2.reporter.okhttp3.OkHttpSender;
|
||||
|
||||
@Configuration
|
||||
public class TracingConfiguration {
|
||||
|
||||
@Bean
|
||||
BytesMessageSender sender() {
|
||||
return OkHttpSender.create("http://127.0.0.1:9411/api/v2/spans");
|
||||
}
|
||||
|
||||
@Bean
|
||||
AsyncZipkinSpanHandler zipkinSpanHandler(BytesMessageSender sender) {
|
||||
return AsyncZipkinSpanHandler.create(sender);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Tracing tracing(AsyncZipkinSpanHandler zipkinSpanHandler) {
|
||||
return Tracing.newBuilder()
|
||||
.localServiceName("Dummy Service")
|
||||
.addSpanHandler(zipkinSpanHandler)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Tracer tracer(Tracing tracing) {
|
||||
return tracing.tracer();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.brave.service;
|
||||
|
||||
import brave.Span;
|
||||
import brave.Tracer;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class TracingService {
|
||||
|
||||
private final Tracer tracer;
|
||||
|
||||
public TracingService(Tracer tracer) {
|
||||
this.tracer = tracer;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void postConstruct() {
|
||||
Span span = tracer.nextSpan().name("Hello from Service").start();
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
span.finish();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue