Unverified Commit b091c0ae authored by 吴晟's avatar 吴晟 Committed by GitHub
Browse files

Make bootstrap instrumentation available in JDK9 - 11 (#3194)

* Make JDK9-11 available for bootstrap instrumentation

* Remove temp folder of old bootstrap instrumentation requirement.
parent 7b53d853
Loading
Loading
Loading
Loading
+34 −14
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@
package org.apache.skywalking.apm.agent.core.plugin.bootstrap;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.instrument.Instrumentation;
@@ -33,8 +32,6 @@ import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ClassInjector;
import net.bytebuddy.pool.TypePool;
import org.apache.skywalking.apm.agent.core.boot.AgentPackageNotFoundException;
import org.apache.skywalking.apm.agent.core.boot.AgentPackagePath;
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.plugin.AbstractClassEnhancePluginDefine;
@@ -82,7 +79,6 @@ public class BootstrapInstrumentBoost {

    public static AgentBuilder inject(PluginFinder pluginFinder, AgentBuilder agentBuilder,
        Instrumentation instrumentation) throws PluginException {

        Map<String, byte[]> classesTypeMap = new HashMap<String, byte[]>();

        if (!prepareJREInstrumentation(pluginFinder, classesTypeMap)) {
@@ -93,17 +89,36 @@ public class BootstrapInstrumentBoost {
            loadHighPriorityClass(classesTypeMap, highPriorityClass);
        }

        File temp = null;
        /**
         * This strategy is no longer available after Java 11.
         * Inject the classes into bootstrap class loader.
         */
        ClassInjector.UsingUnsafe.ofBootLoader().injectRaw(classesTypeMap);
        agentBuilder = agentBuilder.enableUnsafeBootstrapInjection();

        /**
         * Assures that all modules of the supplied types are read by the module of any instrumented type.
         * JDK Module system was introduced since JDK9.
         *
         * The following codes work only JDK Module system exist.
         */
        for (String highPriorityClass : HIGH_PRIORITY_CLASSES) {
            try {
            temp = new File(AgentPackagePath.getPath(), "bootstrapJarTmp");
        } catch (AgentPackageNotFoundException e) {
            logger.error(e, "Bootstrap plugin exist, but SkyWalking agent can't create bootstrapJarTmp folder. Shutting down.");
            throw new UnsupportedOperationException("Bootstrap plugin exist, but SkyWalking agent can't create bootstrapJarTmp folder. Shutting down.", e);
                agentBuilder = agentBuilder.assureReadEdgeTo(instrumentation, Class.forName(highPriorityClass));
            } catch (ClassNotFoundException e) {
                logger.error(e, "Fail to open the high priority class " + highPriorityClass + " to public access in JDK9+");
                throw new UnsupportedOperationException("Fail to open the high priority class " + highPriorityClass + " to public access in JDK9+", e);
            }
        }
        for (String generatedClass : classesTypeMap.keySet()) {
            try {
                agentBuilder = agentBuilder.assureReadEdgeTo(instrumentation, Class.forName(generatedClass));
            } catch (ClassNotFoundException e) {
                logger.error(e, "Fail to open the high generated class " + generatedClass + " to public access in JDK9+");
                throw new UnsupportedOperationException("Fail to open the high generated class " + generatedClass + " to public access in JDK9+", e);
            }
        }

        ClassInjector.UsingInstrumentation.of(temp, ClassInjector.UsingInstrumentation.Target.BOOTSTRAP, instrumentation).injectRaw(classesTypeMap);

        agentBuilder = agentBuilder.enableBootstrapInjection(instrumentation, temp);
        return agentBuilder;
    }

@@ -206,12 +221,17 @@ public class BootstrapInstrumentBoost {
     * @param loadedTypeMap hosts all injected class
     * @param className to load
     */
    private static void loadHighPriorityClass(Map<String, byte[]> loadedTypeMap, String className) {
    private static void loadHighPriorityClass(Map<String, byte[]> loadedTypeMap,
        String className) throws PluginException {
        byte[] enhancedInstanceClassFile;
        try {
            String classResourceName = className.replaceAll("\\.", "/") + ".class";
            InputStream resourceAsStream = AgentClassLoader.getDefault().getResourceAsStream(classResourceName);

            if (resourceAsStream == null) {
                throw new PluginException("High priority class " + className + " not found.");
            }

            ByteArrayOutputStream os = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
+0 −1
Original line number Diff line number Diff line
@@ -136,7 +136,6 @@
                                <mkdir dir="${project.basedir}/../../skywalking-agent/logs"/>
                                <copydir src="${project.basedir}/../config"
                                         dest="${project.basedir}/../../skywalking-agent/config" forceoverwrite="true"/>
                                <mkdir dir="${project.basedir}/../../skywalking-agent/bootstrapJarTmp"/>
                            </tasks>
                        </configuration>
                    </execution>
+1 −1
Original line number Diff line number Diff line
@@ -100,7 +100,7 @@ public class SkyWalkingAgent {
        try {
            agentBuilder = BootstrapInstrumentBoost.inject(pluginFinder, agentBuilder, instrumentation);
        } catch (Exception e) {
            logger.error(e, "SkyWalking agent inject boostrap instrumentation failure. Shutting down.");
            logger.error(e, "SkyWalking agent inject bootstrap instrumentation failure. Shutting down.");
            return;
        }

+1 −3
Original line number Diff line number Diff line
# Setup java agent
1. Agent is available for JDK 1.6 - 11.
1. Find `agent` folder in SkyWalking release package
1. Set `agent.service_name` in `config/agent.config`. Could be any String in English.
1. Set `collector.backend_service` in `config/agent.config`. Default point to `127.0.0.1:11800`, only works for local backend.
@@ -19,7 +20,6 @@ The agent release dist is included in Apache [official release](http://skywalkin
         apm-feign-default-http-9.x.jar
         apm-httpClient-4.x-plugin.jar
         .....
    +--- bootstrapJarTmp
    +--- logs
    skywalking-agent.jar
```
@@ -108,8 +108,6 @@ Now, we have the following known optional plugins.
## Bootstrap class plugins
All bootstrap plugins are optional, due to unexpected risk. Bootstrap plugins are provided in `bootstrap-plugins` folder.

**Make sure `bootstrapJarTmp` writable by agent, which is required by bootstrap instrumentation. New temp jar files will be generated there.**.

## Advanced Features
* Set the settings through system properties for config file override. Read [setting override](Setting-override.md).
* Use gRPC TLS to link backend. See [open TLS](TLS.md)