-
Notifications
You must be signed in to change notification settings - Fork 304
Add profiler env check command to AgentCLI #8671
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
229 changes: 229 additions & 0 deletions
229
.../agent-tooling/src/main/java/datadog/trace/agent/tooling/profiler/EnvironmentChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
package datadog.trace.agent.tooling.profiler; | ||
|
||
import datadog.trace.api.Platform; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.file.FileSystems; | ||
import java.nio.file.FileVisitResult; | ||
import java.nio.file.FileVisitor; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.nio.file.attribute.PosixFilePermissions; | ||
import java.util.Set; | ||
import java.util.jar.JarFile; | ||
|
||
public final class EnvironmentChecker { | ||
public static boolean checkEnvironment(String temp) { | ||
if (!Platform.isJavaVersionAtLeast(8)) { | ||
System.out.println("Profiler requires Java 8 or newer"); | ||
return false; | ||
} | ||
System.out.println( | ||
"Using Java version: " | ||
+ Platform.getRuntimeVersion() | ||
+ " (" | ||
+ System.getProperty("java.home") | ||
+ ")"); | ||
System.out.println("Running as user: " + System.getProperty("user.name")); | ||
boolean result = false; | ||
result |= checkJFR(); | ||
result |= checkDdprof(); | ||
if (!result) {; | ||
System.out.println("Profiler is not supported on this JVM."); | ||
return false; | ||
} else { | ||
System.out.println("Profiler is supported on this JVM."); | ||
} | ||
System.out.println(); | ||
if (!checkTempLocation(temp)) { | ||
System.out.println( | ||
"Profiler will not work properly due to issues with temp directory location."); | ||
return false; | ||
} else { | ||
if (!temp.equals(System.getProperty("java.io.tmpdir"))) { | ||
System.out.println( | ||
"! Make sure to add '-Ddd.profiling.tempdir=" + temp + "' to your JVM command line !"); | ||
} | ||
} | ||
System.out.println("Profiler is ready to be used."); | ||
return true; | ||
} | ||
|
||
private static boolean checkJFR() { | ||
if (Platform.isOracleJDK8()) { | ||
System.out.println( | ||
"JFR is commercial feature in Oracle JDK 8. Make sure you have the right license."); | ||
return true; | ||
} else if (Platform.isJ9()) { | ||
System.out.println("JFR is not supported on J9 JVM."); | ||
return false; | ||
} else { | ||
System.out.println("JFR is supported on " + Platform.getRuntimeVersion()); | ||
return true; | ||
} | ||
} | ||
|
||
private static boolean checkDdprof() { | ||
if (!Platform.isLinux()) { | ||
System.out.println("Datadog profiler is only supported on Linux."); | ||
return false; | ||
} else { | ||
System.out.println("Datadog profiler is supported on " + Platform.getRuntimeVersion()); | ||
return true; | ||
} | ||
} | ||
|
||
private static boolean checkTempLocation(String temp) { | ||
// Check if the temp directory is writable | ||
if (temp == null || temp.isEmpty()) { | ||
System.out.println("Temp directory is not specified."); | ||
return false; | ||
} | ||
|
||
System.out.println("Checking temporary directory: " + temp); | ||
|
||
Path base = Paths.get(temp); | ||
if (!Files.exists(base)) { | ||
System.out.println("Temporary directory does not exist: " + base); | ||
return false; | ||
} | ||
Path target = base.resolve("dd-profiler").normalize(); | ||
boolean rslt = true; | ||
Set<String> supportedViews = FileSystems.getDefault().supportedFileAttributeViews(); | ||
boolean isPosix = supportedViews.contains("posix"); | ||
try { | ||
if (isPosix) { | ||
Files.createDirectories( | ||
target, | ||
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------"))); | ||
} else { | ||
// non-posix, eg. Windows - let's rely on the created folders being world-writable | ||
Files.createDirectories(target); | ||
} | ||
System.out.println("Temporary directory is writable: " + target); | ||
rslt &= checkCreateTempFile(target); | ||
rslt &= checkLoadLibrary(target); | ||
} catch (Exception e) { | ||
System.out.println("Unable to create temp directory in location " + temp); | ||
if (isPosix) { | ||
try { | ||
System.out.println( | ||
"Base dir: " | ||
+ base | ||
+ " [" | ||
+ PosixFilePermissions.toString(Files.getPosixFilePermissions(base)) | ||
+ "]"); | ||
} catch (IOException ignored) { | ||
// never happens | ||
} | ||
} | ||
System.out.println("Error: " + e); | ||
} finally { | ||
if (Files.exists(target)) { | ||
try { | ||
Files.walkFileTree( | ||
target, | ||
new FileVisitor<Path>() { | ||
@Override | ||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) | ||
throws IOException { | ||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) | ||
throws IOException { | ||
Files.delete(file); | ||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult visitFileFailed(Path file, IOException exc) | ||
throws IOException { | ||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult postVisitDirectory(Path dir, IOException exc) | ||
throws IOException { | ||
Files.delete(dir); | ||
return FileVisitResult.CONTINUE; | ||
} | ||
}); | ||
} catch (IOException ignored) { | ||
// should never happen | ||
} | ||
} | ||
} | ||
return rslt; | ||
} | ||
|
||
private static boolean checkCreateTempFile(Path target) { | ||
// create a file to check if the directory is writable | ||
try { | ||
System.out.println("Attempting to create a test file in: " + target); | ||
Path testFile = target.resolve("testfile"); | ||
Files.createFile(testFile); | ||
System.out.println("Test file created: " + testFile); | ||
return true; | ||
} catch (Exception e) { | ||
System.out.println("Unable to create test file in temp directory " + target); | ||
System.out.println("Error: " + e); | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean checkLoadLibrary(Path target) { | ||
if (!Platform.isLinux()) { | ||
// we are loading the native library only on linux | ||
System.out.println("Skipping native library check on non-linux platform"); | ||
return true; | ||
} | ||
boolean rslt = true; | ||
try { | ||
rslt &= extractSoFromJar(target); | ||
if (rslt) { | ||
Path libFile = target.resolve("libjavaProfiler.so"); | ||
System.out.println("Attempting to load native library from: " + libFile); | ||
System.load(libFile.toString()); | ||
System.out.println("Native library loaded successfully"); | ||
} | ||
return true; | ||
} catch (Throwable t) { | ||
System.out.println("Unable to load native library in temp directory " + target); | ||
System.out.println("Error: " + t); | ||
return false; | ||
} | ||
} | ||
|
||
private static boolean extractSoFromJar(Path target) throws Exception { | ||
URL jarUrl = EnvironmentChecker.class.getProtectionDomain().getCodeSource().getLocation(); | ||
try (JarFile jarFile = new JarFile(new File(jarUrl.toURI()))) { | ||
return jarFile.stream() | ||
.filter(e -> e.getName().contains("libjavaProfiler.so")) | ||
.filter( | ||
e -> | ||
e.getName().contains(Platform.isAarch64() ? "/linux-arm64/" : "/linux-x64/") | ||
&& (!Platform.isMusl() || e.getName().contains("-musl"))) | ||
.findFirst() | ||
.map( | ||
e -> { | ||
try { | ||
Path soFile = target.resolve("libjavaProfiler.so"); | ||
Files.createDirectories(soFile.getParent()); | ||
Files.copy(jarFile.getInputStream(e), soFile); | ||
System.out.println("Native library extracted to: " + soFile); | ||
return true; | ||
} catch (Throwable t) { | ||
System.out.println("Failed to extract or load native library"); | ||
System.out.println("Error: " + t); | ||
} | ||
return false; | ||
}) | ||
.orElse(Boolean.FALSE); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will make it easier to diagnose these issue. Thanks!