Remove shutdown hook when DDTracer is shutdown
This is similar to the problem with weak map supplier and may lead to
a memory leak if we created/estroy many DDTracer objects.
MODIFY
dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java
dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java
@@ -60,6 +60,11 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
private final Map<String, String> defaultSpanTags;
/** A configured mapping of service names to update with new values */
private final Map<String, String> serviceNameMappings;
+ /**
+ * JVM shutdown callback, keeping a reference to it to remove this if DDTracer gets destroyed
+ * earlier
+ */
+ private final Thread shutdownCallback;
/** Span context decorators */
private final Map<String, List<AbstractDecorator>> spanContextDecorators =
@@ -163,15 +168,15 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
this.runtimeId = runtimeId;
this.serviceNameMappings = serviceNameMappings;
- try {
- Runtime.getRuntime()
- .addShutdownHook(
+ shutdownCallback =
new Thread() {
@Override
public void run() {
close();
}
- });
+ };
+ try {
+ Runtime.getRuntime().addShutdownHook(shutdownCallback);
} catch (final IllegalStateException ex) {
// The JVM is already shutting down.
}
@@ -200,6 +205,12 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
log.info("New instance: {}", this);
}
+ @Override
+ public void finalize() {
+ Runtime.getRuntime().removeShutdownHook(shutdownCallback);
+ shutdownCallback.run();
+ }
+
/**
* Returns the list of span context decorators
*

Remove shutdown hook when DDTracer is shutdown

This is similar to the problem with weak map supplier and may lead to
a memory leak if we created/estroy many DDTracer objects.