8000 Cleanup of zstd dependency and jps functionality by jbachorik · Pull Request #9010 · DataDog/dd-trace-java · GitHub
[go: up one dir, main page]

Skip to content

Cleanup of zstd dependency and jps functionality #9010

8000
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify the JVMs pids collection
  • Loading branch information
jbachorik committed Jun 19, 2025
commit 25877356d80cff2b1c96e6b956fdc7ab316c69f7
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static datadog.trace.api.ConfigDefaults.DEFAULT_STARTUP_LOGS_ENABLED;
import static datadog.trace.api.Platform.isJavaVersionAtLeast;
import static datadog.trace.api.Platform.isOracleJDK8;
import static datadog.trace.api.telemetry.LogCollector.SEND_TELEMETRY;
import static datadog.trace.bootstrap.Library.WILDFLY;
import static datadog.trace.bootstrap.Library.detectLibraries;
import static datadog.trace.util.AgentThreadFactory.AgentThread.JMX_STARTUP;
Expand Down Expand Up @@ -45,7 +44,6 @@
import datadog.trace.util.AgentTaskScheduler;
import datadog.trace.util.AgentThreadFactory.AgentThread;
import datadog.trace.util.throwable.FatalAgentMisconfigurationError;
import de.thetaphi.forbiddenapis.SuppressForbidden;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -291,8 +289,6 @@ public static void start(
codeOriginEnabled = isFeatureEnabled(AgentFeature.CODE_ORIGIN);
agentlessLogSubmissionEnabled = isFeatureEnabled(AgentFeature.AGENTLESS_LOG_SUBMISSION);

patchJPSAccess(inst);

if (profilingEnabled) {
if (!isOracleJDK8()) {
// Profiling agent startup code is written in a way to allow `startProfilingAgent` be called
Expand Down Expand Up @@ -422,23 +418,6 @@ private static void injectAgentArgsConfig(String agentArgs) {
}
}

@SuppressForbidden
public static void patchJPSAccess(Instrumentation inst) {
if (Platform.isJavaVersionAtLeast(9)) {
// Unclear if supported for J9, may need to revisit
try {
Class.forName("datadog.trace.util.JPMSJPSAccess")
.getMethod("patchModuleAccess", Instrumentation.class)
.invoke(null, inst);
} catch (Exception e) {
log.debug(
SEND_TELEMETRY,
"Failed to patch module access for jvmstat and Java version "
+ Platform.getRuntimeVersion());
}
}
}

public static void shutdown(final boolean sync) {
StaticEventLogger.end("Agent");
StaticEventLogger.stop();
Expand Down

This file was deleted.

This file was deleted.

25 changes: 0 additions & 25 deletions internal-api/src/main/java/datadog/trace/util/JPSUtils.java

This file was deleted.

73 changes: 13 additions & 60 deletions internal-api/src/main/java/datadog/trace/util/PidHelper.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
package datadog.trace.util;

import datadog.trace.api.Platform;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.context.TraceScope;
import de.thetaphi.forbiddenapis.SuppressForbidden;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -39,6 +33,7 @@ public static long getPidAsLong() {
return PID_AS_LONG;
}

@SuppressWarnings("unchecked")
private static String findPid() {
String pid = "";
if (Platform.isJavaVersionAtLeast(9)) {
Expand Down Expand Up @@ -123,14 +118,6 @@ private static Path getJavaProcessesDir() {
}

public static Set<String> getJavaPids() {
// Attempt to use jvmstat directly, fall through to jps process fork strategy
Set<String> directlyObtainedPids = JPSUtils.getVMPids();
if (directlyObtainedPids != null) {
return directlyObtainedPids;
}

// Some JDKs don't have jvmstat available as a module, attempt to read from the hsperfdata
// directory instead
try (Stream<Path> stream = Files.list(getJavaProcessesDir())) {
return stream
.map(Path::getFileName)
Expand All @@ -145,56 +132,22 @@ public static Set<String> getJavaPids() {
if (name.isEmpty()) {
return false;
}
for (int i = 0; i < name.length(); i++) {
if (!Character.isDigit(name.charAt(i))) {
return false;
}
char c = name.charAt(0);
if (c < '0' || c > '9') {
// Short-circuit - let's not parse as long something that is definitely not a long
// number
return false;
}
long pid = -1;
try {
pid = Long.parseLong(name);
} catch (NumberFormatException ignored) {
}
return true;
return pid != -1;
})
.collect(Collectors.toSet());
} catch (IOException e) {
log.debug("Unable to obtain Java PIDs via hsperfdata", e);
}

// there is no supported Java API to achieve this
// one could use sun.jvmstat.monitor.MonitoredHost but it is an internal API and can go away at
// any time -
// also, no guarantee it will work with all JVMs
ProcessBuilder pb = new ProcessBuilder("jps");
try (TraceScope ignored = AgentTracer.get().muteTracing()) {
Process p = pb.start();
// start draining the subcommand's pipes asynchronously to avoid flooding them
CompletableFuture<Set<String>> collecting =
CompletableFuture.supplyAsync(
() -> {
try (BufferedReader br =
new BufferedReader(new InputStreamReader(p.getInputStream()))) {
return br.lines()
.filter(l -> !l.contains("jps"))
.map(
l -> {
int idx = l.indexOf(' ');
return l.substring(0, idx);
})
.collect(java.util.stream.Collectors.toSet());
} catch (IOException e) {
log.debug("Unable to list java processes via 'jps'", e);
return Collections.emptySet();
}
});
if (p.waitFor(1200, TimeUnit.MILLISECONDS)) {
if (p.exitValue() == 0) {
return collecting.get();
} else {
log.debug("Execution of 'jps' failed with exit code {}", p.exitValue());
}
} else {
p.destroyForcibly();
log.debug("Execution of 'jps' timed out");
}
} catch (Exception e) {
log.debug("Unable to list java processes via 'jps'", e);
log.debug("Unable to obtain Java PIDs", e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Might be worth to keep mentioning the source of the pid i.e. hsperfdata, so support can look why this didn't work.

}
return Collections.emptySet();
}
Expand Down
Loading
0