Added SpanStartListener and slf4j SpanStartListener and SpanReceiver
This commit is contained in:
@@ -9,6 +9,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
|
||||
import org.springframework.cloud.sleuth.trace.Trace;
|
||||
import org.springframework.cloud.sleuth.trace.TraceScope;
|
||||
import org.springframework.cloud.sleuth.trace.sampler.AlwaysSampler;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -22,43 +25,61 @@ import org.springframework.web.client.RestTemplate;
|
||||
@EnableAutoConfiguration
|
||||
@RestController
|
||||
@Slf4j
|
||||
public class SampleApplication implements ApplicationListener<EmbeddedServletContainerInitializedEvent> {
|
||||
public class SampleApplication implements
|
||||
ApplicationListener<EmbeddedServletContainerInitializedEvent> {
|
||||
|
||||
public static final String CLIENT_NAME = "testApp";
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
private int port;
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
@Autowired
|
||||
private Trace trace;
|
||||
private int port;
|
||||
|
||||
@SneakyThrows
|
||||
@RequestMapping("/")
|
||||
@SneakyThrows
|
||||
@RequestMapping("/")
|
||||
public String hi() {
|
||||
final Random random = new Random();
|
||||
Thread.sleep(random.nextInt(1000));
|
||||
final Random random = new Random();
|
||||
Thread.sleep(random.nextInt(1000));
|
||||
|
||||
String s = restTemplate.getForObject("http://localhost:" + port + "/hi2", String.class);
|
||||
return "hi/"+s;
|
||||
String s = restTemplate.getForObject("http://localhost:" + port + "/hi2",
|
||||
String.class);
|
||||
return "hi/" + s;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@RequestMapping("/hi2")
|
||||
public String hi2() {
|
||||
final Random random = new Random();
|
||||
Thread.sleep(random.nextInt(1000));
|
||||
return "hi2";
|
||||
}
|
||||
@SneakyThrows
|
||||
@RequestMapping("/hi2")
|
||||
public String hi2() {
|
||||
final Random random = new Random();
|
||||
Thread.sleep(random.nextInt(1000));
|
||||
return "hi2";
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@RequestMapping("/traced")
|
||||
public String traced() {
|
||||
TraceScope scope = trace.startSpan("customTraceEndpoint", new AlwaysSampler());
|
||||
final Random random = new Random();
|
||||
int millis = random.nextInt(1000);
|
||||
log.info("Sleeping for {} millis", millis);
|
||||
Thread.sleep(millis);
|
||||
|
||||
String s = restTemplate.getForObject("http://localhost:" + port + "/hi2", String.class);
|
||||
scope.close();
|
||||
return "hi/"+s;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SampleApplication.class, args);
|
||||
}
|
||||
|
||||
/*@Bean
|
||||
public SpanCollector spanCollector() {
|
||||
return new LoggingSpanCollectorImpl();
|
||||
}*/
|
||||
/*
|
||||
* @Bean public SpanCollector spanCollector() { return new LoggingSpanCollectorImpl();
|
||||
* }
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
|
||||
port = event.getEmbeddedServletContainer().getPort();
|
||||
}
|
||||
@Override
|
||||
public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
|
||||
port = event.getEmbeddedServletContainer().getPort();
|
||||
}
|
||||
}
|
||||
|
||||
18
spring-cloud-sleuth-sample/src/main/resources/logback.xml
Normal file
18
spring-cloud-sleuth-sample/src/main/resources/logback.xml
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
|
||||
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
|
||||
|
||||
<property name="CONSOLE_LOG_PATTERN" value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- }){magenta} %clr(---){faint} %clr(%X{Span-Id:- }){yellow} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wex"/>
|
||||
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } --- %X{Span-Id:- }[%t] %-40.40logger{39} : %m%n%wex"/>
|
||||
|
||||
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
|
||||
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="CONSOLE" />
|
||||
<appender-ref ref="FILE" />
|
||||
</root>
|
||||
<logger name="org.springframework.web" level="DEBUG"/>
|
||||
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user