Skip to content

Commit

Permalink
Merge pull request #218 from DataDog/albertvaka/version-command
Browse files Browse the repository at this point in the history
Make JMXFetch know about its version number
  • Loading branch information
albertvaka authored Apr 29, 2019
2 parents 3c8296b + a1ee115 commit 723058d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@
<url>[email protected]:Datadog/jmxfetch.git</url>
</scm>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/org/datadog/jmxfetch/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -118,15 +119,34 @@ 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);
}

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}.
Expand Down Expand Up @@ -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();
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/org/datadog/jmxfetch/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> ACTIONS =
new HashSet<String>(
Arrays.asList(
Expand All @@ -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";

Expand All @@ -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",
Expand Down Expand Up @@ -216,7 +223,11 @@ 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;
}
return this.action.get(0);
}

Expand All @@ -228,6 +239,10 @@ public boolean isHelp() {
return help;
}

public boolean isVersion() {
return version;
}

public Status getStatus() {
return status;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version=${project.version}

0 comments on commit 723058d

Please sign in to comment.