Rename currentTrace() to currentSpan(), fix up a couple potential NPEs
diff --git a/src/main/java/org/cloudera/htrace/Trace.java b/src/main/java/org/cloudera/htrace/Trace.java
index 80066b0..18aa8fb 100644
--- a/src/main/java/org/cloudera/htrace/Trace.java
+++ b/src/main/java/org/cloudera/htrace/Trace.java
@@ -91,16 +91,6 @@
}
/**
- * Stop any spans that are currently active, as well
- * as their parents. This can be used within threadpools,
- * etc where you should clean up after anyone who might
- * have accidentally leaked spans.
- */
- public static void clearTraceStack() {
- Tracer.getInstance().clearTraceStack();
- }
-
- /**
* Set the processId to be used for all Spans created by this Tracer.
*
* @see Span.java
@@ -133,14 +123,20 @@
* Adds a data annotation to the current span if tracing is currently on.
*/
public static void addKVAnnotation(byte[] key, byte[] value) {
- currentSpan().addKVAnnotation(key, value);
+ Span s = currentSpan();
+ if (s != null) {
+ s.addKVAnnotation(key, value);
+ }
}
/**
* Annotate the current span with the given message.
*/
public static void addTimelineAnnotation(String msg) {
- currentSpan().addTimelineAnnotation(msg);
+ Span s = currentSpan();
+ if (s != null) {
+ s.addTimelineAnnotation(msg);
+ }
}
/**
@@ -158,7 +154,7 @@
* @return Span representing the current trace, or null if not tracing.
*/
public static Span currentSpan() {
- return Tracer.getInstance().currentTrace();
+ return Tracer.getInstance().currentSpan();
}
/**
diff --git a/src/main/java/org/cloudera/htrace/TraceScope.java b/src/main/java/org/cloudera/htrace/TraceScope.java
index 6fd8830..86ad130 100644
--- a/src/main/java/org/cloudera/htrace/TraceScope.java
+++ b/src/main/java/org/cloudera/htrace/TraceScope.java
@@ -31,7 +31,7 @@
public Span detach() {
detached = true;
- Span cur = Tracer.getInstance().currentTrace();
+ Span cur = Tracer.getInstance().currentSpan();
if (cur != span) {
Tracer.LOG.debug("Closing trace span " + span + " but " +
cur + " was top-of-stack");
diff --git a/src/main/java/org/cloudera/htrace/Tracer.java b/src/main/java/org/cloudera/htrace/Tracer.java
index 06e3d9d..9efe627 100644
--- a/src/main/java/org/cloudera/htrace/Tracer.java
+++ b/src/main/java/org/cloudera/htrace/Tracer.java
@@ -34,7 +34,7 @@
public static final Log LOG = LogFactory.getLog(Tracer.class);
private final static Random random = new SecureRandom();
private final List<SpanReceiver> receivers = new CopyOnWriteArrayList<SpanReceiver>();
- private static final ThreadLocal<Span> currentTrace = new ThreadLocal<Span>() {
+ private static final ThreadLocal<Span> currentSpan = new ThreadLocal<Span>() {
@Override
protected Span initialValue() {
return null;
@@ -53,7 +53,7 @@
}
protected Span createNew(String description) {
- Span parent = currentTrace.get();
+ Span parent = currentSpan.get();
if (parent == null) {
return new MilliSpan(description,
/* traceId = */ random.nextLong(),
@@ -66,11 +66,11 @@
}
protected boolean isTracing() {
- return currentTrace.get() != null;
+ return currentSpan.get() != null;
}
- protected Span currentTrace() {
- return currentTrace.get();
+ protected Span currentSpan() {
+ return currentSpan.get();
}
protected void deliver(Span span) {
@@ -88,24 +88,17 @@
}
protected Span setCurrentSpan(Span span) {
- currentTrace.set(span);
+ currentSpan.set(span);
return span;
}
public TraceScope continueSpan(Span s) {
- Span oldCurrent = currentTrace();
+ Span oldCurrent = currentSpan();
setCurrentSpan(s);
return new TraceScope(s, oldCurrent);
}
-
- protected void clearTraceStack() {
- while (isTracing()) {
- currentTrace().stop();
- }
- }
-
protected int numReceivers() {
return receivers.size();
}