Add a NullScope singleton
Introduce a NullScope singleton to reduce object creation. Also adds some
methods for debugging and make trace log mode a little more helpful. Pom
version bumped to 3.0.3-SNAPSHOT.
diff --git a/htrace-core/pom.xml b/htrace-core/pom.xml
index 1685a58..120ce7b 100644
--- a/htrace-core/pom.xml
+++ b/htrace-core/pom.xml
@@ -18,7 +18,7 @@
<parent>
<artifactId>htrace</artifactId>
<groupId>org.htrace</groupId>
- <version>3.0.3</version>
+ <version>3.0.3-SNAPSHOT</version>
</parent>
<name>htrace-core</name>
diff --git a/htrace-core/src/main/java/org/htrace/NullScope.java b/htrace-core/src/main/java/org/htrace/NullScope.java
new file mode 100644
index 0000000..1f90156
--- /dev/null
+++ b/htrace-core/src/main/java/org/htrace/NullScope.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.htrace;
+
+/**
+ * Singleton instance representing an empty {@link TraceScope}.
+ */
+public final class NullScope extends TraceScope {
+
+ public static final TraceScope INSTANCE = new NullScope();
+
+ private NullScope() {
+ super(null, null);
+ }
+
+ @Override
+ public String toString() {
+ return "NullScope";
+ }
+}
diff --git a/htrace-core/src/main/java/org/htrace/Trace.java b/htrace-core/src/main/java/org/htrace/Trace.java
index 4c14b14..8b1255b 100644
--- a/htrace-core/src/main/java/org/htrace/Trace.java
+++ b/htrace-core/src/main/java/org/htrace/Trace.java
@@ -91,9 +91,8 @@
* Pick up an existing span from another thread.
*/
public static TraceScope continueSpan(Span s) {
- // Return an empty TraceScope that does nothing on
- // close
- if (s == null) return new TraceScope(null, null);
+ // Return an empty TraceScope that does nothing on close
+ if (s == null) return NullScope.INSTANCE;
return Tracer.getInstance().continueSpan(s);
}
diff --git a/htrace-core/src/main/java/org/htrace/TraceScope.java b/htrace-core/src/main/java/org/htrace/TraceScope.java
index 07b7929..f001c52 100644
--- a/htrace-core/src/main/java/org/htrace/TraceScope.java
+++ b/htrace-core/src/main/java/org/htrace/TraceScope.java
@@ -61,6 +61,14 @@
return span;
}
+ /**
+ * Return true when {@link #detach()} has been called. Helpful when debugging
+ * multiple threads working on a single span.
+ */
+ public boolean isDetached() {
+ return detached;
+ }
+
@Override
public void close() {
if (span == null) return;
diff --git a/htrace-core/src/main/java/org/htrace/Tracer.java b/htrace-core/src/main/java/org/htrace/Tracer.java
index bf69afc..ac31165 100644
--- a/htrace-core/src/main/java/org/htrace/Tracer.java
+++ b/htrace-core/src/main/java/org/htrace/Tracer.java
@@ -91,6 +91,9 @@
}
protected Span setCurrentSpan(Span span) {
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("setting current span " + span);
+ }
currentSpan.set(span);
return span;
}
@@ -119,4 +122,4 @@
}
return processId;
}
-}
\ No newline at end of file
+}
diff --git a/htrace-core/src/main/java/org/htrace/impl/MilliSpan.java b/htrace-core/src/main/java/org/htrace/impl/MilliSpan.java
index 0d47a40..39c9671 100644
--- a/htrace-core/src/main/java/org/htrace/impl/MilliSpan.java
+++ b/htrace-core/src/main/java/org/htrace/impl/MilliSpan.java
@@ -91,10 +91,7 @@
@Override
public String toString() {
- return "start=" + start + "\nstop=" + stop + "\nparentId=" + parentSpanId
- + "\ndescription=" + description + "\nspanId=" + spanId + "\ntraceId=" + traceId
- + "\ntraceInfo=" + traceInfo + "\nprocessId=" + processId
- + "\ntimeline=" + timeline;
+ return String.format("Span{Id:0x%16x,parentId:0x%16x,desc:%s}", spanId, parentSpanId, description);
}
@Override
@@ -161,4 +158,4 @@
public String getProcessId() {
return processId;
}
-}
\ No newline at end of file
+}
diff --git a/htrace-zipkin/pom.xml b/htrace-zipkin/pom.xml
index 6f1f324..e68f1c2 100644
--- a/htrace-zipkin/pom.xml
+++ b/htrace-zipkin/pom.xml
@@ -18,7 +18,7 @@
<parent>
<artifactId>htrace</artifactId>
<groupId>org.htrace</groupId>
- <version>3.0.3</version>
+ <version>3.0.3-SNAPSHOT</version>
</parent>
<name>htrace-zipkin</name>
diff --git a/pom.xml b/pom.xml
index 24e4e63..41a2e66 100644
--- a/pom.xml
+++ b/pom.xml
@@ -14,7 +14,7 @@
<groupId>org.htrace</groupId>
<artifactId>htrace</artifactId>
- <version>3.0.3</version>
+ <version>3.0.3-SNAPSHOT</version>
<packaging>pom</packaging>
<name>htrace</name>