|
| 1 | +package datadog.trace.bootstrap; |
| 2 | + |
| 3 | +import de.thetaphi.forbiddenapis.SuppressForbidden; |
| 4 | +import java.io.BufferedReader; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.io.InputStreamReader; |
| 7 | +import java.io.OutputStream; |
| 8 | +import java.io.PrintStream; |
| 9 | +import java.lang.instrument.Instrumentation; |
| 10 | +import java.lang.reflect.Method; |
| 11 | + |
| 12 | +/** Special lightweight pre-main class that skips installation on incompatible JVMs. */ |
| 13 | +public class AgentPreCheck { |
| 14 | + public static void premain(final String agentArgs, final Instrumentation inst) { |
| 15 | + agentmain(agentArgs, inst); |
| 16 | + } |
| 17 | + |
| 18 | + @SuppressForbidden |
| 19 | + public static void agentmain(final String agentArgs, final Instrumentation inst) { |
| 20 | + try { |
| 21 | + if (compatible()) { |
| 22 | + continueBootstrap(agentArgs, inst); |
| 23 | + } |
| 24 | + } catch (Throwable e) { |
| 25 | + // If agent failed we should not fail the application. |
| 26 | + // We don't have a log manager here, so just print. |
| 27 | + System.err.println("ERROR: " + e.getMessage()); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private static void reportIncompatibleJava( |
| 32 | + String javaVersion, String javaHome, String agentVersion, PrintStream output) { |
| 33 | + output.println( |
| 34 | + "Warning: " |
| 35 | + + (agentVersion == null ? "This version" : "Version " + agentVersion) |
| 36 | + + " of dd-java-agent is not compatible with Java " |
| 37 | + + javaVersion |
| 38 | + + " found at '" |
| 39 | + + javaHome |
| 40 | + + "' and is effectively disabled."); |
| 41 | + output.println("Please upgrade your Java version to 8+"); |
| 42 | + } |
| 43 | + |
| 44 | + static void sendTelemetry(String forwarderPath, String javaVersion, String agentVersion) { |
| 45 | + // Hardcoded payload for unsupported Java version. |
| 46 | + String payload = |
| 47 | + "{\"metadata\":{" |
| 48 | + + "\"runtime_name\":\"jvm\"," |
| 49 | + + "\"language_name\":\"jvm\"," |
| 50 | + + "\"runtime_version\":\"" |
| 51 | + + javaVersion |
| 52 | + + "\"," |
| 53 | + + "\"language_version\":\"" |
| 54 | + + javaVersion |
| 55 | + + "\"," |
| 56 | + + "\"tracer_version\":\"" |
| 57 | + + agentVersion |
| 58 | + + "\"}," |
| 59 | + + "\"points\":[{" |
| 60 | + + "\"name\":\"library_entrypoint.abort\"," |
| 61 | + + "\"tags\":[\"reason:incompatible_runtime\"]" |
| 62 | + + "}]" |
| 63 | + + "}"; |
| 64 | + |
| 65 | + ForwarderJsonSenderThread t = new ForwarderJsonSenderThread(forwarderPath, payload); |
| 66 | + t.setDaemon(true); |
| 67 | + t.start(); |
| 68 | + } |
| 69 | + |
| 70 | + private static String tryGetProperty(String property) { |
| 71 | + try { |
| 72 | + return System.getProperty(property); |
| 73 | + } catch (SecurityException e) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @SuppressForbidden |
| 79 | + private static boolean compatible() { |
| 80 | + String javaVersion = tryGetProperty("java.version"); |
| 81 | + String javaHome = tryGetProperty("java.home"); |
| 82 | + |
| 83 | + return compatible(javaVersion, javaHome, System.err); |
| 84 | + } |
| 85 | + |
| 86 | + // Reachable for testing |
| 87 | + static boolean compatible(String javaVersion, String javaHome, PrintStream output) { |
| 88 | + int majorJavaVersion = parseJavaMajorVersion(javaVersion); |
| 89 | + |
| 90 | + if (majorJavaVersion >= 8) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + |
| 94 | + String agentVersion = getAgentVersion(); |
| 95 | + |
| 96 | + reportIncompatibleJava(javaVersion, javaHome, agentVersion, output); |
| 97 | + |
| 98 | + String forwarderPath = System.getenv("DD_TELEMETRY_FORWARDER_PATH"); |
| 99 | + if (forwarderPath != null) { |
| 100 | + sendTelemetry(forwarderPath, javaVersion, agentVersion); |
| 101 | + } |
| 102 | + |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + // Reachable for testing |
| 107 | + static int parseJavaMajorVersion(String javaVersion) { |
| 108 | + int major = 0; |
| 109 | + if (javaVersion == null || javaVersion.isEmpty()) { |
| 110 | + return major; |
| 111 | + } |
| 112 | + |
| 113 | + int start = 0; |
| 114 | + if (javaVersion.charAt(0) == '1' |
| 115 | + && javaVersion.length() >= 3 |
| 116 | + && javaVersion.charAt(1) == '.' |
| 117 | + && Character.isDigit(javaVersion.charAt(2))) { |
| 118 | + start = 2; |
| 119 | + } |
| 120 | + |
| 121 | + // Parse the major digit and be a bit lenient, allowing digits followed by any non digit |
| 122 | + for (int i = start; i < javaVersion.length(); i++) { |
| 123 | + char c = javaVersion.charAt(i); |
| 124 | + if (Character.isDigit(c)) { |
| 125 | + major *= 10; |
| 126 | + major += Character.digit(c, 10); |
| 127 | + } else { |
| 128 | + break; |
| 129 | + } |
| 130 | + } |
| 131 | + return major; |
| 132 | + } |
| 133 | + |
| 134 | + private static String getAgentVersion() { |
| 135 | + try { |
| 136 | + // Get the resource as an InputStream |
| 137 | + InputStream is = AgentPreCheck.class.getResourceAsStream("/dd-java-agent.version"); |
| 138 | + if (is == null) { |
| 139 | + return null; |
| 140 | + } |
| 141 | + |
| 142 | + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); |
| 143 | + final StringBuilder sb = new StringBuilder(); |
| 144 | + for (int c = reader.read(); c != -1; c = reader.read()) { |
| 145 | + sb.append((char) c); |
| 146 | + } |
| 147 | + reader.close(); |
| 148 | + |
| 149 | + return sb.toString().trim(); |
| 150 | + } catch (Throwable e) { |
| 151 | + return null; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + @SuppressForbidden |
| 156 | + private static void continueBootstrap(final String agentArgs, final Instrumentation inst) |
| 157 | + throws Exception { |
| 158 | + Class<?> clazz = Class.forName("datadog.trace.bootstrap.AgentBootstrap"); |
| 159 | + Method agentMain = clazz.getMethod("agentmain", String.class, Instrumentation.class); |
| 160 | + agentMain.invoke(null, agentArgs, inst); |
| 161 | + } |
| 162 | + |
| 163 | + public static final class ForwarderJsonSenderThread extends Thread { |
| 164 | + private final String forwarderPath; |
| 165 | + private final String payload; |
| 166 | + |
| 167 | + public ForwarderJsonSenderThread(String forwarderPath, String payload) { |
| 168 | + super("dd-forwarder-json-sender"); |
| 169 | + setDaemon(true); |
| 170 | + this.forwarderPath = forwarderPath; |
| 171 | + this.payload = payload; |
| 172 | + } |
| 173 | + |
| 174 | + @SuppressForbidden |
| 175 | + @Override |
| 176 | + public void run() { |
| 177 | + ProcessBuilder builder = new ProcessBuilder(forwarderPath, "library_entrypoint"); |
| 178 | + try { |
| 179 | + Process process = builder.start(); |
| 180 | + OutputStream out = null; |
| 181 | + try { |
| 182 | + out = process.getOutputStream(); |
| 183 | + out.write(payload.getBytes()); |
| 184 | + } finally { |
| 185 | + if (out != null) { |
| 186 | + out.close(); |
| 187 | + } |
| 188 | + } |
| 189 | + } catch (Throwable e) { |
| 190 | + System.err.println("Failed to send telemetry: " + e.getMessage()); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments