updates to supporting classes

This commit is contained in:
Spencer Gibb
2015-06-22 13:43:18 -06:00
parent 3a28d4abd7
commit a7b910ed40
2 changed files with 45 additions and 5 deletions

View File

@@ -1,16 +1,23 @@
package org.springframework.cloud.sleuth.trace;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @author Spencer Gibb
*/
public class SpanHolder {
private static final ThreadLocal<Span> currentSpan = new ThreadLocal<>();
private static final Log log = LogFactory.getLog(SpanHolder.class);
public Span getCurrentSpan() {
public static Span getCurrentSpan() {
return currentSpan.get();
}
public void setCurrentSpan(Span span) {
public static void setCurrentSpan(Span span) {
if (log.isTraceEnabled()) {
log.trace("Setting current span " + span);
}
currentSpan.set(span);
}
}

View File

@@ -1,6 +1,7 @@
package org.springframework.cloud.sleuth.trace;
import lombok.Data;
import lombok.extern.apachecommons.CommonsLog;
import java.io.Closeable;
import java.io.IOException;
@@ -9,6 +10,7 @@ import java.io.IOException;
* @author Spencer Gibb
*/
@Data
@CommonsLog
public class TraceScope implements Closeable {
/**
* the span for this scope
@@ -30,13 +32,44 @@ public class TraceScope implements Closeable {
* @return the same Span object
*/
public Span detach() {
//TODO: implement detach
if (detached) {
error("Tried to detach trace span " + span + " but " +
"it has already been detached.");
}
detached = true;
Span cur = SpanHolder.getCurrentSpan();
if (cur != span) {
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);
}
return span;
}
@Override
public void close() throws IOException {
span.stop();
//TODO: set savedSpan to currentSpan in SpanHolder
if (detached) {
return;
}
detached = true;
Span cur = SpanHolder.getCurrentSpan();
if (cur != span) {
error("Tried to close 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 {
span.stop();
SpanHolder.setCurrentSpan(savedSpan);
}
}
private void error(String msg) {
log.error(msg);
throw new RuntimeException(msg);
}
}