-
Notifications
You must be signed in to change notification settings - Fork 323
Provide access to VM arguments via shared CLIHelper #8538
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 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a9d4400
Abstract away getVMArgumentsThroughReflection into a VMArgsCache class
mtoffl01 d688313
Change underlying data structure of VMArgsCache to HashSet
mtoffl01 d6344a8
Update internal-api/src/main/java/datadog/trace/bootstrap/config/prov…
mtoffl01 6aba3f8
Update internal-api/src/main/java/datadog/trace/bootstrap/config/prov…
mtoffl01 8afbec1
Update internal-api/src/main/java/datadog/trace/bootstrap/config/prov…
mtoffl01 d807b02
Clean up for initial review
mtoffl01 0603490
fix merge conflicts
mtoffl01 7ce4953
move VMArgsCache to components/cli
mtoffl01 7a9f694
fix datadog.cli import
mtoffl01 dd17ca2
nits: remove outdated comments
mtoffl01 2798bed
annotate initJvmArgs with @SuppressForbidden to allow Class.forName()…
mtoffl01 1d30309
Merge branch 'master' into mtoff/jvm-args
mtoffl01 2ca8d98
Merge remote-tracking branch 'origin/master' into mtoff/jvm-args
mcculls 253e5e4
Fix package directory
mcculls e67ae7a
Cleanup code:
mcculls b844607
Remove warning when reflection fails
mcculls 8837a21
Expose components:cli from internal-api
mcculls 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| plugins { | ||
| id("me.champeau.jmh") | ||
| } | ||
|
|
||
| apply(from = "$rootDir/gradle/java.gradle") | ||
|
|
||
| jmh { | ||
| version = "1.28" | ||
| } |
102 changes: 102 additions & 0 deletions
102
components/cli/src/main/java/datadog.cli/CLIHelper.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,102 @@ | ||
| package datadog.cli; | ||
|
|
||
| import de.thetaphi.forbiddenapis.SuppressForbidden; | ||
| import java.io.BufferedReader; | ||
| import java.io.FileReader; | ||
| import java.io.IOException; | ||
| import java.lang.management.ManagementFactory; | ||
| import java.lang.reflect.Field; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
|
|
||
| public class CLIHelper { | ||
| public static final CLIHelper ARGS = new CLIHelper(initJvmArgs()); | ||
|
|
||
| private final HashSet<String> args; | ||
|
|
||
| public CLIHelper(List<String> args) { | ||
| this.args = new HashSet<>(args); | ||
| } | ||
|
|
||
| public HashSet<String> getJvmArgs() { | ||
mcculls marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return this.args; | ||
| } | ||
|
|
||
| public boolean contains(String argument) { | ||
| return this.args.contains(argument); | ||
| } | ||
|
|
||
| @SuppressForbidden | ||
| private static List<String> initJvmArgs() { | ||
| // If linux OS, use procfs | ||
| // TODO: equals, or contains? | ||
| if (System.getProperty("os.name").equalsIgnoreCase("linux")) { | ||
| try { | ||
| // Get the JVM arguments from /proc/self/cmdline | ||
| return getJvmArgsFromProcCmdline(); | ||
| } catch (IOException e) { | ||
| // ignore exception, try other methods | ||
| } | ||
| } | ||
| // Try Oracle-based | ||
| // IBM Semeru Runtime 1.8.0_345-b01 will throw UnsatisfiedLinkError here. | ||
| try { | ||
| final Class<?> managementFactoryHelperClass = | ||
| Class.forName("sun.management.ManagementFactoryHelper"); | ||
|
|
||
| final Class<?> vmManagementClass = Class.forName("sun.management.VMManagement"); | ||
|
|
||
| Object vmManagement; | ||
|
|
||
| try { | ||
| vmManagement = | ||
| managementFactoryHelperClass.getDeclaredMethod("getVMManagement").invoke(null); | ||
| } catch (final NoSuchMethodException e) { | ||
| // Older vm before getVMManagement() existed | ||
| final Field field = managementFactoryHelperClass.getDeclaredField("jvm"); | ||
| field.setAccessible(true); | ||
| vmManagement = field.get(null); | ||
| field.setAccessible(false); | ||
| } | ||
|
|
||
| //noinspection unchecked | ||
| return (List<String>) vmManagementClass.getMethod("getVmArguments").invoke(vmManagement); | ||
| } catch (final ReflectiveOperationException | UnsatisfiedLinkError ignored) { | ||
| // Ignored exception | ||
| } | ||
|
|
||
| // Try IBM-based. | ||
| try { | ||
| final Class<?> VMClass = Class.forName("com.ibm.oti.vm.VM"); | ||
| final String[] argArray = (String[]) VMClass.getMethod("getVMArgs").invoke(null); | ||
| return Arrays.asList(argArray); | ||
| } catch (final ReflectiveOperationException ignored) { | ||
| // Ignored exception | ||
| } | ||
|
|
||
| // Fallback to default | ||
| try { | ||
| System.err.println( | ||
| "WARNING: Unable to get VM args through reflection. A custom java.util.logging.LogManager may not work correctly"); | ||
| return ManagementFactory.getRuntimeMXBean().getInputArguments(); | ||
| } catch (final Throwable t) { | ||
| // Throws InvocationTargetException on modularized applications | ||
| // with non-opened java.management module | ||
| System.err.println("WARNING: Unable to get VM args using managed beans"); | ||
| } | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| private static List<String> getJvmArgsFromProcCmdline() throws IOException { | ||
| BufferedReader argsReader = new BufferedReader(new FileReader("/proc/self/cmdline")); | ||
| String cmdLine = argsReader.readLine(); | ||
| if (cmdLine != null) { | ||
| // Return JVM arguments as a list of strings split by null characters | ||
| return Arrays.asList(cmdLine.split("\0")); | ||
mcculls marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.