rename SpanHolder to TraceContextHolder
This commit is contained in:
@@ -68,7 +68,7 @@ public class DefaultTrace implements Trace {
|
||||
@Override
|
||||
public <T> TraceScope startSpan(String description, Sampler<T> s, T info) {
|
||||
Span span = null;
|
||||
if (isTracing() || s.next(info)) {
|
||||
if (TraceContextHolder.isTracing() || s.next(info)) {
|
||||
span = createNew(description);
|
||||
}
|
||||
return doStart(span);
|
||||
@@ -114,12 +114,12 @@ public class DefaultTrace implements Trace {
|
||||
// Return an empty TraceScope that does nothing on close
|
||||
if (span == null) return NullScope.INSTANCE;
|
||||
Span oldSpan = getCurrentSpan();
|
||||
SpanHolder.setCurrentSpan(span);
|
||||
TraceContextHolder.setCurrentSpan(span);
|
||||
return new TraceScope(this, span, oldSpan);
|
||||
}
|
||||
|
||||
protected Span getCurrentSpan() {
|
||||
return SpanHolder.getCurrentSpan();
|
||||
return TraceContextHolder.getCurrentSpan();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -130,11 +130,6 @@ public class DefaultTrace implements Trace {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTracing() {
|
||||
return getCurrentSpan() != null;
|
||||
}
|
||||
|
||||
//TODO: rename? this is the end of a Span lifecycle
|
||||
@Override
|
||||
public void deliver(Span span) {
|
||||
|
||||
@@ -77,10 +77,5 @@ public interface Trace {
|
||||
*/
|
||||
void addKVAnnotation(String key, String value);
|
||||
|
||||
/**
|
||||
* Returns true if the current thread is a part of a trace, false otherwise.
|
||||
*/
|
||||
boolean isTracing();
|
||||
|
||||
void deliver(Span span);
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
package org.springframework.cloud.sleuth;
|
||||
|
||||
import lombok.extern.apachecommons.CommonsLog;
|
||||
import org.springframework.core.NamedThreadLocal;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@CommonsLog
|
||||
public class SpanHolder {
|
||||
private static final ThreadLocal<Span> currentSpan = new ThreadLocal<>();
|
||||
public class TraceContextHolder {
|
||||
private static final ThreadLocal<Span> currentSpan = new NamedThreadLocal<>("Trace Context");
|
||||
|
||||
public static Span getCurrentSpan() {
|
||||
return currentSpan.get();
|
||||
@@ -45,14 +45,14 @@ public class TraceScope implements Closeable {
|
||||
}
|
||||
detached = true;
|
||||
|
||||
Span cur = SpanHolder.getCurrentSpan();
|
||||
Span cur = TraceContextHolder.getCurrentSpan();
|
||||
if (cur != span) {
|
||||
Utils.error("Tried to detach trace span " + span + " but " +
|
||||
"it is not the current span for the " +
|
||||
Thread.currentThread().getName() + " thread. You have " +
|
||||
"probably forgotten to close or detach " + cur);
|
||||
} else {
|
||||
SpanHolder.setCurrentSpan(savedSpan);
|
||||
TraceContextHolder.setCurrentSpan(savedSpan);
|
||||
}
|
||||
return span;
|
||||
}
|
||||
@@ -64,7 +64,7 @@ public class TraceScope implements Closeable {
|
||||
return;
|
||||
}
|
||||
detached = true;
|
||||
Span cur = SpanHolder.getCurrentSpan();
|
||||
Span cur = TraceContextHolder.getCurrentSpan();
|
||||
if (cur != span) {
|
||||
Utils.error("Tried to close trace span " + span + " but " +
|
||||
"it is not the current span for the " +
|
||||
@@ -74,7 +74,7 @@ public class TraceScope implements Closeable {
|
||||
span.stop();
|
||||
//TODO: use ApplicationEvents here?
|
||||
trace.deliver(span);
|
||||
SpanHolder.setCurrentSpan(savedSpan);
|
||||
TraceContextHolder.setCurrentSpan(savedSpan);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.springframework.cloud.sleuth.sampler;
|
||||
|
||||
import org.springframework.cloud.sleuth.Sampler;
|
||||
import org.springframework.cloud.sleuth.SpanHolder;
|
||||
import org.springframework.cloud.sleuth.TraceContextHolder;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
@@ -10,6 +10,6 @@ public class IsTracingSampler implements Sampler<Object> {
|
||||
|
||||
@Override
|
||||
public boolean next(Object info) {
|
||||
return SpanHolder.getCurrentSpan() != null;
|
||||
return TraceContextHolder.getCurrentSpan() != null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,10 +17,11 @@ package org.springframework.cloud.sleuth.web.client;
|
||||
|
||||
import static org.springframework.cloud.sleuth.Trace.SPAN_ID_NAME;
|
||||
import static org.springframework.cloud.sleuth.Trace.TRACE_ID_NAME;
|
||||
import static org.springframework.cloud.sleuth.TraceContextHolder.getCurrentSpan;
|
||||
import static org.springframework.cloud.sleuth.TraceContextHolder.isTracing;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.cloud.sleuth.SpanHolder;
|
||||
import org.springframework.cloud.sleuth.Trace;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
@@ -42,13 +43,13 @@ public class TraceRestTemplateInterceptor implements ClientHttpRequestIntercepto
|
||||
@Override
|
||||
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
|
||||
ClientHttpRequestExecution execution) throws IOException {
|
||||
setHeader(request, SPAN_ID_NAME, SpanHolder.getCurrentSpan().getSpanId());
|
||||
setHeader(request, TRACE_ID_NAME, SpanHolder.getCurrentSpan().getTraceId());
|
||||
setHeader(request, SPAN_ID_NAME, getCurrentSpan().getSpanId());
|
||||
setHeader(request, TRACE_ID_NAME, getCurrentSpan().getTraceId());
|
||||
return execution.execute(request, body);
|
||||
}
|
||||
|
||||
public void setHeader(HttpRequest request, String spanIdName, String spanId) {
|
||||
if (!request.getHeaders().containsKey(spanIdName) && SpanHolder.isTracing()) {
|
||||
if (!request.getHeaders().containsKey(spanIdName) && isTracing()) {
|
||||
request.getHeaders().add(spanIdName, spanId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user