JAVA-11493 Renamed spring-sleuth to spring-cloud-sleuth (#12365)

This commit is contained in:
Dhawal Kapil
2022-06-16 23:57:01 +05:30
committed by GitHub
parent a5ddb7dd9c
commit 4206dfd9e6
13 changed files with 5 additions and 5 deletions
@@ -0,0 +1,11 @@
package com.baeldung.sleuth.traceid;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SleuthCurrentTraceIdApp {
public static void main(String[] args) {
SpringApplication.run(SleuthCurrentTraceIdApp.class, args);
}
}
@@ -0,0 +1,32 @@
package com.baeldung.sleuth.traceid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import brave.Span;
import brave.Tracer;
@RestController
public class SleuthTraceIdController {
private static final Logger logger = LoggerFactory.getLogger(SleuthTraceIdController.class);
@Autowired
private Tracer tracer;
@GetMapping("/traceid")
public String getSleuthTraceId() {
logger.info("Hello with Sleuth");
Span span = tracer.currentSpan();
if (span != null) {
logger.info("Span ID hex {}", span.context().spanIdString());
logger.info("Span ID decimal {}", span.context().spanId());
logger.info("Trace ID hex {}", span.context().traceIdString());
logger.info("Trace ID decimal {}", span.context().traceId());
}
return "Hello from Sleuth";
}
}
@@ -0,0 +1,26 @@
package com.baeldung.spring.session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class SchedulingService {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private final SleuthService sleuthService;
@Autowired
public SchedulingService(SleuthService sleuthService) {
this.sleuthService = sleuthService;
}
@Scheduled(fixedDelay = 30000)
public void scheduledWork() throws InterruptedException {
logger.info("Start some work from the scheduled task");
sleuthService.asyncMethod();
logger.info("End work from scheduled task");
}
}
@@ -0,0 +1,68 @@
package com.baeldung.spring.session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.Executor;
@RestController
public class SleuthController {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private final SleuthService sleuthService;
private final Executor executor;
@Autowired
public SleuthController(SleuthService sleuthService, Executor executor) {
this.sleuthService = sleuthService;
this.executor = executor;
}
@GetMapping("/")
public String helloSleuth() {
logger.info("Hello Sleuth");
return "success";
}
@GetMapping("/same-span")
public String helloSleuthSameSpan() throws InterruptedException {
logger.info("Same Span");
sleuthService.doSomeWorkSameSpan();
return "success";
}
@GetMapping("/new-span")
public String helloSleuthNewSpan() throws InterruptedException {
logger.info("New Span");
sleuthService.doSomeWorkNewSpan();
return "success";
}
@GetMapping("/new-thread")
public String helloSleuthNewThread() {
logger.info("New Thread");
Runnable runnable = () -> {
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.info("I'm inside the new thread - with a new span");
};
executor.execute(runnable);
logger.info("I'm done - with the original span");
return "success";
}
@GetMapping("/async")
public String helloSleuthAsync() throws InterruptedException {
logger.info("Before Async Method Call");
sleuthService.asyncMethod();
logger.info("After Async Method Call");
return "success";
}
}
@@ -0,0 +1,48 @@
package com.baeldung.spring.session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import brave.Span;
import brave.Tracer;
import brave.Tracer.SpanInScope;
@Service
public class SleuthService {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private final Tracer tracer;
@Autowired
public SleuthService(Tracer tracer) {
this.tracer = tracer;
}
public void doSomeWorkSameSpan() throws InterruptedException {
Thread.sleep(1000L);
logger.info("Doing some work");
}
public void doSomeWorkNewSpan() throws InterruptedException {
logger.info("I'm in the original span");
Span newSpan = tracer.nextSpan().name("newSpan").start();
try (SpanInScope ws = tracer.withSpanInScope(newSpan.start())) {
Thread.sleep(1000L);
logger.info("I'm in the new span doing some cool work that needs its own span");
} finally {
newSpan.finish();
}
logger.info("I'm in the original span");
}
@Async
public void asyncMethod() throws InterruptedException {
logger.info("Start Async Method");
Thread.sleep(1000L);
logger.info("End Async Method");
}
}
@@ -0,0 +1,11 @@
package com.baeldung.spring.session;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SleuthWebApp {
public static void main(String[] args) {
SpringApplication.run(SleuthWebApp.class, args);
}
}
@@ -0,0 +1,55 @@
package com.baeldung.spring.session;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.instrument.async.LazyTraceExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
@Configuration
@EnableAsync
@EnableScheduling
public class ThreadConfig extends AsyncConfigurerSupport implements SchedulingConfigurer {
@Autowired
private BeanFactory beanFactory;
@Bean
public Executor executor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(1);
threadPoolTaskExecutor.setMaxPoolSize(1);
threadPoolTaskExecutor.initialize();
return new LazyTraceExecutor(beanFactory, threadPoolTaskExecutor);
}
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(1);
threadPoolTaskExecutor.setMaxPoolSize(1);
threadPoolTaskExecutor.initialize();
return new LazyTraceExecutor(beanFactory, threadPoolTaskExecutor);
}
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.setScheduler(schedulingExecutor());
}
@Bean(destroyMethod = "shutdown")
public Executor schedulingExecutor() {
return Executors.newScheduledThreadPool(1);
}
}
@@ -0,0 +1 @@
spring.application.name=Baeldung Sleuth Tutorial
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- this is the configuration needed for the Sleuth examples;
don't override this with the standard logback config
-->
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="feign" level="DEBUG"/>
<logger name="org.springframework.cloud.sleuth" level="TRACE"/>
<logger name="org.springframework.boot.autoconfigure.logging" level="INFO"/>
<logger name="org.springframework.cloud.sleuth.log" level="DEBUG"/>
<logger name="org.springframework.cloud.sleuth.trace" level="DEBUG"/>
<logger name="org.springframework.cloud.sleuth.instrument.rxjava" level="DEBUG"/>
<logger name="org.springframework.cloud.sleuth.instrument.reactor" level="TRACE"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</configuration>