Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom ConnectionFactory #530

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/org/datadog/jmxfetch/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ public class AppConfig {
*/
private ServiceNameProvider serviceNameProvider;

/**
* Controls how JMX connections are created.
*/
@Builder.Default
private ConnectionFactory connectionFactory = new DefaultConnectionFactory();

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

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

public ConnectionFactory getConnectionFactory() {
return connectionFactory;
}

/**
* @return Whether or not internal threads will be run as daemon.
*/
Expand Down
37 changes: 2 additions & 35 deletions src/main/java/org/datadog/jmxfetch/ConnectionFactory.java
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;
}
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);
}
}
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 @@ -424,11 +424,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
Loading