Skip to content

Commit

Permalink
Add shutdown hook to finish the spark application trace (#7357)
Browse files Browse the repository at this point in the history
Add shutdown hook to finish the application span from a shutdown hook, in case where the JVM is shutdown using System.exit(code)
  • Loading branch information
paul-laffon-dd authored Aug 2, 2024
1 parent 0a5a92f commit 42654e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.api.SpanLink;
import datadog.trace.util.AgentThreadFactory;
import de.thetaphi.forbiddenapis.SuppressForbidden;
import java.io.PrintWriter;
import java.io.StringWriter;
Expand Down Expand Up @@ -111,7 +112,7 @@ public abstract class AbstractDatadogSparkListener extends SparkListener {
private int maxExecutorCount = 0;
private long availableExecutorTime = 0;

private boolean applicationEnded = false;
private volatile boolean applicationEnded = false;

public AbstractDatadogSparkListener(SparkConf sparkConf, String appId, String sparkVersion) {
tracer = AgentTracer.get();
Expand All @@ -125,6 +126,22 @@ public AbstractDatadogSparkListener(SparkConf sparkConf, String appId, String sp
databricksServiceName = getDatabricksServiceName(sparkConf, databricksClusterName);
sparkServiceName = getSparkServiceName(sparkConf, isRunningOnDatabricks);

// If JVM exiting with System.exit(code), it bypass the code closing the application span
//
// Using shutdown hook to close the span, but it is only best effort:
// - no guarantee it will be executed before the tracer shuts down
// - no access to the exit code
Runtime.getRuntime()
.addShutdownHook(
AgentThreadFactory.newAgentThread(
AgentThreadFactory.AgentThread.DATA_JOBS_MONITORING_SHUTDOWN_HOOK,
() -> {
if (!applicationEnded) {
log.info("Finishing application trace from shutdown hook");
finishApplication(System.currentTimeMillis(), null, 0, null);
}
}));

log.info("Created datadog spark listener: {}", this.getClass().getSimpleName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public enum AgentThread {

PROCESS_SUPERVISOR("dd-process-supervisor"),

DATA_JOBS_MONITORING_SHUTDOWN_HOOK("dd-data-jobs-shutdown-hook"),

DATA_STREAMS_MONITORING("dd-data-streams-monitor"),

DEBUGGER_SNAPSHOT_SERIALIZER("dd-debugger-snapshot-serializer"),
Expand Down

0 comments on commit 42654e4

Please sign in to comment.