Skip to content

Commit

Permalink
Support custom ConnectionFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
mcculls committed Jun 24, 2024
1 parent ae35f9f commit 220239a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 35 deletions.
9 changes: 9 additions & 0 deletions src/main/java/org/datadog/jmxfetch/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ public class AppConfig {
*/
private ServiceNameProvider serviceNameProvider;

/**
* Controls how JMX connections are created.
*/
private ConnectionFactory connectionFactory;

@Builder.Default
private Status status = new Status();

Expand Down Expand Up @@ -475,6 +480,10 @@ public ServiceNameProvider getServiceNameProvider() {
return serviceNameProvider;
}

public ConnectionFactory getConnectionFactory() {
return connectionFactory;
}

/**
* @return Whether or not internal threads will be run as daemon.
*/
Expand Down
35 changes: 2 additions & 33 deletions src/main/java/org/datadog/jmxfetch/ConnectionFactory.java
Original file line number Diff line number Diff line change
@@ -1,40 +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 {
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;
}
40 changes: 40 additions & 0 deletions src/main/java/org/datadog/jmxfetch/DefaultConnectionFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 {
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);
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/datadog/jmxfetch/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,11 @@ public Connection getConnection(
log.info(
"Connection closed or does not exist. "
+ "Attempting to create a new connection...");
return ConnectionFactory.createConnection(connectionParams);
return appConfig.getConnectionFactory().createConnection(connectionParams);
} else if (forceNewConnection) {
log.info("Forcing a new connection, attempting to create...");
connection.closeConnector();
return ConnectionFactory.createConnection(connectionParams);
return appConfig.getConnectionFactory().createConnection(connectionParams);
}
return connection;
}
Expand Down

0 comments on commit 220239a

Please sign in to comment.