Remove unnecessary and complex logic
MODIFY
javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/OpenTelemetryAgent.java
javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/OpenTelemetryAgent.java
@@ -9,16 +9,10 @@ import io.opentelemetry.javaagent.bootstrap.AgentInitializer;
import java.io.File;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
-import java.lang.management.ManagementFactory;
-import java.lang.reflect.Field;
import java.net.URISyntaxException;
import java.security.CodeSource;
-import java.util.Arrays;
-import java.util.List;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
/**
* Premain-Class for the OpenTelemetry Java agent.
@@ -44,7 +38,6 @@ import java.util.regex.Pattern;
// Too early for logging
@SuppressWarnings("SystemOut")
public final class OpenTelemetryAgent {
- private static final Class<?> thisClass = OpenTelemetryAgent.class;
public static void premain(String agentArgs, Instrumentation inst) {
agentmain(agentArgs, inst);
@@ -56,7 +49,7 @@ public final class OpenTelemetryAgent {
AgentInitializer.initialize(inst, javaagentFile);
} catch (Throwable ex) {
// Don't rethrow. We don't have a log manager here, so just print.
- System.err.println("ERROR " + thisClass.getName());
+ System.err.println("ERROR " + OpenTelemetryAgent.class.getName());
ex.printStackTrace();
}
}
@@ -64,109 +57,28 @@ public final class OpenTelemetryAgent {
private static synchronized File installBootstrapJar(Instrumentation inst)
throws IOException, URISyntaxException {
- // First try Code Source
- CodeSource codeSource = thisClass.getProtectionDomain().getCodeSource();
+ CodeSource codeSource = OpenTelemetryAgent.class.getProtectionDomain().getCodeSource();
- if (codeSource != null) {
- File javaagentFile = new File(codeSource.getLocation().toURI());
-
- if (javaagentFile.isFile()) {
- // passing verify false for vendors who sign the agent jar, because jar file signature
- // verification is very slow before the JIT compiler starts up, which on Java 8 is not until
- // after premain executes
- JarFile agentJar = new JarFile(javaagentFile, false);
- verifyJarManifestMainClassIsThis(javaagentFile, agentJar);
- inst.appendToBootstrapClassLoaderSearch(agentJar);
- return javaagentFile;
- }
- }
-
- System.out.println("Could not get bootstrap jar from code source, using -javaagent arg");
-
- // ManagementFactory indirectly references java.util.logging.LogManager
- // - On Oracle-based JDKs after 1.8
- // - On IBM-based JDKs since at least 1.7
- // This prevents custom log managers from working correctly
- // Use reflection to bypass the loading of the class
- List<String> arguments = getVmArgumentsThroughReflection();
-
- String agentArgument = null;
- for (String arg : arguments) {
- if (arg.startsWith("-javaagent")) {
- if (agentArgument == null) {
- agentArgument = arg;
- } else {
- throw new IllegalStateException(
- "Multiple javaagents specified and code source unavailable, "
- + "not installing tracing agent");
- }
- }
- }
-
- if (agentArgument == null) {
- throw new IllegalStateException(
- "Could not find javaagent parameter and code source unavailable, "
- + "not installing tracing agent");
+ if (codeSource == null) {
+ throw new IllegalStateException("could not get agent jar location");
}
- // argument is of the form -javaagent:/path/to/java-agent.jar=optionalargumentstring
- Matcher matcher = Pattern.compile("-javaagent:([^=]+).*").matcher(agentArgument);
-
- if (!matcher.matches()) {
- throw new IllegalStateException("Unable to parse javaagent parameter: " + agentArgument);
- }
+ File javaagentFile = new File(codeSource.getLocation().toURI());
- File javaagentFile = new File(matcher.group(1));
if (!javaagentFile.isFile()) {
- throw new IllegalStateException("Unable to find javaagent file: " + javaagentFile);
+ throw new IllegalStateException(
+ "agent jar location doesn't appear to be a file: " + javaagentFile.getAbsolutePath());
}
+ // passing verify false for vendors who sign the agent jar, because jar file signature
+ // verification is very slow before the JIT compiler starts up, which on Java 8 is not until
+ // after premain executes
JarFile agentJar = new JarFile(javaagentFile, false);
verifyJarManifestMainClassIsThis(javaagentFile, agentJar);
inst.appendToBootstrapClassLoaderSearch(agentJar);
return javaagentFile;
}
- private static List<String> getVmArgumentsThroughReflection() {
- ClassLoader classLoader = ClassLoader.getSystemClassLoader();
- try {
- // Try Oracle-based
- Class managementFactoryHelperClass =
- classLoader.loadClass("sun.management.ManagementFactoryHelper");
-
- Class vmManagementClass = classLoader.loadClass("sun.management.VMManagement");
-
- Object vmManagement;
-
- try {
- vmManagement =
- managementFactoryHelperClass.getDeclaredMethod("getVMManagement").invoke(null);
- } catch (NoSuchMethodException e) {
- // Older vm before getVMManagement() existed
- Field field = managementFactoryHelperClass.getDeclaredField("jvm");
- field.setAccessible(true);
- vmManagement = field.get(null);
- field.setAccessible(false);
- }
-
- return (List<String>) vmManagementClass.getMethod("getVmArguments").invoke(vmManagement);
-
- } catch (ReflectiveOperationException e) {
- try { // Try IBM-based.
- Class vmClass = classLoader.loadClass("com.ibm.oti.vm.VM");
- String[] argArray = (String[]) vmClass.getMethod("getVMArgs").invoke(null);
- return Arrays.asList(argArray);
- } catch (ReflectiveOperationException e1) {
- // Fallback to default
- System.out.println(
- "WARNING: Unable to get VM args through reflection. "
- + "A custom java.util.logging.LogManager may not work correctly");
-
- return ManagementFactory.getRuntimeMXBean().getInputArguments();
- }
- }
- }
-
// this protects against the case where someone adds the contents of opentelemetry-javaagent.jar
// by mistake to their application's "uber.jar"
//

Remove unnecessary and complex logic (#3395)