Skip to content

Commit

Permalink
[attach_api] reword exception message on failures
Browse files Browse the repository at this point in the history
Reword the exception message when unable to find any JVM matching the
regular expression.
  • Loading branch information
yannmh committed Oct 26, 2016
1 parent 8542d40 commit a823eda
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/main/java/org/datadog/jmxfetch/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ public static void main(String[] args) {
System.exit(1);
}

if( config.getAction().equals( AppConfig.ACTION_LIST_JVMS )) {
if(config.getAction().equals(AppConfig.ACTION_LIST_JVMS)) {
List<com.sun.tools.attach.VirtualMachineDescriptor> descriptors = com.sun.tools.attach.VirtualMachine.list();

System.out.println("List of JVMs for user " + System.getProperty("user.name") );
for( com.sun.tools.attach.VirtualMachineDescriptor descriptor : descriptors ) {
for(com.sun.tools.attach.VirtualMachineDescriptor descriptor : descriptors) {
System.out.println( "\tJVM id " + descriptor.id() + ": '" + descriptor.displayName() + "'" );
}
System.exit(0);
Expand Down Expand Up @@ -368,7 +369,7 @@ public void init(boolean forceNewConnection) {
} catch (IOException e) {
instance.cleanUp();
brokenInstances.add(instance);
String warning = CANNOT_CONNECT_TO_INSTANCE + instance + " " + e.getMessage();
String warning = CANNOT_CONNECT_TO_INSTANCE + instance + ". " + e.getMessage();
this.reportStatus(appConfig, reporter, instance, 0, warning, Status.STATUS_ERROR);
this.sendServiceCheck(reporter, instance, warning, Status.STATUS_ERROR);
LOGGER.error(warning);
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/org/datadog/jmxfetch/AttachApiConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.management.remote.JMXServiceURL;

Expand Down Expand Up @@ -35,11 +33,9 @@ private JMXServiceURL getAddress(LinkedHashMap<String, Object> connectionParams)
}

private String getJMXUrlForProcessRegex(String processRegex) throws com.sun.tools.attach.AttachNotSupportedException, IOException {
List<String> jvms = new ArrayList<String>();
for (com.sun.tools.attach.VirtualMachineDescriptor vmd : com.sun.tools.attach.VirtualMachine.list()) {
if (vmd.displayName().matches(processRegex)) {
com.sun.tools.attach.VirtualMachine vm = com.sun.tools.attach.VirtualMachine.attach(vmd);
LOGGER.info("Matched JVM '" + vmd.displayName() + "' against regex '" + processRegex + "'");
String connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);
//If jmx agent is not running in VM, load it and return the connector url
if (connectorAddress == null) {
Expand All @@ -52,10 +48,9 @@ private String getJMXUrlForProcessRegex(String processRegex) throws com.sun.tool

return connectorAddress;
}
jvms.add( vmd.displayName() );
}

throw new IOException("Cannot find JVM matching regex: '" + processRegex + "'; available JVMs (for this user account): " + jvms );
throw new IOException("No match found. Available JVMs can be listed with the `list_jvms` command.");
}

private void loadJMXAgent(com.sun.tools.attach.VirtualMachine vm) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/datadog/jmxfetch/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void init(boolean forceNewConnection) throws IOException, FailedLoginExce
@Override
public String toString() {
if (this.yaml.get(PROCESS_NAME_REGEX) != null) {
return "process_regex: " + this.yaml.get(PROCESS_NAME_REGEX);
return "process_regex: `" + this.yaml.get(PROCESS_NAME_REGEX) + "`";
} else if (this.yaml.get("jmx_url") != null) {
return (String) this.yaml.get("jmx_url");
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/datadog/jmxfetch/TestServiceChecks.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void testServiceCheckCRITICAL() throws Exception {

assertEquals(Reporter.formatServiceCheckPrefix("non_running_process"), scName);
assertEquals(Status.STATUS_ERROR, scStatus);
assertTrue( scMessage, scMessage.startsWith("Cannot connect to instance process_regex: .*non_running_process_test.* Cannot find JVM matching regex: '.*non_running_process_test.*'; available JVMs (for this user account): "));
assertEquals("Cannot connect to instance process_regex: `.*non_running_process_test.*`. No match found. Available JVMs can be listed with the `list_jvms` command.", scMessage);
assertEquals(scTags.length, 3);
assertTrue(Arrays.asList(scTags).contains("instance:jmx_test_instance"));

Expand All @@ -137,7 +137,7 @@ public void testServiceCheckCRITICAL() throws Exception {

assertEquals(Reporter.formatServiceCheckPrefix("non_running_process"), scName);
assertEquals(Status.STATUS_ERROR, scStatus);
assertEquals("Cannot connect to instance process_regex: .*non_running_process_test.*. Is a JMX Server running at this address?", scMessage);
assertEquals("Cannot connect to instance process_regex: `.*non_running_process_test.*`. Is a JMX Server running at this address?", scMessage);
assertEquals(scTags.length, 3);
assertTrue(Arrays.asList(scTags).contains("instance:jmx_test_instance"));
assertTrue(Arrays.asList(scTags).contains("env:stage"));
Expand Down

0 comments on commit a823eda

Please sign in to comment.