private static AgentBuilder getAgentBuilder()

in apm-agent-core/src/main/java/co/elastic/apm/agent/bci/ElasticApmAgent.java [680:754]


    private static AgentBuilder getAgentBuilder(final ByteBuddy byteBuddy, final CoreConfigurationImpl coreConfiguration, final Logger logger,
                                                final AgentBuilder.DescriptionStrategy descriptionStrategy, final boolean premain,
                                                final boolean useTypePoolCache) {
        AgentBuilder.LocationStrategy locationStrategy = AgentBuilder.LocationStrategy.ForClassLoader.WEAK;
        if (agentJarFile != null) {
            try {
                locationStrategy = new AgentBuilder.LocationStrategy.Compound(
                    // it's important to first try loading from the agent jar and not the class loader of the instrumented class
                    // the latter may not have access to the agent resources:
                    // when adding the agent to the bootstrap CL (appendToBootstrapClassLoaderSearch)
                    // the bootstrap CL can load its classes but not its resources
                    // the application class loader may cache the fact that a resource like AbstractSpan.class can't be resolved
                    // and also refuse to load the class
                    new AgentBuilder.LocationStrategy.Simple(ClassFileLocator.ForJarFile.of(agentJarFile)),
                    AgentBuilder.LocationStrategy.ForClassLoader.WEAK,
                    new AgentBuilder.LocationStrategy.Simple(new RootPackageCustomLocator("java.", ClassFileLocator.ForClassLoader.ofBootLoader()))
                );
            } catch (IOException e) {
                logger.warn("Failed to add ClassFileLocator for the agent jar. Some instrumentations may not work", e);
            }
        }
        return new AgentBuilder.Default(byteBuddy)
            .with(RedefinitionStrategy.RETRANSFORMATION)
            // when runtime attaching, only retransform up to 100 classes at once and sleep 100ms in-between as retransformation causes a stop-the-world pause
            .with(premain ? RedefinitionStrategy.BatchAllocator.ForTotal.INSTANCE : RedefinitionStrategy.BatchAllocator.ForFixedSize.ofSize(100))
            .with(premain ? RedefinitionStrategy.Listener.NoOp.INSTANCE : RedefinitionStrategy.Listener.Pausing.of(100, TimeUnit.MILLISECONDS))
            .with(new RedefinitionStrategy.Listener.Adapter() {
                @Override
                public Iterable<? extends List<Class<?>>> onError(int index, List<Class<?>> batch, Throwable throwable, List<Class<?>> types) {
                    logger.warn("Error while redefining classes {}", throwable.getMessage());
                    logger.debug(throwable.getMessage(), throwable);
                    return super.onError(index, batch, throwable, types);
                }
            })
            .with(descriptionStrategy)
            .with(locationStrategy)
            .with(new ErrorLoggingListener())
            // ReaderMode.FAST as we don't need to read method parameter names
            .with(useTypePoolCache
                ? new LruTypePoolCache(TypePool.Default.ReaderMode.FAST).scheduleEntryEviction()
                : AgentBuilder.PoolStrategy.Default.FAST)
            .ignore(any(), isReflectionClassLoader())
            .or(any(), classLoaderWithName("org.codehaus.groovy.runtime.callsite.CallSiteClassLoader"))
            .or(nameStartsWith("org.aspectj."))
            .or(nameStartsWith("org.groovy."))
            .or(nameStartsWith("com.p6spy."))
            .or(nameStartsWith("net.bytebuddy."))
            .or(nameStartsWith("org.stagemonitor."))
            .or(any(), classLoaderWithNamePrefix("com.newrelic."))
            .or(nameStartsWith("com.newrelic."))
            .or(any(), classLoaderWithNamePrefix("com.nr.agent."))
            .or(nameStartsWith("com.nr.agent."))
            .or(any(), classLoaderWithNamePrefix("com.dynatrace."))
            .or(nameStartsWith("com.dynatrace."))
            // AppDynamics
            .or(any(), classLoaderWithNamePrefix("com.singularity"))
            .or(nameStartsWith("com.singularity."))
            .or(any(), classLoaderWithNamePrefix("com.appdynamics."))
            .or(nameStartsWith("com.appdynamics."))
            .or(any(), classLoaderWithNamePrefix("com.cisco.mtagent."))
            .or(nameStartsWith("com.cisco.mtagent."))
            .or(any(), classLoaderWithNamePrefix("com.instana."))
            .or(nameStartsWith("com.instana."))
            .or(any(), classLoaderWithNamePrefix("datadog."))
            .or(nameStartsWith("datadog."))
            .or(nameStartsWith("org.glowroot."))
            .or(nameStartsWith("com.compuware."))
            .or(nameStartsWith("io.sqreen."))
            .or(nameStartsWith("com.contrastsecurity."))
            .or(nameContains("javassist"))
            .or(nameContains(".asm."))
            .or(anyMatch(coreConfiguration.getDefaultClassesExcludedFromInstrumentation()))
            .or(anyMatch(coreConfiguration.getClassesExcludedFromInstrumentation()))
            .disableClassFormatChanges();
    }