Big refactor of sleuth core API
Instrumentation should be able to get by with only 2 interfaces: TraceManager and TraceAccessor (the former is not needed if you aren't starting a new Span). No explicit access to thread locals or manipulation of thread context is required (except locally where necessary). A Span is enclosed by a Trace (actually a view of the complete Trace that would be constructed remotely).
This commit is contained in:
@@ -21,7 +21,7 @@ import java.util.Random;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.sleuth.Trace;
|
||||
import org.springframework.cloud.sleuth.TraceManager;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -32,7 +32,7 @@ import org.springframework.stereotype.Component;
|
||||
public class SampleBackground {
|
||||
|
||||
@Autowired
|
||||
private Trace trace;
|
||||
private TraceManager traceManager;
|
||||
|
||||
@SneakyThrows
|
||||
@Async
|
||||
@@ -40,7 +40,7 @@ public class SampleBackground {
|
||||
final Random random = new Random();
|
||||
int millis = random.nextInt(1000);
|
||||
Thread.sleep(millis);
|
||||
this.trace.addAnnotation("background-sleep-millis", String.valueOf(millis));
|
||||
this.traceManager.addAnnotation("background-sleep-millis", String.valueOf(millis));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,21 +19,21 @@ package sample;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
|
||||
import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.cloud.sleuth.Trace;
|
||||
import org.springframework.cloud.sleuth.TraceContextHolder;
|
||||
import org.springframework.cloud.sleuth.TraceScope;
|
||||
import org.springframework.cloud.sleuth.TraceAccessor;
|
||||
import org.springframework.cloud.sleuth.TraceManager;
|
||||
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@@ -44,7 +44,9 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
@Autowired
|
||||
private Trace trace;
|
||||
private TraceManager trace;
|
||||
@Autowired
|
||||
private TraceAccessor accessor;
|
||||
@Autowired
|
||||
private SampleBackground controller;
|
||||
private int port;
|
||||
@@ -69,7 +71,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
|
||||
int millis = random.nextInt(1000);
|
||||
Thread.sleep(millis);
|
||||
SampleController.this.trace.addAnnotation("callable-sleep-millis", String.valueOf(millis));
|
||||
Span currentSpan = TraceContextHolder.getCurrentSpan();
|
||||
Span currentSpan = SampleController.this.accessor.getCurrentSpan();
|
||||
return "async hi: " + currentSpan;
|
||||
}
|
||||
};
|
||||
@@ -94,7 +96,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
|
||||
@SneakyThrows
|
||||
@RequestMapping("/traced")
|
||||
public String traced() {
|
||||
TraceScope scope = this.trace.startSpan("customTraceEndpoint",
|
||||
Trace scope = this.trace.startSpan("customTraceEndpoint",
|
||||
new AlwaysSampler(), null);
|
||||
final Random random = new Random();
|
||||
int millis = random.nextInt(1000);
|
||||
@@ -104,7 +106,7 @@ ApplicationListener<EmbeddedServletContainerInitializedEvent> {
|
||||
|
||||
String s = this.restTemplate.getForObject("http://localhost:" + this.port
|
||||
+ "/call", String.class);
|
||||
scope.close();
|
||||
this.trace.close(scope);
|
||||
return "traced/" + s;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user