Skip to content
Merged
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
23 changes: 23 additions & 0 deletions internal-api/src/main/java/datadog/trace/util/PidHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -70,6 +75,24 @@ public static Set<String> getJavaPids() {
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(
Paths.get(
System.getProperty("java.io.tmpdir")
+ "hsperfdata_"
+ System.getProperty("user.name")))) {
return stream
.filter(file -> !Files.isDirectory(file))
.map(Path::getFileName)
.map(Path::toString)
.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 -
Expand Down
Loading