diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index 1d8e82572b..bb1af47126 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -114,6 +114,7 @@ spring-boot-graalvm-docker spring-boot-validations spring-boot-openapi + spring-boot-brave diff --git a/spring-boot-modules/spring-boot-brave/pom.xml b/spring-boot-modules/spring-boot-brave/pom.xml new file mode 100644 index 0000000000..1cc8f44cf7 --- /dev/null +++ b/spring-boot-modules/spring-boot-brave/pom.xml @@ -0,0 +1,56 @@ + + + 4.0.0 + com.baeldung + spring-boot-brave + 1.0.0-SNAPSHOT + spring-boot-brave + jar + + + com.baeldung.spring-boot-modules + spring-boot-modules + 1.0.0-SNAPSHOT + + + + + org.springframework.boot + spring-boot-starter-web + + + io.zipkin.brave + brave + 6.0.2 + + + io.zipkin.reporter2 + zipkin-reporter + 3.3.0 + + + io.zipkin.reporter2 + zipkin-sender-okhttp3 + 3.3.0 + + + io.zipkin.reporter2 + zipkin-reporter-brave + 3.3.0 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-brave/src/main/java/com/baeldung/brave/Application.java b/spring-boot-modules/spring-boot-brave/src/main/java/com/baeldung/brave/Application.java new file mode 100644 index 0000000000..7c7bc05a07 --- /dev/null +++ b/spring-boot-modules/spring-boot-brave/src/main/java/com/baeldung/brave/Application.java @@ -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); + } +} diff --git a/spring-boot-modules/spring-boot-brave/src/main/java/com/baeldung/brave/configuration/TracingConfiguration.java b/spring-boot-modules/spring-boot-brave/src/main/java/com/baeldung/brave/configuration/TracingConfiguration.java new file mode 100644 index 0000000000..5b36fecce2 --- /dev/null +++ b/spring-boot-modules/spring-boot-brave/src/main/java/com/baeldung/brave/configuration/TracingConfiguration.java @@ -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(); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-brave/src/main/java/com/baeldung/brave/service/TracingService.java b/spring-boot-modules/spring-boot-brave/src/main/java/com/baeldung/brave/service/TracingService.java new file mode 100644 index 0000000000..139718c54d --- /dev/null +++ b/spring-boot-modules/spring-boot-brave/src/main/java/com/baeldung/brave/service/TracingService.java @@ -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(); + } + } +}