You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by GitBox <gi...@apache.org> on 2021/06/05 16:42:46 UTC

[GitHub] [skywalking] muse-dev[bot] commented on a change in pull request #7069: Agent supports the collection of JVM arguments and jar dependencies information.

muse-dev[bot] commented on a change in pull request #7069:
URL: https://github.com/apache/skywalking/pull/7069#discussion_r646010973



##########
File path: apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/jvm/JVMUtil.java
##########
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.apm.agent.core.jvm;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import java.io.File;
+import java.io.IOException;
+import java.lang.instrument.Instrumentation;
+import java.lang.management.ManagementFactory;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import org.apache.skywalking.apm.agent.core.boot.ServiceManager;
+import org.apache.skywalking.apm.agent.core.logging.api.ILog;
+import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
+import org.apache.skywalking.apm.agent.core.os.OSUtil;
+import org.apache.skywalking.apm.network.common.v3.KeyStringValuePair;
+
+public class JVMUtil {
+
+    private static final ILog LOGGER = LogManager.getLogger(JVMUtil.class);
+
+    private static Map<String, String> JAR_PATH_MAP = new HashMap<>();
+
+    private static Set<String> JAR_FILE_LIST = new HashSet<>();
+
+    private static List<String> getJarFileNameList() {
+        List<String> jarFileNameList = new ArrayList<>();
+        for (String jarFile : JAR_FILE_LIST) {
+            String jarFileName = jarFile.substring(jarFile.lastIndexOf("/") + 1);
+            jarFileNameList.add(jarFileName);
+        }
+        Collections.sort(jarFileNameList);
+        return jarFileNameList;
+    }
+
+    public static void loadJvmInfo() {
+        loadJarPath();
+        loadJarFileList();
+    }
+
+    private static void loadJarPath() {
+        Instrumentation instrumentation = ServiceManager.INSTRUMENTATION;
+
+        Class[] clzzs = instrumentation.getAllLoadedClasses();
+        Set<ClassLoader> classLoaders = new HashSet<>();
+        for (final Class clzz : clzzs) {
+            ClassLoader classLoader = clzz.getClassLoader();
+            if (classLoader != null) {
+                classLoaders.add(clzz.getClassLoader());
+            }
+        }
+
+        for (ClassLoader classLoader : classLoaders) {
+            classResCalc(classLoader.getClass());
+        }
+    }
+
+    private static void classResCalc(Class c) {
+        try {
+            java.net.URL url = c.getResource("/");
+            if (null != url) {
+                JAR_PATH_MAP.put(url.toString(), "");
+            }
+        } catch (Exception e) {
+            LOGGER.warn("Error msg: {}", e.getMessage());
+        }
+    }
+
+    private static void loadJarFileList() {
+        String[] classPathJar = getClasspath().split(OSUtil.getPathSeparator());

Review comment:
       *NULL_DEREFERENCE:*  object returned by `getClasspath()` could be null and is dereferenced at line 97.
   (at-me [in a reply](https://docs.muse.dev/docs/talk-to-muse/) with `help` or `ignore`)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org