-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support custom ConnectionFactory (#530)
- Loading branch information
Showing
4 changed files
with
56 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,42 +1,9 @@ | ||
package org.datadog.jmxfetch; | ||
|
||
import static org.datadog.jmxfetch.Instance.isDirectInstance; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
/** Singleton used to create connections to the MBeanServer. */ | ||
@Slf4j | ||
public class ConnectionFactory { | ||
public static final String PROCESS_NAME_REGEX = "process_name_regex"; | ||
|
||
public interface ConnectionFactory { | ||
/** Factory method to create connections, both remote and local to the target JVM. */ | ||
public static Connection createConnection(Map<String, Object> connectionParams) | ||
throws IOException { | ||
// This is used by dd-java-agent to enable directly connecting to the mbean server. | ||
// This works since jmxfetch is being run as a library inside the process being monitored. | ||
if (isDirectInstance(connectionParams)) { | ||
log.info("Connecting to JMX directly on the JVM"); | ||
return new JvmDirectConnection(); | ||
} | ||
|
||
if (connectionParams.get(PROCESS_NAME_REGEX) != null) { | ||
try { | ||
// AttachNotSupportedException is accessible in java 7 and 8 through tools.jar | ||
// and java 9+ by default | ||
Class.forName("com.sun.tools.attach.AttachNotSupportedException"); | ||
} catch (ClassNotFoundException e) { | ||
throw new IOException( | ||
"Unable to find tools.jar." | ||
+ " Are you using a JDK and did you set the path to tools.jar ?"); | ||
} | ||
log.info("Connecting using Attach API"); | ||
return new AttachApiConnection(connectionParams); | ||
} | ||
|
||
log.info("Connecting using JMX Remote"); | ||
return new RemoteConnection(connectionParams); | ||
} | ||
Connection createConnection(Map<String, Object> connectionParams) throws IOException; | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/org/datadog/jmxfetch/DefaultConnectionFactory.java
This file contains 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,42 @@ | ||
package org.datadog.jmxfetch; | ||
|
||
import static org.datadog.jmxfetch.Instance.isDirectInstance; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
/** Singleton used to create connections to the MBeanServer. */ | ||
@Slf4j | ||
public class DefaultConnectionFactory implements ConnectionFactory { | ||
public static final String PROCESS_NAME_REGEX = "process_name_regex"; | ||
|
||
/** Factory method to create connections, both remote and local to the target JVM. */ | ||
public Connection createConnection(Map<String, Object> connectionParams) | ||
throws IOException { | ||
// This is used by dd-java-agent to enable directly connecting to the mbean server. | ||
// This works since jmxfetch is being run as a library inside the process being monitored. | ||
if (isDirectInstance(connectionParams)) { | ||
log.info("Connecting to JMX directly on the JVM"); | ||
return new JvmDirectConnection(); | ||
} | ||
|
||
if (connectionParams.get(PROCESS_NAME_REGEX) != null) { | ||
try { | ||
// AttachNotSupportedException is accessible in java 7 and 8 through tools.jar | ||
// and java 9+ by default | ||
Class.forName("com.sun.tools.attach.AttachNotSupportedException"); | ||
} catch (ClassNotFoundException e) { | ||
throw new IOException( | ||
"Unable to find tools.jar." | ||
+ " Are you using a JDK and did you set the path to tools.jar ?"); | ||
} | ||
log.info("Connecting using Attach API"); | ||
return new AttachApiConnection(connectionParams); | ||
} | ||
|
||
log.info("Connecting using JMX Remote"); | ||
return new RemoteConnection(connectionParams); | ||
} | ||
} |
This file contains 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