|
8 | 8 | import java.io.OutputStream;
|
9 | 9 | import java.io.PrintStream;
|
10 | 10 | import java.io.StringWriter;
|
| 11 | +import java.lang.management.ManagementFactory; |
11 | 12 | import java.net.HttpURLConnection;
|
12 | 13 | import java.net.URL;
|
13 | 14 | import java.net.URLConnection;
|
@@ -1261,4 +1262,78 @@ static public String getTraceParentHeader(Span span)
|
1261 | 1262 |
|
1262 | 1263 | return traceparent;
|
1263 | 1264 | }
|
| 1265 | + |
| 1266 | + /** |
| 1267 | + * Checks if a specified VM argument is present. |
| 1268 | + * |
| 1269 | + * This method retrieves the list of VM arguments and searches for the specified argument name. |
| 1270 | + * If the argument is found, it returns true. If the argument is not found, it returns false. |
| 1271 | + * |
| 1272 | + * @param vmArgName the name of the VM argument to search for |
| 1273 | + * @return {@code true} if the specified VM argument is present, {@code false} otherwise |
| 1274 | + */ |
| 1275 | + static public boolean containsVMArgument(String vmArgName) |
| 1276 | + { |
| 1277 | + List<String> argslist = ManagementFactory.getRuntimeMXBean().getInputArguments(); |
| 1278 | + for (String string : argslist) |
| 1279 | + { |
| 1280 | + if(string.matches("(?i)(" + vmArgName + "):.*")) |
| 1281 | + { |
| 1282 | + return true; |
| 1283 | + } |
| 1284 | + } |
| 1285 | + return false; |
| 1286 | + } |
| 1287 | + |
| 1288 | + /** |
| 1289 | + * Fetches the value of a specified VM argument. |
| 1290 | + * |
| 1291 | + * This method retrieves the list of VM arguments and searches for the specified argument name. |
| 1292 | + * If the argument is found, it returns the value associated with it. If the argument is not found, |
| 1293 | + * it returns an empty string. |
| 1294 | + * |
| 1295 | + * @param vmArgName the name of the VM argument to search for |
| 1296 | + * @return the value of the specified VM argument, or an empty string if the argument is not found |
| 1297 | + */ |
| 1298 | + static public String fetchVMArgument(String vmArgName) |
| 1299 | + { |
| 1300 | + List<String> argslist = ManagementFactory.getRuntimeMXBean().getInputArguments(); |
| 1301 | + for (String string : argslist) |
| 1302 | + { |
| 1303 | + if(string.matches("(?i)(" + vmArgName + "):.*")) |
| 1304 | + { |
| 1305 | + String[] keyval = string.split(vmArgName+":"); |
| 1306 | + |
| 1307 | + return keyval[1]; |
| 1308 | + } |
| 1309 | + } |
| 1310 | + return ""; |
| 1311 | + } |
| 1312 | + |
| 1313 | + /** |
| 1314 | + * Checks if the OpenTelemetry Java agent is used by inspecting the VM arguments. |
| 1315 | + * |
| 1316 | + * This method fetches the VM argument specified by "-javaagent" and checks if it contains |
| 1317 | + * the term "opentelemetry". If the argument is found and contains "opentelemetry", it returns true. |
| 1318 | + * Otherwise, it returns false. |
| 1319 | + * |
| 1320 | + * @return {@code true} if the OpenTelemetry Java agent is detected, {@code false} otherwise. |
| 1321 | + */ |
| 1322 | + static public boolean isOtelJavaagentUsed() |
| 1323 | + { |
| 1324 | + String javaAgentPath = fetchVMArgument("-javaagent"); |
| 1325 | + if (!javaAgentPath.isEmpty()) |
| 1326 | + { |
| 1327 | + System.out.println("javaagent VM argument detected: " + javaAgentPath); |
| 1328 | + |
| 1329 | + File jaFile = new File(javaAgentPath); |
| 1330 | + |
| 1331 | + if (jaFile.getName().contains("opentelemetry") || jaFile.getName().contains("otel")) |
| 1332 | + { |
| 1333 | + System.out.println("Otel javaagent argument detected: " + javaAgentPath); |
| 1334 | + return true; |
| 1335 | + } |
| 1336 | + } |
| 1337 | + return false; |
| 1338 | + } |
1264 | 1339 | }
|
0 commit comments