New API: Add shutdown callback to shutdown the Tracer
Also add getMeta() to Span interface.
MODIFY
dd-trace/src/main/java/datadog/trace/tracer/Span.java
dd-trace/src/main/java/datadog/trace/tracer/Span.java
package datadog.trace.tracer;
+import java.util.Map;
+
/**
* A single measurement of time with arbitrary key-value attributes.
*
@@ -94,6 +96,13 @@ public interface Span {
*/
void attachThrowable(Throwable throwable);
+ /**
+ * Get all the metadata attached to this span.
+ *
+ * @return
+ */
+ Map<String, Object> getMeta();
+
/**
* Get a meta value on a span.
*
@@ -127,5 +136,6 @@ public interface Span {
*
* @param finishTimestampNanoseconds Epoch time in nanoseconds.
*/
+ // FIXME: This should take a Timestamp object instead.
void finish(long finishTimestampNanoseconds);
}

MODIFY
dd-trace/src/main/java/datadog/trace/tracer/SpanImpl.java
dd-trace/src/main/java/datadog/trace/tracer/SpanImpl.java
@@ -212,6 +212,7 @@ class SpanImpl implements Span {
}
}
+ @Override
@JsonGetter("meta")
synchronized Map<String, String> getMeta() {
final Map<String, String> result = new HashMap<>(meta.size());
@@ -264,6 +265,7 @@ class SpanImpl implements Span {
}
}
+ // FIXME: This should take a Timestamp object instead.
@Override
public synchronized void finish(final long finishTimestampNanoseconds) {
if (isFinished()) {

MODIFY
dd-trace/src/main/java/datadog/trace/tracer/Tracer.java
dd-trace/src/main/java/datadog/trace/tracer/Tracer.java
@@ -5,7 +5,6 @@ import datadog.trace.tracer.sampling.AllSampler;
import datadog.trace.tracer.sampling.Sampler;
import datadog.trace.tracer.writer.Writer;
import java.io.Closeable;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -29,6 +28,12 @@ public class Tracer implements Closeable {
/** Interceptors to be called on certain trace and span events */
private final List<Interceptor> interceptors;
+ /**
+ * JVM shutdown callback, keeping a reference to it to remove this if DDTracer gets destroyed
+ * earlier
+ */
+ private final Thread shutdownCallback;
+
@Builder
private Tracer(
final Config config,
@@ -45,6 +50,19 @@ public class Tracer implements Closeable {
interceptors != null
? Collections.unmodifiableList(new ArrayList<>(interceptors))
: Collections.<Interceptor>emptyList();
+
+ shutdownCallback =
+ new Thread() {
+ @Override
+ public void run() {
+ close();
+ }
+ };
+ try {
+ Runtime.getRuntime().addShutdownHook(shutdownCallback);
+ } catch (final IllegalStateException ex) {
+ // The JVM is already shutting down.
+ }
}
/** @return {@link Writer} used by this tracer */
@@ -124,7 +142,13 @@ public class Tracer implements Closeable {
}
@Override
- public void close() throws IOException {
+ public void finalize() {
+ Runtime.getRuntime().removeShutdownHook(shutdownCallback);
+ shutdownCallback.run();
+ }
+
+ @Override
+ public void close() {
writer.close();
}
}

New API: Add shutdown callback to shutdown the Tracer

Also add getMeta() to Span interface.