From 61f4a70d8ff41cfeead4d9b567c94a906803baaa Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Wed, 10 Apr 2019 16:49:00 +0200 Subject: [PATCH 1/2] Make JMXFetch know about its version number --- pom.xml | 6 +++++ src/main/java/org/datadog/jmxfetch/App.java | 24 +++++++++++++++++-- .../java/org/datadog/jmxfetch/AppConfig.java | 16 ++++++++++++- src/main/resources/project.properties | 1 + 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 src/main/resources/project.properties diff --git a/pom.xml b/pom.xml index 324bcc088..35f64757f 100644 --- a/pom.xml +++ b/pom.xml @@ -164,6 +164,12 @@ git@github.com:Datadog/jmxfetch.git + + + src/main/resources + true + + org.apache.maven.plugins diff --git a/src/main/java/org/datadog/jmxfetch/App.java b/src/main/java/org/datadog/jmxfetch/App.java index 9f657a348..814a19480 100644 --- a/src/main/java/org/datadog/jmxfetch/App.java +++ b/src/main/java/org/datadog/jmxfetch/App.java @@ -38,6 +38,7 @@ import java.util.ListIterator; import java.util.Map; import java.util.Map.Entry; +import java.util.Properties; import java.util.concurrent.CancellationException; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutionException; @@ -118,8 +119,14 @@ public static void main(String[] args) { System.exit(1); } + // Display the version and quit + if (config.isVersion() || AppConfig.ACTION_HELP.equals(config.getAction())) { + JCommander.getConsole().println("JMX Fetch " + getVersion()); + System.exit(0); + } + // Display the help and quit - if (config.isHelp() || config.getAction().equals(AppConfig.ACTION_HELP)) { + if (config.isHelp() || AppConfig.ACTION_HELP.equals(config.getAction())) { commander.usage(); System.exit(0); } @@ -127,6 +134,19 @@ public static void main(String[] args) { System.exit(run(config)); } + /** Returns our own version number. */ + public static String getVersion() { + try { + final Properties properties = new Properties(); + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + properties.load(classLoader.getResourceAsStream("project.properties")); + return properties.getProperty("version"); + } catch (IOException e) { + e.printStackTrace(); + return "?.?.?"; + } + } + /** * Main entry point of JMXFetch that returns integer on exit instead of calling {@code * System#exit}. @@ -164,7 +184,7 @@ public static int run(AppConfig config) { // Set up the shutdown hook to properly close resources attachShutdownHook(); - LOGGER.info("JMX Fetch has started"); + LOGGER.info("JMX Fetch " + getVersion() + " has started"); // set up the config status config.updateStatus(); diff --git a/src/main/java/org/datadog/jmxfetch/AppConfig.java b/src/main/java/org/datadog/jmxfetch/AppConfig.java index 13d156125..41a93087b 100644 --- a/src/main/java/org/datadog/jmxfetch/AppConfig.java +++ b/src/main/java/org/datadog/jmxfetch/AppConfig.java @@ -30,6 +30,7 @@ public class AppConfig { public static final String ACTION_LIST_NOT_MATCHING = "list_not_matching_attributes"; public static final String ACTION_LIST_LIMITED = "list_limited_attributes"; public static final String ACTION_HELP = "help"; + public static final String ACTION_VERSION = "version"; public static final HashSet ACTIONS = new HashSet( Arrays.asList( @@ -40,10 +41,10 @@ public class AppConfig { ACTION_LIST_NOT_MATCHING, ACTION_LIST_LIMITED, ACTION_HELP, + ACTION_VERSION, ACTION_LIST_JVMS)); private static final String AD_WIN_PIPE_PATH = "\\\\.\\pipe\\"; - private static final String AD_LEGACY_PIPE_NAME = "dd-service_discovery"; private static final String AD_PIPE_NAME = "dd-auto_discovery"; private static final String AD_LAUNCH_FILE = "jmx.launch"; @@ -57,6 +58,12 @@ public class AppConfig { help = true) private boolean help; + @Parameter( + names = {"--version", "-v"}, + description = "Display the version number and exit", + help = true) + private boolean version; + @Parameter( names = {"--log_level", "-L"}, description = "Level of verbosity", @@ -217,6 +224,9 @@ public String getStatusLocation() { } public String getAction() { + if (this.action == null || this.action.isEmpty()) { + return null; + } return this.action.get(0); } @@ -228,6 +238,10 @@ public boolean isHelp() { return help; } + public boolean isVersion() { + return version; + } + public Status getStatus() { return status; } diff --git a/src/main/resources/project.properties b/src/main/resources/project.properties new file mode 100644 index 000000000..defbd4820 --- /dev/null +++ b/src/main/resources/project.properties @@ -0,0 +1 @@ +version=${project.version} From a1ee115093f25827ae5ac5eb53cd71a4fe588af1 Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Mon, 15 Apr 2019 19:53:20 +0200 Subject: [PATCH 2/2] Missing javadoc --- src/main/java/org/datadog/jmxfetch/AppConfig.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/datadog/jmxfetch/AppConfig.java b/src/main/java/org/datadog/jmxfetch/AppConfig.java index 41a93087b..eeae07f1a 100644 --- a/src/main/java/org/datadog/jmxfetch/AppConfig.java +++ b/src/main/java/org/datadog/jmxfetch/AppConfig.java @@ -223,6 +223,7 @@ public String getStatusLocation() { return this.statusLocation; } + /** Returns the action parameter of the app if any, or null otherwise. */ public String getAction() { if (this.action == null || this.action.isEmpty()) { return null;